| 1 |
import errno |
| 2 |
import math |
| 3 |
import py |
| 4 |
import sys |
| 5 |
|
| 6 |
from rpython.translator import cdir |
| 7 |
from rpython.rlib import jit, rposix, objectmodel |
| 8 |
from rpython.rlib.rfloat import INFINITY, NAN, isfinite, _likely_raise, _error_check_errno_unary_math |
| 9 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 10 |
from rpython.tool.sourcetools import func_with_new_name |
| 11 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 12 |
from rpython.translator.platform import platform |
| 13 |
|
| 14 |
use_library_isinf_isnan = False |
| 15 |
if sys.platform == "win32": |
| 16 |
if platform.name == "msvc": |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
eci = ExternalCompilationInfo( |
| 21 |
includes=['math.h', 'float.h'], |
| 22 |
post_include_bits=['#pragma function(floor)'], |
| 23 |
) |
| 24 |
use_library_isinf_isnan = True |
| 25 |
else: |
| 26 |
eci = ExternalCompilationInfo() |
| 27 |
|
| 28 |
cdir = py.path.local(cdir) |
| 29 |
math_eci = ExternalCompilationInfo( |
| 30 |
include_dirs=[cdir], |
| 31 |
includes=['src/ll_math.h'], |
| 32 |
separate_module_files=[cdir.join('src', 'll_math.c')], |
| 33 |
) |
| 34 |
math_prefix = '_pypy_math_' |
| 35 |
else: |
| 36 |
eci = ExternalCompilationInfo( |
| 37 |
libraries=['m']) |
| 38 |
math_eci = eci |
| 39 |
math_prefix = '' |
| 40 |
|
| 41 |
def llexternal(name, ARGS, RESULT, **kwargs): |
| 42 |
return rffi.llexternal(name, ARGS, RESULT, compilation_info=eci, |
| 43 |
sandboxsafe=True, **kwargs) |
| 44 |
|
| 45 |
def math_llexternal(name, ARGS, RESULT, **kwargs): |
| 46 |
return rffi.llexternal(math_prefix + name, ARGS, RESULT, |
| 47 |
compilation_info=math_eci, |
| 48 |
sandboxsafe=True, **kwargs) |
| 49 |
|
| 50 |
math_fabs = llexternal('fabs', [rffi.DOUBLE], rffi.DOUBLE) |
| 51 |
math_log = llexternal('log', [rffi.DOUBLE], rffi.DOUBLE) |
| 52 |
math_log10 = llexternal('log10', [rffi.DOUBLE], rffi.DOUBLE) |
| 53 |
math_log1p = math_llexternal('log1p', [rffi.DOUBLE], rffi.DOUBLE) |
| 54 |
math_copysign = llexternal('copysign', [rffi.DOUBLE, rffi.DOUBLE], rffi.DOUBLE, |
| 55 |
elidable_function=True) |
| 56 |
math_atan2 = llexternal('atan2', [rffi.DOUBLE, rffi.DOUBLE], rffi.DOUBLE) |
| 57 |
math_frexp = llexternal('frexp', [rffi.DOUBLE, rffi.INT_realP], rffi.DOUBLE) |
| 58 |
math_modf = llexternal('modf', [rffi.DOUBLE, rffi.DOUBLEP], rffi.DOUBLE) |
| 59 |
math_ldexp = llexternal('ldexp', [rffi.DOUBLE, rffi.INT], rffi.DOUBLE, |
| 60 |
save_err=rffi.RFFI_FULL_ERRNO_ZERO) |
| 61 |
math_pow = llexternal('pow', [rffi.DOUBLE, rffi.DOUBLE], rffi.DOUBLE, |
| 62 |
save_err=rffi.RFFI_FULL_ERRNO_ZERO) |
| 63 |
math_fmod = llexternal('fmod', [rffi.DOUBLE, rffi.DOUBLE], rffi.DOUBLE, |
| 64 |
save_err=rffi.RFFI_FULL_ERRNO_ZERO) |
| 65 |
math_hypot = llexternal('hypot', [rffi.DOUBLE, rffi.DOUBLE], rffi.DOUBLE, |
| 66 |
save_err=rffi.RFFI_FULL_ERRNO_ZERO) |
| 67 |
math_floor = llexternal('floor', [rffi.DOUBLE], rffi.DOUBLE, elidable_function=True) |
| 68 |
math_sqrt = llexternal('sqrt', [rffi.DOUBLE], rffi.DOUBLE) |
| 69 |
math_sin = llexternal('sin', [rffi.DOUBLE], rffi.DOUBLE, elidable_function=True) |
| 70 |
math_cos = llexternal('cos', [rffi.DOUBLE], rffi.DOUBLE, elidable_function=True) |
| 71 |
|
| 72 |
@jit.elidable |
| 73 |
def sqrt_nonneg(x): |
| 74 |
return math_sqrt(x) |
| 75 |
sqrt_nonneg.oopspec = "math.sqrt_nonneg(x)" |
| 76 |
|
| 77 |
ERANGE = errno.ERANGE |
| 78 |
EDOM = errno.EDOM |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
VERY_LARGE_FLOAT = 1.0 |
| 85 |
while VERY_LARGE_FLOAT * 100.0 != INFINITY: |
| 86 |
VERY_LARGE_FLOAT *= 64.0 |
| 87 |
|
| 88 |
_lib_isnan = llexternal('_isnan', [lltype.Float], lltype.Signed) |
| 89 |
_lib_finite = llexternal('_finite', [lltype.Float], lltype.Signed) |
| 90 |
|
| 91 |
def ll_math_isnan(y): |
| 92 |
|
| 93 |
|
| 94 |
if use_library_isinf_isnan and not jit.we_are_jitted(): |
| 95 |
return bool(_lib_isnan(y)) |
| 96 |
return y != y |
| 97 |
|
| 98 |
def ll_math_isinf(y): |
| 99 |
if jit.we_are_jitted(): |
| 100 |
return (y + VERY_LARGE_FLOAT) == y |
| 101 |
elif use_library_isinf_isnan: |
| 102 |
return not _lib_finite(y) and not _lib_isnan(y) |
| 103 |
else: |
| 104 |
return y == INFINITY or y == -INFINITY |
| 105 |
|
| 106 |
def ll_math_isfinite(y): |
| 107 |
|
| 108 |
|
| 109 |
if use_library_isinf_isnan and not jit.we_are_jitted(): |
| 110 |
return bool(_lib_finite(y)) |
| 111 |
return (y - y) == 0.0 |
| 112 |
|
| 113 |
|
| 114 |
ll_math_floor = math_floor |
| 115 |
|
| 116 |
ll_math_copysign = math_copysign |
| 117 |
|
| 118 |
|
| 119 |
def ll_math_atan2(y, x): |
| 120 |
"""wrapper for atan2 that deals directly with special cases before |
| 121 |
delegating to the platform libm for the remaining cases. This |
| 122 |
is necessary to get consistent behaviour across platforms. |
| 123 |
Windows, FreeBSD and alpha Tru64 are amongst platforms that don't |
| 124 |
always follow C99. |
| 125 |
""" |
| 126 |
if math.isnan(x): |
| 127 |
return NAN |
| 128 |
|
| 129 |
if not isfinite(y): |
| 130 |
if math.isnan(y): |
| 131 |
return NAN |
| 132 |
if math.isinf(x): |
| 133 |
if math_copysign(1.0, x) == 1.0: |
| 134 |
|
| 135 |
return math_copysign(0.25 * math.pi, y) |
| 136 |
else: |
| 137 |
|
| 138 |
return math_copysign(0.75 * math.pi, y) |
| 139 |
|
| 140 |
return math_copysign(0.5 * math.pi, y) |
| 141 |
|
| 142 |
if math.isinf(x) or y == 0.0: |
| 143 |
if math_copysign(1.0, x) == 1.0: |
| 144 |
|
| 145 |
return math_copysign(0.0, y) |
| 146 |
else: |
| 147 |
|
| 148 |
return math_copysign(math.pi, y) |
| 149 |
|
| 150 |
return math_atan2(y, x) |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
def ll_math_frexp(x): |
| 159 |
|
| 160 |
if not isfinite(x) or not x: |
| 161 |
mantissa = x |
| 162 |
exponent = 0 |
| 163 |
else: |
| 164 |
if objectmodel.revdb_flag_io_disabled(): |
| 165 |
return _revdb_frexp(x) |
| 166 |
exp_p = lltype.malloc(rffi.INT_realP.TO, 1, flavor='raw') |
| 167 |
try: |
| 168 |
mantissa = math_frexp(x, exp_p) |
| 169 |
exponent = rffi.cast(lltype.Signed, exp_p[0]) |
| 170 |
finally: |
| 171 |
lltype.free(exp_p, flavor='raw') |
| 172 |
return (mantissa, exponent) |
| 173 |
|
| 174 |
|
| 175 |
INT_MAX = int(2**31-1) |
| 176 |
INT_MIN = int(-2**31) |
| 177 |
|
| 178 |
def ll_math_ldexp(x, exp): |
| 179 |
if x == 0.0 or not isfinite(x): |
| 180 |
return x |
| 181 |
if exp > INT_MAX: |
| 182 |
|
| 183 |
r = math_copysign(INFINITY, x) |
| 184 |
errno = ERANGE |
| 185 |
elif exp < INT_MIN: |
| 186 |
|
| 187 |
r = math_copysign(0.0, x) |
| 188 |
errno = 0 |
| 189 |
else: |
| 190 |
r = math_ldexp(x, exp) |
| 191 |
errno = rposix.get_saved_errno() |
| 192 |
if math.isinf(r): |
| 193 |
errno = ERANGE |
| 194 |
if errno: |
| 195 |
_likely_raise(errno, r) |
| 196 |
return r |
| 197 |
|
| 198 |
|
| 199 |
def ll_math_modf(x): |
| 200 |
|
| 201 |
|
| 202 |
if objectmodel.revdb_flag_io_disabled(): |
| 203 |
return _revdb_modf(x) |
| 204 |
if not isfinite(x): |
| 205 |
if math.isnan(x): |
| 206 |
return (x, x) |
| 207 |
else: |
| 208 |
return (math_copysign(0.0, x), x) |
| 209 |
intpart_p = lltype.malloc(rffi.DOUBLEP.TO, 1, flavor='raw') |
| 210 |
try: |
| 211 |
fracpart = math_modf(x, intpart_p) |
| 212 |
intpart = intpart_p[0] |
| 213 |
finally: |
| 214 |
lltype.free(intpart_p, flavor='raw') |
| 215 |
return (fracpart, intpart) |
| 216 |
|
| 217 |
|
| 218 |
def ll_math_fmod(x, y): |
| 219 |
|
| 220 |
if math.isinf(y) and isfinite(x): |
| 221 |
return x |
| 222 |
|
| 223 |
r = math_fmod(x, y) |
| 224 |
errno = rposix.get_saved_errno() |
| 225 |
if math.isnan(r): |
| 226 |
if math.isnan(x) or math.isnan(y): |
| 227 |
errno = 0 |
| 228 |
else: |
| 229 |
errno = EDOM |
| 230 |
if errno: |
| 231 |
_likely_raise(errno, r) |
| 232 |
return r |
| 233 |
|
| 234 |
|
| 235 |
def ll_math_hypot(x, y): |
| 236 |
|
| 237 |
if math.isinf(x): |
| 238 |
return math_fabs(x) |
| 239 |
if math.isinf(y): |
| 240 |
return math_fabs(y) |
| 241 |
|
| 242 |
r = math_hypot(x, y) |
| 243 |
errno = rposix.get_saved_errno() |
| 244 |
if not isfinite(r): |
| 245 |
if math.isnan(r): |
| 246 |
if math.isnan(x) or math.isnan(y): |
| 247 |
errno = 0 |
| 248 |
else: |
| 249 |
errno = EDOM |
| 250 |
else: |
| 251 |
if isfinite(x) and isfinite(y): |
| 252 |
errno = ERANGE |
| 253 |
else: |
| 254 |
errno = 0 |
| 255 |
if errno: |
| 256 |
_likely_raise(errno, r) |
| 257 |
return r |
| 258 |
|
| 259 |
|
| 260 |
def ll_math_pow(x, y): |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
if math.isnan(y): |
| 265 |
if x == 1.0: |
| 266 |
return 1.0 |
| 267 |
return y |
| 268 |
|
| 269 |
if not isfinite(x): |
| 270 |
if math.isnan(x): |
| 271 |
if y == 0.0: |
| 272 |
return 1.0 |
| 273 |
return x |
| 274 |
else: |
| 275 |
odd_y = not math.isinf(y) and math_fmod(math_fabs(y), 2.0) == 1.0 |
| 276 |
if y > 0.0: |
| 277 |
if odd_y: |
| 278 |
return x |
| 279 |
return math_fabs(x) |
| 280 |
elif y == 0.0: |
| 281 |
return 1.0 |
| 282 |
else: |
| 283 |
if odd_y: |
| 284 |
return math_copysign(0.0, x) |
| 285 |
return 0.0 |
| 286 |
|
| 287 |
if math.isinf(y): |
| 288 |
if math_fabs(x) == 1.0: |
| 289 |
return 1.0 |
| 290 |
elif y > 0.0 and math_fabs(x) > 1.0: |
| 291 |
return y |
| 292 |
elif y < 0.0 and math_fabs(x) < 1.0: |
| 293 |
if x == 0.0: |
| 294 |
raise ValueError("0**-inf: divide by zero") |
| 295 |
return -y |
| 296 |
else: |
| 297 |
return 0.0 |
| 298 |
|
| 299 |
r = math_pow(x, y) |
| 300 |
errno = rposix.get_saved_errno() |
| 301 |
if not isfinite(r): |
| 302 |
if math.isnan(r): |
| 303 |
|
| 304 |
errno = EDOM |
| 305 |
else: |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
if x == 0.0: |
| 310 |
errno = EDOM |
| 311 |
else: |
| 312 |
errno = ERANGE |
| 313 |
if errno: |
| 314 |
_likely_raise(errno, r) |
| 315 |
return r |
| 316 |
|
| 317 |
def ll_math_sqrt(x): |
| 318 |
if x < 0.0: |
| 319 |
raise ValueError("math domain error") |
| 320 |
|
| 321 |
if isfinite(x): |
| 322 |
return sqrt_nonneg(x) |
| 323 |
|
| 324 |
return x |
| 325 |
|
| 326 |
def ll_math_log(x): |
| 327 |
if x <= 0: |
| 328 |
raise ValueError("math domain error") |
| 329 |
return math_log(x) |
| 330 |
|
| 331 |
def ll_math_log10(x): |
| 332 |
if x <= 0: |
| 333 |
raise ValueError("math domain error") |
| 334 |
return math_log10(x) |
| 335 |
|
| 336 |
def ll_math_log1p(x): |
| 337 |
if x == 0.0: |
| 338 |
return x |
| 339 |
if x <= -1.0: |
| 340 |
if x == -1: |
| 341 |
raise OverflowError("math range error") |
| 342 |
raise ValueError("math domain error") |
| 343 |
return math_log1p(x) |
| 344 |
|
| 345 |
def ll_math_sin(x): |
| 346 |
if math.isinf(x): |
| 347 |
raise ValueError("math domain error") |
| 348 |
return math_sin(x) |
| 349 |
|
| 350 |
def ll_math_cos(x): |
| 351 |
if math.isinf(x): |
| 352 |
raise ValueError("math domain error") |
| 353 |
return math_cos(x) |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
def new_unary_math_function(name, can_overflow, c99): |
| 360 |
if sys.platform == 'win32' and c99: |
| 361 |
c_func = math_llexternal(name, [rffi.DOUBLE], rffi.DOUBLE, |
| 362 |
save_err=rffi.RFFI_FULL_ERRNO_ZERO) |
| 363 |
else: |
| 364 |
c_func = llexternal(name, [rffi.DOUBLE], rffi.DOUBLE, |
| 365 |
save_err=rffi.RFFI_FULL_ERRNO_ZERO) |
| 366 |
|
| 367 |
def ll_math(x): |
| 368 |
r = c_func(x) |
| 369 |
_error_check_errno_unary_math(x, r, can_overflow) |
| 370 |
return r |
| 371 |
|
| 372 |
return func_with_new_name(ll_math, 'll_math_' + name) |
| 373 |
|
| 374 |
|
| 375 |
def _revdb_frexp(x): |
| 376 |
|
| 377 |
from rpython.rlib import revdb |
| 378 |
return revdb.emulate_frexp(x) |
| 379 |
|
| 380 |
def _revdb_modf(x): |
| 381 |
|
| 382 |
from rpython.rlib import revdb |
| 383 |
return revdb.emulate_modf(x) |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
unary_math_functions = [ |
| 389 |
'acos', 'asin', 'atan', |
| 390 |
'ceil', 'cosh', 'exp', 'fabs', |
| 391 |
'sinh', 'tan', 'tanh', |
| 392 |
'acosh', 'asinh', 'atanh', 'expm1', |
| 393 |
] |
| 394 |
unary_math_functions_can_overflow = [ |
| 395 |
'cosh', 'exp', 'sinh', 'expm1', |
| 396 |
] |
| 397 |
unary_math_functions_c99 = [ |
| 398 |
'acosh', 'asinh', 'atanh', 'expm1', |
| 399 |
] |
| 400 |
|
| 401 |
for name in unary_math_functions: |
| 402 |
can_overflow = name in unary_math_functions_can_overflow |
| 403 |
c99 = name in unary_math_functions_c99 |
| 404 |
globals()['ll_math_' + name] = new_unary_math_function(name, can_overflow, |
| 405 |
c99) |