]> sourceware.org Git - glibc.git/blobdiff - sysdeps/i386/fpu/e_pow.S
Fix x86 pow inaccuracy for large integer exponents (bug 706).
[glibc.git] / sysdeps / i386 / fpu / e_pow.S
index 63c44f135758485d8acf6c6929ee1d5236adb306..73d2421162af67748cefbc7e412974b4cdb85c3c 100644 (file)
@@ -32,6 +32,9 @@ limit:        .double 0.29
        ASM_TYPE_DIRECTIVE(p63,@object)
 p63:   .byte 0, 0, 0, 0, 0, 0, 0xe0, 0x43
        ASM_SIZE_DIRECTIVE(p63)
+       ASM_TYPE_DIRECTIVE(p10,@object)
+p10:   .byte 0, 0, 0, 0, 0, 0, 0x90, 0x40
+       ASM_SIZE_DIRECTIVE(p10)
 
        .section .rodata.cst16,"aM",@progbits,16
 
@@ -114,9 +117,17 @@ ENTRY(__ieee754_pow)
        fucomp  %st(1)          // y : x
        fnstsw
        sahf
-       jne     2f
+       jne     3f
 
-       /* OK, we have an integer value for y.  */
+       /* OK, we have an integer value for y.  If large enough that
+          errors may propagate out of the 11 bits excess precision, use
+          the algorithm for real exponent instead.  */
+       fld     %st             // y : y : x
+       fabs                    // |y| : y : x
+       fcompl  MO(p10)         // y : x
+       fnstsw
+       sahf
+       jnc     2f
        popl    %eax
        cfi_adjust_cfa_offset (-4)
        popl    %edx
@@ -157,7 +168,14 @@ ENTRY(__ieee754_pow)
 
        cfi_adjust_cfa_offset (8)
        .align ALIGNARG(4)
-2:     /* y is a real number.  */
+2:     // y is a large integer (absolute value at least 1L<<10), but
+       // may be odd unless at least 1L<<64.  So it may be necessary
+       // to adjust the sign of a negative result afterwards.
+       fxch                    // x : y
+       fabs                    // |x| : y
+       fxch                    // y : x
+       .align ALIGNARG(4)
+3:     /* y is a real number.  */
        fxch                    // x : y
        fldl    MO(one)         // 1.0 : x : y
        fldl    MO(limit)       // 0.29 : 1.0 : x : y
@@ -182,9 +200,41 @@ ENTRY(__ieee754_pow)
        f2xm1                   // 2^fract(y*log2(x))-1 : int(y*log2(x))
        faddl   MO(one)         // 2^fract(y*log2(x)) : int(y*log2(x))
        fscale                  // 2^fract(y*log2(x))*2^int(y*log2(x)) : int(y*log2(x))
-       addl    $8, %esp
-       cfi_adjust_cfa_offset (-8)
        fstp    %st(1)          // 2^fract(y*log2(x))*2^int(y*log2(x))
+       testb   $2, %dh
+       jz      292f
+       // x is negative.  If y is an odd integer, negate the result.
+       fldl    20(%esp)        // y : abs(result)
+       fld     %st             // y : y : abs(result)
+       fabs                    // |y| : y : abs(result)
+       fcompl  MO(p63)         // y : abs(result)
+       fnstsw
+       sahf
+       jnc     291f
+
+       // We must find out whether y is an odd integer.
+       fld     %st             // y : y : abs(result)
+       fistpll (%esp)          // y : abs(result)
+       fildll  (%esp)          // int(y) : y : abs(result)
+       fucompp                 // abs(result)
+       fnstsw
+       sahf
+       jne     292f
+
+       // OK, the value is an integer, but is it odd?
+       popl    %eax
+       cfi_adjust_cfa_offset (-4)
+       popl    %edx
+       cfi_adjust_cfa_offset (-4)
+       andb    $1, %al
+       jz      290f            // jump if not odd
+       // It's an odd integer.
+       fchs
+290:   ret
+       cfi_adjust_cfa_offset (8)
+291:   fstp    %st(0)          // abs(result)
+292:   addl    $8, %esp
+       cfi_adjust_cfa_offset (-8)
        ret
 
 
