A89: Re: What the heck is wrong with this?!


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

A89: Re: What the heck is wrong with this?!




One more thing:

 > >  The example (which works) you multiplied a floating-point value by another
 > >  floating-point value, and an automatic cast was given to the copy (equal)
 > >  operator.  However, in both of your examples which don't work, you 
 > attempted
 > >  to multiply an integer by a float.  In most modern compilers, this is
 > >  normally allowed, but it seems you must link with a special library to make
 > >  it work in TIGCC.  You might want to try the following type-casts, and see
 > >  if it works (for your two examples):

It is completely incorrect. Multiplying anything with anything is 
allowed in C (well, scalar types anyway), since C sports automatic 
type casting on scalar types (as opposed to Pascal, for example). 
It is not a question of compilers, it is the question of the language 
definition. On top of that, if a particular combination of operands 
is illegal then the compiler gives an error message and does not try 
to load magic library calls. No library can turn an invalid construct 
to a valid one.

The reason for

 int x = 3.8 * 2.7;
 
working, while

 int x;
 
 x = x * 3.8;
 
missing library calls is actually very simple indeed: in the first
case, it is the *compiler* which will calculate the result. Since the
expression contains only constants, the compiler will calculate the
result at compile time and transform it to this:

  int x = 10;
  
(2.7*3.8 is 10.26, which truncated to integer gives 10).
Not even the stupidest compiler wiull generate code for calculating a
constant in run-time. All compilers evaluate constant expressions in
compile time.

Now the second case is different, one of the operands for the multiply
is a variable therefore the compiler can not calculate the result. It
has to turn it into a real run-time multiply.

Since one of the operands of the multiply is a double (the constant), 
the other operand is turned to double too. (This is an implicit type
cast, defined by the C language). To do this, the compiler calls a 
support library function that turns an int to a double.
Then the two doubles must be multiplied, thus the compiler invokes the
support library function for double multiply. Now the result should be 
stored in an integer variable. The compiler, obeying yet another
implicit type cast, will thus invoke a third support lib function
which transforms doubles to integers. The result is finally put into x.

This is the reason there were 3 functions missing, their names explain 
what they do, as shown with the capital letters:

_floatsidf  : make FLOAT transforming a Signed Integer to a Double Float
_adddf3     : ADD Double Float (the 3 is reference to a 3-op machine)
_fixdfsi	: make FIXpoint by converting a Double Float to a Signed Integer

Best Regards,

Zoltan



References: