blob: 6799d200caf60fc94945b6e13a4b9ddd001cc203 [file] [log] [blame]
Christian Heimes53876d92008-04-19 00:31:39 +00001#include "Python.h"
2
Mark Dickinson87ec0852009-02-09 17:15:59 +00003#ifdef X87_DOUBLE_ROUNDING
4/* On x86 platforms using an x87 FPU, this function is called from the
5 Py_FORCE_DOUBLE macro (defined in pymath.h) to force a floating-point
6 number out of an 80-bit x87 FPU register and into a 64-bit memory location,
7 thus rounding from extended precision to double precision. */
8double _Py_force_double(double x)
9{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000010 volatile double y;
11 y = x;
12 return y;
Mark Dickinson87ec0852009-02-09 17:15:59 +000013}
14#endif
15
Mark Dickinson7abf8d42009-04-18 20:17:52 +000016#ifdef HAVE_GCC_ASM_FOR_X87
Mark Dickinsonb08a53a2009-04-16 19:52:09 +000017
18/* inline assembly for getting and setting the 387 FPU control word on
19 gcc/x86 */
20
21unsigned short _Py_get_387controlword(void) {
22 unsigned short cw;
23 __asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
24 return cw;
25}
26
27void _Py_set_387controlword(unsigned short cw) {
28 __asm__ __volatile__ ("fldcw %0" : : "m" (cw));
29}
30
Mark Dickinsonb08a53a2009-04-16 19:52:09 +000031#endif
32
33
Christian Heimes53876d92008-04-19 00:31:39 +000034#ifndef HAVE_HYPOT
35double hypot(double x, double y)
36{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 double yx;
Christian Heimes53876d92008-04-19 00:31:39 +000038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 x = fabs(x);
40 y = fabs(y);
41 if (x < y) {
42 double temp = x;
43 x = y;
44 y = temp;
45 }
46 if (x == 0.)
47 return 0.;
48 else {
49 yx = y/x;
50 return x*sqrt(1.+yx*yx);
51 }
Christian Heimes53876d92008-04-19 00:31:39 +000052}
53#endif /* HAVE_HYPOT */
54
55#ifndef HAVE_COPYSIGN
Mark Dickinson23b62862009-04-18 14:14:48 +000056double
Christian Heimes53876d92008-04-19 00:31:39 +000057copysign(double x, double y)
58{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 /* use atan2 to distinguish -0. from 0. */
60 if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
61 return fabs(x);
62 } else {
63 return -fabs(x);
64 }
Christian Heimes53876d92008-04-19 00:31:39 +000065}
66#endif /* HAVE_COPYSIGN */
67
Mark Dickinsonf2537862009-04-18 13:58:18 +000068#ifndef HAVE_ROUND
69double
70round(double x)
71{
72 double absx, y;
73 absx = fabs(x);
74 y = floor(absx);
75 if (absx - y >= 0.5)
Yury Selivanov614bfcc2015-06-02 18:53:46 -040076 y += 1.0;
Mark Dickinsonf2537862009-04-18 13:58:18 +000077 return copysign(y, x);
78}
79#endif /* HAVE_ROUND */