@@ -230,6 +280,16 @@ ENTRY(__ieee754_pow)
        testb   $2, %dh
        jz      16f             // jump if x == +inf
 
+       // fistpll raises invalid exception for |y| >= 1L<<63, so test
+       // that (in which case y is certainly even) before testing
+       // whether y is odd.
+       fld     %st             // y : y
+       fabs                    // |y| : y
+       fcompl  MO(p63)         // y
+       fnstsw
+       sahf
+       jnc     16f
+
        // We must find out whether y is an odd integer.
        fld     %st             // y : y
        fistpll (%esp)          // y
@@ -239,20 +299,13 @@ ENTRY(__ieee754_pow)
        sahf
        jne     17f
 
-       // OK, the value is an integer, but is the number of bits small
-       // enough so that all are coming from the mantissa?
+       // OK, the value is an integer.
        popl    %eax
        cfi_adjust_cfa_offset (-4)
        popl    %edx
        cfi_adjust_cfa_offset (-4)
        andb    $1, %al
        jz      18f             // jump if not odd
-       movl    %edx, %eax
-       orl     %edx, %edx
-       jns     155f
-       negl    %eax
-155:   cmpl    $0x00200000, %eax
-       ja      18f             // does not fit in mantissa bits
        // It's an odd integer.
        shrl    $31, %edx
        fldl    MOX(minf_mzero, %edx, 8)
@@ -289,6 +342,16 @@ ENTRY(__ieee754_pow)
        testb   $2, %dh
        jz      25f
 
+       // fistpll raises invalid exception for |y| >= 1L<<63, so test
+       // that (in which case y is certainly even) before testing
+       // whether y is odd.
+       fld     %st             // y : y
+       fabs                    // |y| : y
+       fcompl  MO(p63)         // y
+       fnstsw
+       sahf
+       jnc     25f
+
        fld     %st             // y : y
        fistpll (%esp)          // y
        fildll  (%esp)          // int(y) : y
@@ -297,16 +360,13 @@ ENTRY(__ieee754_pow)
        sahf
        jne     26f
 
-       // OK, the value is an integer, but is the number of bits small
-       // enough so that all are coming from the mantissa?
+       // OK, the value is an integer.
        popl    %eax
        cfi_adjust_cfa_offset (-4)
        popl    %edx
        cfi_adjust_cfa_offset (-4)
        andb    $1, %al
        jz      27f             // jump if not odd
-       cmpl    $0xffe00000, %edx
-       jbe     27f             // does not fit in mantissa bits
        // It's an odd integer.
        // Raise divide-by-zero exception and get minus infinity value.
        fldl    MO(one)
@@ -329,6 +389,14 @@ ENTRY(__ieee754_pow)
 21:    testb   $2, %dh
        jz      22f
 
+       // fistpll raises invalid exception for |y| >= 1L<<63, so test
+       // that (in which case y is certainly even) before testing
+       // whether y is odd.
+       fcoml   MO(p63)         // y
+       fnstsw
+       sahf
+       jnc     22f
+
        fld     %st             // y : y
        fistpll (%esp)          // y
        fildll  (%esp)          // int(y) : y
@@ -337,16 +405,13 @@ ENTRY(__ieee754_pow)
        sahf
        jne     23f
 
-       // OK, the value is an integer, but is the number of bits small
-       // enough so that all are coming from the mantissa?
+       // OK, the value is an integer.
        popl    %eax
        cfi_adjust_cfa_offset (-4)
        popl    %edx
        cfi_adjust_cfa_offset (-4)
        andb    $1, %al
        jz      24f             // jump if not odd
-       cmpl    $0xffe00000, %edx
-       jae     24f             // does not fit in mantissa bits
        // It's an odd integer.
        fldl    MO(mzero)
        ret
This page took 0.033935 seconds and 5 git commands to generate.