On Tue, 25 Dec 2001, Nadav Har'El wrote:

> On Tue, Dec 25, 2001, Shlomi Fish wrote about "Implementation of log(x) for SPARC32":
> > I need an implementation of the C function double log(double x) for SPARC
> > (32-bit - not UltraSPARC) written using Assembler or C or a combination of
> > both.
> >
> > I tried to rip code out of the glibc (which I was succesfully able to do
> > with the i386 architecture), but I could not make sense of what was going
> > on there.
>
> It's too bad the manual 
>(http://www.gnu.org/manual/glibc-2.2.3/html_node/libc_381.html#SEC390)
> doesn't say anything about the algorithms used. At least the source code
> should have a full description of the algorithm, or a pointer to an article
> or book describing it. If it doesn't, it's a shame :(
>

Actually, usually glibc calls the processor's FPU logarithm routine.

> Anyway, I doubt the log() code has any i386-specific stuff in there, except
> perhaps relying on the exact format of a floating point number (and how many
> bits of mantissa/exponent it has) to get absolute accuracy with the lowest
> number of operations. Isn't there an IEEE standard for 64-bit doubles by now?
>
> Why do you say glibc is i386 specific? Isn't it supposed to work on the Sparc
> too? I know that Redhat once had a version for sparc, so glibc must have
> worked on the sparc too!
>

I did not say it is i386 specific. I'm sure it works fine on SPARCs.
However, with things like math, there are separate routines for each
architecture. And the SPARC's one is too obfuscated, and I cannot
understand how I can rip the relevant code.

> If you want to understand the "tricks" usually used to calculated "special
> functions", like log (or sin or bessel functions, or whatever), you might
> want to take a look at books like "Numerical Recipes in C" (includes an
> interesting introduction to this area, but I couldn't find where it talks
> about log()), Knuth's "The Art of Computer Programming Vol 1", or other
> books.
>
> The site http://www.dattalo.com/technical/theory/logs.html (which I found
> by googling "logarithm algorithm calculating") might also be relevant to
> you (I didn't verify its quality though).
>

FYI, I eventually gave up and coded a logarithm function in perl:

#################
#!/usr/bin/perl

use strict;

my $e_const = exp(1);
my $e_const_reci = (1/$e_const);

my $number = shift || 516;

printf("%.80lf\n", mylog($number));
printf("%.80lf\n", log($number));

sub mylog
{
    my $number = shift;
    my $exp_base = 0;
    while ($number > 2)
    {
        $number *= $e_const_reci;
        $exp_base++;
    }
    $number -= 1;
    my $x_times_n = $number;
    my $result = 0;
    for(my $a = 1; $a < 28 ; $a++)
    {
        if ($a & 0x1)
        {
            $result += $x_times_n/$a;
        }
        else
        {
            $result -= $x_times_n/$a;
        }
        $x_times_n *= $number;
        #printf("%i: %.80lf\n", $a, $result);
    }
    return $result+$exp_base;
}
#####################################

Works like a charm with exactly the same accuracy. I'm going to convert it
to C, and by this I will have a truly portable log() implementation for
the kernel.

IMO, someone should port stuff like a large part of glibc as well as STL,
etc to the kernel. That would make the lives of programmers much easier
because they won't have to reinvent the wheel times and again.

Regards,

        Shlomi Fish

>
> --
> Nadav Har'El                        |      Tuesday, Dec 25 2001, 10 Tevet 5762
> [EMAIL PROTECTED]             |-----------------------------------------
> Phone: +972-53-245868, ICQ 13349191 |Are you still here? The message is over.
> http://nadav.harel.org.il           |Shoo! Go away!
>
> =================================================================
> To unsubscribe, send mail to [EMAIL PROTECTED] with
> the word "unsubscribe" in the message body, e.g., run the command
> echo unsubscribe | mail [EMAIL PROTECTED]
>



----------------------------------------------------------------------
Shlomi Fish        [EMAIL PROTECTED]
Home Page:         http://t2.technion.ac.il/~shlomif/
Home E-mail:       [EMAIL PROTECTED]

"Let's suppose you have a table with 2^n cups..."
"Wait a second - is n a natural number?"


=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to