pymath.c 1.53 KB
Newer Older
1 2
#include "Python.h"

3 4 5 6 7 8 9
#ifdef X87_DOUBLE_ROUNDING
/* On x86 platforms using an x87 FPU, this function is called from the
   Py_FORCE_DOUBLE macro (defined in pymath.h) to force a floating-point
   number out of an 80-bit x87 FPU register and into a 64-bit memory location,
   thus rounding from extended precision to double precision. */
double _Py_force_double(double x)
{
10 11 12
    volatile double y;
    y = x;
    return y;
13 14 15
}
#endif

16
#ifdef HAVE_GCC_ASM_FOR_X87
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

/* inline assembly for getting and setting the 387 FPU control word on
   gcc/x86 */

unsigned short _Py_get_387controlword(void) {
    unsigned short cw;
    __asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
    return cw;
}

void _Py_set_387controlword(unsigned short cw) {
    __asm__ __volatile__ ("fldcw %0" : : "m" (cw));
}

#endif


34 35 36
#ifndef HAVE_HYPOT
double hypot(double x, double y)
{
37
    double yx;
38

39 40 41 42 43 44 45 46 47 48 49 50 51
    x = fabs(x);
    y = fabs(y);
    if (x < y) {
        double temp = x;
        x = y;
        y = temp;
    }
    if (x == 0.)
        return 0.;
    else {
        yx = y/x;
        return x*sqrt(1.+yx*yx);
    }
52 53 54 55
}
#endif /* HAVE_HYPOT */

#ifndef HAVE_COPYSIGN
56
double
57 58
copysign(double x, double y)
{
59 60 61 62 63 64
    /* use atan2 to distinguish -0. from 0. */
    if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
        return fabs(x);
    } else {
        return -fabs(x);
    }
65 66 67
}
#endif /* HAVE_COPYSIGN */

68 69 70 71 72 73 74 75
#ifndef HAVE_ROUND
double
round(double x)
{
    double absx, y;
    absx = fabs(x);
    y = floor(absx);
    if (absx - y >= 0.5)
76
    y += 1.0;
77 78 79
    return copysign(y, x);
}
#endif /* HAVE_ROUND */