07-30-05 01:51 AM
Bruno Bonfils <asyd+news@asyd.net> wrote:
> PM <pm@gmx.de> writes:
>
>
> So, why :
>
> % cat isinf.c
> #include <stdio.h>
> #include <math.h>
>
> int main (void)
> {
> float myfloat = 0.000;
> return isinf(myfloat);
> }
>
> % gcc -Wall isinf.c -o isinf -lm
> isinf.c: In function `main':
> isinf.c:7: warning: implicit declaration of function `isinf'
> Undefined first referenced
> symbol in file
> isinf /var/tmp//cc0mv2QJ.o
> ld: fatal: Symbol referencing errors. No output written to isinf
A real answer:
Because C99's isinf() can not be implemented in the library.
C99 defines isinf() to be able to cope with ANY floating-point type.
And obviously C doesn't allow that in an actual function.
So this has to be implemented by a macro that
is specific to a particular implementation of the C99 compiler.
In Sun Studio, isinf() becomes a compiler builtin function called
__builtin_isinf() and the compiler takes care of that.
# uname -a
SunOS park2 5.10 Generic_118822-02 sun4u sparc SUNW,Sun-Blade-1000
# cat isinf.c
#include <math.h>
int func(double a)
{
return isinf(a);
}
# cc -V isinf.c -c -xO3 -S
cc: Sun C 5.7 2005/01/07
acomp: Sun C 5.7 2005/01/07
iropt: Sun Compiler Common 10 2005/01/07
cg: Sun Compiler Common 10 2005/01/07
# cat isinf.s
...
.global func
func:
/* 000000 5 */ sethi %hi(0x80000000),%o5
/* 0x0004 */ sethi %hi(0x7ff00000),%o4
/* 0x0008 */ andn %o0,%o5,%o3
/* 0x000c 4 */ add %sp,-104,%sp
/* 0x0010 5 */ xor %o3,%o4,%o2
/* 0x0014 */ st %o1,[%sp+92]
/* 0x0018 */ srl %o2,0,%g5
/* 0x001c */ st %o0,[%sp+96]
/* 0x0020 */ srl %o1,0,%o1
/* 0x0024 */ sub %g0,%g5,%g4
/* 0x0028 */ sub %g0,%o1,%g1
/* 0x002c */ srlx %g4,63,%g3
/* 0x0030 */ srlx %g1,63,%o5
/* 0x0034 */ xor %g3,1,%g2
/* 0x0038 */ xor %o5,1,%o0
/* 0x003c */ and %g2,%o0,%o0
/* 0x0040 */ retl ! Result = %o0
/* 0x0044 */ add %sp,104,%sp
/* 0x0048 0 */ .type func,2
/* 0x0048 0 */ .size func,(.-func)
...
As you see from above, there's no call.
A background on what's going on above:
isinf() macro is defined within the #if of:
#if defined(_STDC_C99) || _XOPEN_SOURCE - 0 >= 600 || defined(__C99FEATURES_
_)
and gcc 3.x obviously doesn't seem to define any of those macros
(and it shouldn't unless it can cope with all those macros
under that condition).
As Casper pointed out, there's a new math.h coming
that would work with gcc.
> and for Thomas :
>
> % gcc -Wall isinf.c -o isinf -lm -lsunmath
> isinf.c: In function `main':
> isinf.c:7: warning: implicit declaration of function `isinf'
> ld: fatal: library -lsunmath: not found
> ld: fatal: File processing errors. No output written to isinf
> collect2: ld returned 1 exit status
> zsh: exit 1 gcc -Wall isinf.c -o isinf -lm -lsunmath
libsunmath.a is included in the Sun compiler.
However, isinf() in libsunmath is different than C99's isinf().
It's "int isinf(double)" and is a real library routine.
--
#pragma ident "Seongbae Park, compiler, http://blogs.sun.com/seongbae/"
[ Post a follow-up to this message ]
|