/* sinfcosf.s -- sinf and cosf for standard math library. Written by Eric Postpischil, June 2007. */ #define SignificandBits 24 #define ExponentBits 8 .literal8 .align 3 Half: .double .5 .const .align 4 /* Coefficients to calculate x*(x**4+c0*x**2+c1) * c4*(x**4+c2*x**2+c3). c0 and c2 are paired for loading into an XMM register, and so are c1 and c3. */ // Two binades, fractions of pi, minimize ULP error, refactored: C02: .double -4.1647719829166299, -3.6493923712207774 C13: .double 15.485466333375713, 2.6522034727867632 C4: .double 0.076492480587904088 /* ExponentFold specifies how many bits of the exponent will not be used to look up entries in the table. This must match the ExponentFold used to generate the table. The behavior for infinite inputs relies on the table contents -- if the table entries looked up for an infinite input are m0 and m1, then m0*infinity + m1*infinity should generate a NaN. This happens of either m0 or m1 is a zero or they have opposite signs. If this were not true, additional arrangements would have to be made for infinite inputs. */ #define ExponentFold 1 // Include the reduction table. See sinfcosfMakeTable.c for more information. #include "sinfcosfTable.s" // Rename the general registers (just to make it easier to keep track of them). #if defined __i386__ #define r0 %eax #define r1 %ecx #define r2 %edx #define r3 %ebx #define r4 %esp #define r5 %ebp #define r6 %esi #define r7 %edi #elif defined __x86_64__ #define r0 %rax #define r1 %rcx #define r2 %rdx #define r3 %rbx #define r4 %rsp #define r5 %rbp #define r6 %rsi #define r7 %rdi #else #error "Unknown architecture." #endif .text // Define symbols common to sinf and cosf. #define BaseP r0 // Base address for position-independent addressing. #define xInteger r1 // The input x in an integer register. #define p %xmm0 // Must be in %xmm0 for return on x86_64. #define x %xmm1 #define ki %xmm2 #define k %xmm3 #define m1 %xmm4 #define pa m1 #if defined __i386__ // Define location of argument x. #define Argx 4(%esp) // Define how to address data. BaseP must contain the address of label 0. #define Address(label) label-0b(BaseP) #elif defined __x86_64__ // Define location of argument x. #define Argx %xmm0 // Define how to address data. #define Address(label) label(%rip) #endif /* float sinf(float x). Notes: Citations in parentheses below indicate the source of a requirement. "C" stands for ISO/IEC 9899:TC2. The Open Group specification (IEEE Std 1003.1, 2004 edition) adds no requirements since it defers to C and requires errno behavior only if we choose to support it by arranging for "math_errhandling & MATH_ERRNO" to be non-zero, which we do not. Return value: For +/- zero, return zero with same sign (C F.9 12 and F.9.1.6). For +/- infinity, return a NaN (C F.9.1.6). For a NaN, return the same NaN (C F.9 11 and 13). Otherwise: If the rounding mode is round-to-nearest, return sine(x) faithfully rounded. Not currently implemented: In other rounding modes, return sine(x) possibly with slightly worse error, not necessarily honoring the rounding mode (Ali Sazegari narrowing C F.9 10). All results are in [-1, 1]. This is corollary to faithful rounding. Exceptions: Raise underflow for a denormal result (C F.9 7 and Draft Standard for Floating-Point Arithmetic P754 Draft 1.2.5 9.5). If the input is the smallest normal, underflow may or may not be raised. This is stricter than the older 754 standard. May or may not raise inexact, even if the result is exact (C F.9 8). Raise invalid if the input is a signalling NaN (C 5.2.4.2.2 3, in spite of C 4.2.1) or an infinity (C F.9.1.6) but not if the input is a quiet NaN (C F.9 11). May not raise exceptions otherwise (C F.9 9). Properties: Desired to be monotonic. Not yet proven! Exhaustive testing proved this routine returns faithfully rounded results. */ .align 5 .globl _sinf _sinf: // Put x into an integer register. #if defined __i386__ mov Argx, xInteger // Put x into xInteger. #elif defined __x86_64__ movd Argx, xInteger // Put x into xInteger. #endif cvtss2sd Argx, x // Convert x to double precision. #if defined __i386__ // Get address of 0 in BaseP. call 0f // Push program counter onto stack. 0: pop BaseP // Get program counter. #elif defined __x86_64__ // Get address of reduction table in BaseP. lea ReductionTable(%rip), BaseP #endif unpcklpd x, x // Duplicate x. /* Shift: SignificandBits-1 bits right to remove significand. ExponentFold bits right to remove unused bits of exponent. 4 bits left to multiply by size of a table entry. */ shr $SignificandBits-1+ExponentFold-4, xInteger // Clear bits other than those we are using from exponent. and $(1<