[build system] defaults in C, specializations in asm?

"Markus Wichmann" <[email protected]>
Newsgroups gmane.linux.lib.dietlibc
Message-ID <[email protected]>
Hi all,

I am currently enhancing dietlibc's poor libm for x86_64 and came across
an idea: would it be possible to write default implementations for
necessairy functions in C and have the build system look for specialized
assembler versions?

For instance, you could write an exponential function: e^x is defined as
being the sum for n=0 to infinity over x^n/n!. Or, in C:

#define EPSILON (1e-20) /*or whatever else suits you */
double exp(double x)
{
    if (x < 0)
        return 1/exp(-x);
    else if (x == 0)
        return 1;
    else {
        double n = 1;
        double sum = 1;
        double addend = 1, last_n;

        do {
            addend *= x/n;
            sum += addend;
            last_n = n;
            n += 1;
        } while (addend > EPSILON && last_n != n);
        return sum;
    }
}

Place that into a file like libm/exp.c and then tell the build system: If
there is $(ARCH)/exp.S put that in as dependency for libm, else use
libm/exp.c. That way every arch the support for which is still in
development would have a complete if slow library. Adding new
architectures would become easier a lot, too.

I have to admit, though, the trigonometric functions might impose a bit of
a challenge.

Speaking of which: I saw you use the fprem1 trick for i386 for the sine.
Asked rationally: What for? fsin fails to operate if the source operand is
outside the bounds of -2^63 to 2^63. But in that case even long double
yields far too many quantization errors. Even the addition of one is an
idempotent operation in that area. Basically, if the input gets so big we
could return anything between -1 and 1 as it is simply impossible to know
which angle was the original meaning of the input. Using fprem1 does not
necessarily make the result closer to the precise result. (Alway returning
zero is OK in that case. And it's faster than trying to recover already
lost accuracy.)

HTH,
Markus
-- 
Progress (n.): Process through which USENET evolved from smart people in
front of dumb terminals to dumb people in front of smart terminals.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.