source for math functions? - Linux

This is a discussion on source for math functions? - Linux ; Hi guys, this subject is a bit of topic and I am sorry in advance... I am trying to figure out how math functions like pow exp log etc are implemented in the C++ stl lib for example (cmath or ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: source for math functions?

  1. source for math functions?

    Hi guys, this subject is a bit of topic and I am sorry in advance... I
    am trying to figure out how math functions like pow exp log etc are
    implemented in the C++ stl lib for example (cmath or math.h). So that I
    thought I might be able able to figure out by looking at their
    implementation under linux as the source code is available. But of
    course the source tree is huge so I was wondering if someone could
    point me to the right files (which lib I should look into).
    Many thanks, mark


  2. Re: source for math functions?

    In article <1156940152.916404.81280@h48g2000cwc.googlegroups.c om>,
    mast2as@yahoo.com wrote:
    >Hi guys, this subject is a bit of topic and I am sorry in advance... I
    >am trying to figure out how math functions like pow exp log etc are
    >implemented in the C++ stl lib for example (cmath or math.h).


    Those functions are in libm, which is distributed as part of the glibc
    package, or whatever other libc implementation you use. A lot of them are
    written in assembly code to take advantage of FPU instructions, so there is a
    separate implementation for each processor type.

    Taking glibc and exp() as an example, all of the following source files are
    involved in its implementation on various platforms:

    sysdeps/i386/fpu/e_exp.S
    sysdeps/i386/fpu/t_exp.c
    sysdeps/ia64/fpu/e_exp.S
    sysdeps/ia64/fpu/w_exp.c
    sysdeps/ieee754/dbl-64/e_exp.c
    sysdeps/ieee754/dbl-64/t_exp.c
    sysdeps/ieee754/dbl-64/w_exp.c
    sysdeps/m68k/fpu/e_exp.c
    sysdeps/m68k/fpu/t_exp.c

    --
    The attacker\x92s overall goal would very probably be to convince other users
    to run an unsafe program, by using the digital signature to convince them
    that it is actually bona fide Microsoft software and therefore safe to run.
    -- security bulletin MS01-017 ushers in a new definition of "safe"

  3. Re: source for math functions?

    Thank you Alan -
    Alan Curry wrote:
    >
    > Those functions are in libm, which is distributed as part of the glibc
    > package, or whatever other libc implementation you use. A lot of them are
    > written in assembly code to take advantage of FPU instructions, so there is a
    > separate implementation for each processor type.



  4. Re: source for math functions?

    "mast2as@yahoo.com" said:
    >Hi guys, this subject is a bit of topic and I am sorry in advance... I
    >am trying to figure out how math functions like pow exp log etc are
    >implemented in the C++ stl lib for example (cmath or math.h). So that I
    >thought I might be able able to figure out by looking at their
    >implementation under linux as the source code is available.


    These can be a bit of pain to dig out (or at least I find the glibc source
    rather hard to find anything). However, I hope I can offer some help
    for these.

    When doing this kind of digging, the first thing to do could be to find
    out what is the actual library call name being made -- some things can
    be pretty obscured by macros in various headers.

    So, we can start by creating a small piece of code that calls the
    function we're interested in:

    #include

    void foo() {
    float f=acos(0.5);
    }

    There's no need for the code to contain main, to be runnable, or to do
    anything sensible at all -- but the code must be correct enough to
    compile, and it must contain all the appropriate headers.

    The next thing is to run the above file through the C preprocessor;
    let's say the file is named foo.c, so the following is what is needed:

    gcc -E foo.c | less

    (pipe to less, as the gcc invocation will print the preprocessing results
    to stdout)

    From the above (scrolling to the very end of the output) we find that
    the call to acos still calls acos (so, there's no macro trickery this
    time). Also, we find the declaration for acos, stripped of all
    preprocessing magic:

    extern double acos (double __x) __attribute__ ((__nothrow__)); extern double __acos (double __x) __attribute__ ((__nothrow__));

    Note the double declaration (one for acos, one for __acos).

    The next thing is to find the actual implementation code for this function:

    sysdeps/generic/w_acos.c contains definition for __acos, and declares
    acos a weak alias to __acos. Further, this code will call __ieee754_acos,
    declared in math/math_private.h and defined in sysdeps/i386/e_acos.S .
    This latter file is no longer in C, but in assembly language, and
    contains the assembly-language floating point instruction calls which
    are used for the calculation.


    Pretty much, "grep" command is a valuable friend in tracking these.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)

+ Reply to Thread