1

I've seen this question, whose answers conclude that builtin math functions (like __builtin_sin, __builtin_fmod, etc.) can be substituted for functions from the C standard library.

I wrote the following program:

float fmod_test(float arg1, float arg2) {
    return __builtin_fmod(arg1, arg2)
}

void _start() {}

And compiled it as follows:

gcc -nostdlib test.c -o test

Unfortunately, I received the following error:

/tmp/ccuHpvCP.o: In function `fmod_test':
test.c:(.text+0x1d): undefined reference to `fmod'
collect2: error: ld returned 1 exit status

It seems that __builtin_fmod uses fmod in the background and needs to link to it, rather than producing an inline version as might be expected of a "built in" function.

Is there any way of using these builtin functions without linking to external libraries?

0

2 Answers 2

4

The answer to this question depends on exactly which C compiler you are using. You appear to be using GCC; the answer for that compiler is no.

These functions are "built in" in the sense that GCC knows their names and can optimize away some calls to them, for instance fmod(7.0, 2.0) may well be evaluated at compile time. But GCC does not provide runtime definitions of these functions. It relies on the C library, which is a separate project, to provide them.

Sign up to request clarification or add additional context in comments.

1 Comment

Saying GCC does not provide run-time definitions may be over ply broad. It might for certain architectures, at least in the sense of providing instructions to do it, and not for others, and it might for some arguments (such as where one is a constant for which the function is easier to compute) and not others.
2

As the gcc manual says:

Many of these functions are only optimized in certain cases; if they are not optimized in a particular case, a call to the library function is emitted.

So there is no guaranteed way to avoid the possibility of a call to the library function.

However, you can experiment with how you call the function and your optimization options, in hopes of finding a combination that does get inlined. In particular, with floating point builtins, gcc will usually only inline them if -ffast-math is in effect, because its inline code may not attain as much precision or handle all corner cases (NaN, infinity, denormals, setting errno, etc) as the carefully-written library function would. That's the case here, and indeed if you enable -ffast-math you do get inlined code: see on godbolt. (It will look better if you turn on optimization.)

Of course, if you later change your compiler options, or call the function in a different way, or switch to a different compiler version, the compiler might again emit a library call. You'll know if this happens because your program won't link, so at least it won't break silently, and you can then try to readjust your code and/or compilation options, or if necessary, write or import your own version of the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.