140b93d101254d2693a613bcd1f17344a9c9033a
[platform/upstream/glibc.git] / sysdeps / x86_64 / fpu / e_log2l.S
1 /*
2  * Written by J.T. Conklin <jtc@netbsd.org>.
3  * Adapted for use as log2 by Ulrich Drepper <drepper@cygnus.com>.
4  * Public domain.
5  *
6  * Changed to use fyl2xp1 for values near 1, <drepper@cygnus.com>.
7  * Adapted for x86-64 by Andreas Jaeger <aj@suse.de>.
8  */
9
10 #include <machine/asm.h>
11
12         .section .rodata.cst8,"aM",@progbits,8
13
14         .p2align 3
15         ASM_TYPE_DIRECTIVE(one,@object)
16 one:    .double 1.0
17         ASM_SIZE_DIRECTIVE(one)
18         /* It is not important that this constant is precise.  It is only
19            a value which is known to be on the safe side for using the
20            fyl2xp1 instruction.  */
21         ASM_TYPE_DIRECTIVE(limit,@object)
22 limit:  .double 0.29
23         ASM_SIZE_DIRECTIVE(limit)
24
25
26 #ifdef PIC
27 # define MO(op) op##(%rip)
28 #else
29 # define MO(op) op
30 #endif
31
32         .text
33 ENTRY(__ieee754_log2l)
34         fldl    MO(one)
35         fldt    8(%rsp)         // x : 1
36         fxam
37         fnstsw
38         fld     %st             // x : x : 1
39         testb   $1, %ah
40         jnz     3f              // in case x is NaN or ±Inf
41 4:      fsub    %st(2), %st     // x-1 : x : 1
42         fld     %st             // x-1 : x-1 : x : 1
43         fabs                    // |x-1| : x-1 : x : 1
44         fcompl  MO(limit)       // x-1 : x : 1
45         fnstsw                  // x-1 : x : 1
46         andb    $0x45, %ah
47         jz      2f
48         fstp    %st(1)          // x-1 : 1
49         fyl2xp1                 // log(x)
50         ret
51
52 2:      fstp    %st(0)          // x : 1
53         fyl2x                   // log(x)
54         ret
55
56 3:      testb   $4, %ah
57         jnz     4b              // in case x is ±Inf
58         fstp    %st(1)
59         fstp    %st(1)
60         ret
61 END (__ieee754_log2l)
62
63
64 ENTRY(__log2l_finite)
65         fldl    MO(one)
66         fldt    8(%rsp)         // x : 1
67         fld     %st             // x : x : 1
68         fsub    %st(2), %st     // x-1 : x : 1
69         fld     %st             // x-1 : x-1 : x : 1
70         fabs                    // |x-1| : x-1 : x : 1
71         fcompl  MO(limit)       // x-1 : x : 1
72         fnstsw                  // x-1 : x : 1
73         andb    $0x45, %ah
74         jz      2b
75         fstp    %st(1)          // x-1 : 1
76         fyl2xp1                 // log(x)
77         ret
78 END (__log2l_finite)