blob: 24b804223eef19a9e551d6916ea1230f7bfd7644 [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 */
Gregory P. Smith3015fb82018-11-12 22:01:22 -080020#ifdef _Py_MEMORY_SANITIZER
Gregory P. Smith1584a002018-11-12 12:07:14 -080021__attribute__((no_sanitize_memory))
22#endif
Mark Dickinsonb08a53a2009-04-16 19:52:09 +000023unsigned short _Py_get_387controlword(void) {
24 unsigned short cw;
25 __asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
26 return cw;
27}
28
29void _Py_set_387controlword(unsigned short cw) {
30 __asm__ __volatile__ ("fldcw %0" : : "m" (cw));
31}
32
Mark Dickinsonb08a53a2009-04-16 19:52:09 +000033#endif
34
35
Christian Heimes53876d92008-04-19 00:31:39 +000036#ifndef HAVE_HYPOT
37double hypot(double x, double y)
38{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 double yx;
Christian Heimes53876d92008-04-19 00:31:39 +000040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 x = fabs(x);
42 y = fabs(y);
43 if (x < y) {
44 double temp = x;
45 x = y;
46 y = temp;
47 }
48 if (x == 0.)
49 return 0.;
50 else {
51 yx = y/x;
52 return x*sqrt(1.+yx*yx);
53 }
Christian Heimes53876d92008-04-19 00:31:39 +000054}
55#endif /* HAVE_HYPOT */
56
57#ifndef HAVE_COPYSIGN
Mark Dickinson23b62862009-04-18 14:14:48 +000058double
Christian Heimes53876d92008-04-19 00:31:39 +000059copysign(double x, double y)
60{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 /* use atan2 to distinguish -0. from 0. */
62 if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
63 return fabs(x);
64 } else {
65 return -fabs(x);
66 }
Christian Heimes53876d92008-04-19 00:31:39 +000067}
68#endif /* HAVE_COPYSIGN */
69
Mark Dickinsonf2537862009-04-18 13:58:18 +000070#ifndef HAVE_ROUND
71double
72round(double x)
73{
74 double absx, y;
75 absx = fabs(x);
76 y = floor(absx);
77 if (absx - y >= 0.5)
Yury Selivanov614bfcc2015-06-02 18:53:46 -040078 y += 1.0;
Mark Dickinsonf2537862009-04-18 13:58:18 +000079 return copysign(y, x);
80}
81#endif /* HAVE_ROUND */