File indexing completed on 2025-01-18 10:06:46
0001 #ifndef Py_INTERNAL_PYMATH_H
0002 #define Py_INTERNAL_PYMATH_H
0003 #ifdef __cplusplus
0004 extern "C" {
0005 #endif
0006
0007 #ifndef Py_BUILD_CORE
0008 # error "this header requires Py_BUILD_CORE define"
0009 #endif
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 static inline void _Py_ADJUST_ERANGE1(double x)
0034 {
0035 if (errno == 0) {
0036 if (x == Py_HUGE_VAL || x == -Py_HUGE_VAL) {
0037 errno = ERANGE;
0038 }
0039 }
0040 else if (errno == ERANGE && x == 0.0) {
0041 errno = 0;
0042 }
0043 }
0044
0045 static inline void _Py_ADJUST_ERANGE2(double x, double y)
0046 {
0047 if (x == Py_HUGE_VAL || x == -Py_HUGE_VAL ||
0048 y == Py_HUGE_VAL || y == -Py_HUGE_VAL)
0049 {
0050 if (errno == 0) {
0051 errno = ERANGE;
0052 }
0053 }
0054 else if (errno == ERANGE) {
0055 errno = 0;
0056 }
0057 }
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 #ifdef HAVE_GCC_ASM_FOR_X87
0087 #define HAVE_PY_SET_53BIT_PRECISION 1
0088
0089
0090 extern unsigned short _Py_get_387controlword(void);
0091 extern void _Py_set_387controlword(unsigned short);
0092
0093 #define _Py_SET_53BIT_PRECISION_HEADER \
0094 unsigned short old_387controlword, new_387controlword
0095 #define _Py_SET_53BIT_PRECISION_START \
0096 do { \
0097 old_387controlword = _Py_get_387controlword(); \
0098 new_387controlword = (old_387controlword & ~0x0f00) | 0x0200; \
0099 if (new_387controlword != old_387controlword) { \
0100 _Py_set_387controlword(new_387controlword); \
0101 } \
0102 } while (0)
0103 #define _Py_SET_53BIT_PRECISION_END \
0104 do { \
0105 if (new_387controlword != old_387controlword) { \
0106 _Py_set_387controlword(old_387controlword); \
0107 } \
0108 } while (0)
0109 #endif
0110
0111
0112
0113 #if defined(_MSC_VER) && !defined(_WIN64) && !defined(_M_ARM)
0114 #define HAVE_PY_SET_53BIT_PRECISION 1
0115
0116 #include <float.h> // __control87_2()
0117
0118 #define _Py_SET_53BIT_PRECISION_HEADER \
0119 unsigned int old_387controlword, new_387controlword, out_387controlword
0120
0121
0122 #define _Py_SET_53BIT_PRECISION_START \
0123 do { \
0124 __control87_2(0, 0, &old_387controlword, NULL); \
0125 new_387controlword = \
0126 (old_387controlword & ~(_MCW_PC | _MCW_RC)) | (_PC_53 | _RC_NEAR); \
0127 if (new_387controlword != old_387controlword) { \
0128 __control87_2(new_387controlword, _MCW_PC | _MCW_RC, \
0129 &out_387controlword, NULL); \
0130 } \
0131 } while (0)
0132 #define _Py_SET_53BIT_PRECISION_END \
0133 do { \
0134 if (new_387controlword != old_387controlword) { \
0135 __control87_2(old_387controlword, _MCW_PC | _MCW_RC, \
0136 &out_387controlword, NULL); \
0137 } \
0138 } while (0)
0139 #endif
0140
0141
0142
0143 #ifdef HAVE_GCC_ASM_FOR_MC68881
0144 #define HAVE_PY_SET_53BIT_PRECISION 1
0145 #define _Py_SET_53BIT_PRECISION_HEADER \
0146 unsigned int old_fpcr, new_fpcr
0147 #define _Py_SET_53BIT_PRECISION_START \
0148 do { \
0149 __asm__ ("fmove.l %%fpcr,%0" : "=g" (old_fpcr)); \
0150 \
0151 new_fpcr = (old_fpcr & ~0xf0) | 0x80; \
0152 if (new_fpcr != old_fpcr) { \
0153 __asm__ volatile ("fmove.l %0,%%fpcr" : : "g" (new_fpcr));\
0154 } \
0155 } while (0)
0156 #define _Py_SET_53BIT_PRECISION_END \
0157 do { \
0158 if (new_fpcr != old_fpcr) { \
0159 __asm__ volatile ("fmove.l %0,%%fpcr" : : "g" (old_fpcr)); \
0160 } \
0161 } while (0)
0162 #endif
0163
0164
0165 #ifndef _Py_SET_53BIT_PRECISION_HEADER
0166 # define _Py_SET_53BIT_PRECISION_HEADER
0167 # define _Py_SET_53BIT_PRECISION_START
0168 # define _Py_SET_53BIT_PRECISION_END
0169 #endif
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184 #if !defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754) && \
0185 !defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) && \
0186 !defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754)
0187 # define _PY_SHORT_FLOAT_REPR 0
0188 #endif
0189
0190
0191
0192
0193 #if defined(X87_DOUBLE_ROUNDING) && !defined(HAVE_PY_SET_53BIT_PRECISION)
0194 # define _PY_SHORT_FLOAT_REPR 0
0195 #endif
0196
0197 #ifndef _PY_SHORT_FLOAT_REPR
0198 # define _PY_SHORT_FLOAT_REPR 1
0199 #endif
0200
0201
0202 #ifdef __cplusplus
0203 }
0204 #endif
0205 #endif