tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] libtcc bug when evaluating math expressions


From: david wallin
Subject: Re: [Tinycc-devel] libtcc bug when evaluating math expressions
Date: Thu, 27 Nov 2003 17:15:47 +0000

Daniel,

I tried to implement my own abs function, not optimal in any way. Evaluating "evaluate_me(-1.0)" gives:

ans1 = 256.000000


cheers,

--david.


8<---------------------------------BEGIN

#include <libtcc.h>
//#include "cc_samples.h"

#include <math.h>

#define TCC19
//#define TCC20

double dabs (double x) {
  return x > 0.0 ? x : -x;
}

int main() {

char *eval_me = "#include <math.h>\ndouble evaluate_me(double v) { return dabs(v); }\0" ;

#ifdef TCC20
  double (*evaluate_me)(double);
  unsigned long val;
#endif

  TCCState *tcc_state;
  tcc_state = tcc_new();
  tcc_set_output_type(tcc_state, TCC_OUTPUT_MEMORY);
  tcc_add_symbol(tcc_state, "dabs", (unsigned long)&dabs);
  tcc_add_library(tcc_state, "m");

  tcc_compile_string(tcc_state, eval_me);

  tcc_relocate(tcc_state);

#ifdef TCC19
double (*evaluate_me)(double) = (double (*)(double))tcc_get_symbol(tcc_state, "evaluate_me");
#endif
#ifdef TCC20
    tcc_get_symbol(tcc_state, &val, "evaluate_me");
    evaluate_me = (void *)val;
#endif

    printf("ans1 = %f\n", evaluate_me(-1.0));

  return 0;

}

8<---------------------------------END


On Thursday, November 27, 2003, at 04:03 PM, Daniel Glöckner wrote:

On Thu, Nov 27, 2003 at 01:28:45PM +0000, David Wallin wrote:
  char *eval_me =   "double evaluate_me(double x, double v) { return
(double) ((-1.0 * x) > v * fabs(v)); }\0" ;

#include <math.h>

fabs is not defined and so defaults to returning an integer in eax
instead of a double in st0. On exit of fabs(), eax holds an undefined
value which is then converted to a double and multiplied with v.
GCC knows about fabs and generates the fabs instruction, even with -O0.

  Daniel





reply via email to

[Prev in Thread] Current Thread [Next in Thread]