numba-cuda 0.19.1__py3-none-any.whl → 0.20.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of numba-cuda might be problematic. Click here for more details.
- numba_cuda/VERSION +1 -1
- numba_cuda/numba/cuda/__init__.py +1 -1
- numba_cuda/numba/cuda/_internal/cuda_bf16.py +12706 -1470
- numba_cuda/numba/cuda/_internal/cuda_fp16.py +2653 -8769
- numba_cuda/numba/cuda/api.py +6 -1
- numba_cuda/numba/cuda/bf16.py +285 -2
- numba_cuda/numba/cuda/cgutils.py +2 -2
- numba_cuda/numba/cuda/cloudpickle/__init__.py +21 -0
- numba_cuda/numba/cuda/cloudpickle/cloudpickle.py +1598 -0
- numba_cuda/numba/cuda/cloudpickle/cloudpickle_fast.py +17 -0
- numba_cuda/numba/cuda/codegen.py +1 -1
- numba_cuda/numba/cuda/compiler.py +373 -30
- numba_cuda/numba/cuda/core/analysis.py +319 -0
- numba_cuda/numba/cuda/core/annotations/__init__.py +0 -0
- numba_cuda/numba/cuda/core/annotations/type_annotations.py +304 -0
- numba_cuda/numba/cuda/core/base.py +1289 -0
- numba_cuda/numba/cuda/core/bytecode.py +727 -0
- numba_cuda/numba/cuda/core/caching.py +2 -2
- numba_cuda/numba/cuda/core/compiler.py +6 -14
- numba_cuda/numba/cuda/core/compiler_machinery.py +497 -0
- numba_cuda/numba/cuda/core/config.py +747 -0
- numba_cuda/numba/cuda/core/consts.py +124 -0
- numba_cuda/numba/cuda/core/cpu.py +370 -0
- numba_cuda/numba/cuda/core/environment.py +68 -0
- numba_cuda/numba/cuda/core/event.py +511 -0
- numba_cuda/numba/cuda/core/funcdesc.py +330 -0
- numba_cuda/numba/cuda/core/inline_closurecall.py +1889 -0
- numba_cuda/numba/cuda/core/interpreter.py +48 -26
- numba_cuda/numba/cuda/core/ir_utils.py +15 -26
- numba_cuda/numba/cuda/core/options.py +262 -0
- numba_cuda/numba/cuda/core/postproc.py +249 -0
- numba_cuda/numba/cuda/core/pythonapi.py +1868 -0
- numba_cuda/numba/cuda/core/rewrites/__init__.py +26 -0
- numba_cuda/numba/cuda/core/rewrites/ir_print.py +90 -0
- numba_cuda/numba/cuda/core/rewrites/registry.py +104 -0
- numba_cuda/numba/cuda/core/rewrites/static_binop.py +40 -0
- numba_cuda/numba/cuda/core/rewrites/static_getitem.py +187 -0
- numba_cuda/numba/cuda/core/rewrites/static_raise.py +98 -0
- numba_cuda/numba/cuda/core/ssa.py +496 -0
- numba_cuda/numba/cuda/core/targetconfig.py +329 -0
- numba_cuda/numba/cuda/core/tracing.py +231 -0
- numba_cuda/numba/cuda/core/transforms.py +952 -0
- numba_cuda/numba/cuda/core/typed_passes.py +738 -7
- numba_cuda/numba/cuda/core/typeinfer.py +1948 -0
- numba_cuda/numba/cuda/core/unsafe/__init__.py +0 -0
- numba_cuda/numba/cuda/core/unsafe/bytes.py +67 -0
- numba_cuda/numba/cuda/core/unsafe/eh.py +66 -0
- numba_cuda/numba/cuda/core/unsafe/refcount.py +98 -0
- numba_cuda/numba/cuda/core/untyped_passes.py +1983 -0
- numba_cuda/numba/cuda/cpython/cmathimpl.py +560 -0
- numba_cuda/numba/cuda/cpython/mathimpl.py +499 -0
- numba_cuda/numba/cuda/cpython/numbers.py +1474 -0
- numba_cuda/numba/cuda/cuda_paths.py +422 -246
- numba_cuda/numba/cuda/cudadecl.py +1 -1
- numba_cuda/numba/cuda/cudadrv/__init__.py +1 -1
- numba_cuda/numba/cuda/cudadrv/devicearray.py +2 -1
- numba_cuda/numba/cuda/cudadrv/driver.py +11 -140
- numba_cuda/numba/cuda/cudadrv/dummyarray.py +111 -24
- numba_cuda/numba/cuda/cudadrv/libs.py +5 -5
- numba_cuda/numba/cuda/cudadrv/mappings.py +1 -1
- numba_cuda/numba/cuda/cudadrv/nvrtc.py +19 -8
- numba_cuda/numba/cuda/cudadrv/nvvm.py +1 -4
- numba_cuda/numba/cuda/cudadrv/runtime.py +1 -1
- numba_cuda/numba/cuda/cudaimpl.py +5 -1
- numba_cuda/numba/cuda/debuginfo.py +85 -2
- numba_cuda/numba/cuda/decorators.py +3 -3
- numba_cuda/numba/cuda/descriptor.py +3 -4
- numba_cuda/numba/cuda/deviceufunc.py +66 -2
- numba_cuda/numba/cuda/dispatcher.py +18 -39
- numba_cuda/numba/cuda/flags.py +141 -1
- numba_cuda/numba/cuda/fp16.py +0 -2
- numba_cuda/numba/cuda/include/13/cuda_bf16.h +5118 -0
- numba_cuda/numba/cuda/include/13/cuda_bf16.hpp +3865 -0
- numba_cuda/numba/cuda/include/13/cuda_fp16.h +5363 -0
- numba_cuda/numba/cuda/include/13/cuda_fp16.hpp +3483 -0
- numba_cuda/numba/cuda/lowering.py +7 -144
- numba_cuda/numba/cuda/mathimpl.py +2 -1
- numba_cuda/numba/cuda/memory_management/nrt.py +43 -17
- numba_cuda/numba/cuda/misc/findlib.py +75 -0
- numba_cuda/numba/cuda/models.py +9 -1
- numba_cuda/numba/cuda/np/npdatetime_helpers.py +217 -0
- numba_cuda/numba/cuda/np/npyfuncs.py +1807 -0
- numba_cuda/numba/cuda/np/numpy_support.py +553 -0
- numba_cuda/numba/cuda/np/ufunc/ufuncbuilder.py +59 -0
- numba_cuda/numba/cuda/nvvmutils.py +1 -1
- numba_cuda/numba/cuda/printimpl.py +12 -1
- numba_cuda/numba/cuda/random.py +1 -1
- numba_cuda/numba/cuda/serialize.py +1 -1
- numba_cuda/numba/cuda/simulator/__init__.py +1 -1
- numba_cuda/numba/cuda/simulator/api.py +1 -1
- numba_cuda/numba/cuda/simulator/compiler.py +4 -0
- numba_cuda/numba/cuda/simulator/cudadrv/devicearray.py +1 -1
- numba_cuda/numba/cuda/simulator/kernelapi.py +1 -1
- numba_cuda/numba/cuda/simulator/memory_management/nrt.py +14 -2
- numba_cuda/numba/cuda/target.py +35 -17
- numba_cuda/numba/cuda/testing.py +7 -19
- numba_cuda/numba/cuda/tests/__init__.py +1 -1
- numba_cuda/numba/cuda/tests/cloudpickle_main_class.py +9 -0
- numba_cuda/numba/cuda/tests/core/test_serialize.py +4 -4
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_devicerecord.py +1 -1
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_libraries.py +1 -1
- numba_cuda/numba/cuda/tests/cudadrv/test_deallocations.py +1 -1
- numba_cuda/numba/cuda/tests/cudadrv/test_detect.py +6 -3
- numba_cuda/numba/cuda/tests/cudadrv/test_emm_plugins.py +1 -1
- numba_cuda/numba/cuda/tests/cudadrv/test_linker.py +18 -2
- numba_cuda/numba/cuda/tests/cudadrv/test_module_callbacks.py +2 -1
- numba_cuda/numba/cuda/tests/cudadrv/test_nvjitlink.py +1 -1
- numba_cuda/numba/cuda/tests/cudadrv/test_ptds.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/extensions_usecases.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_array.py +2 -1
- numba_cuda/numba/cuda/tests/cudapy/test_atomics.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_bfloat16.py +539 -2
- numba_cuda/numba/cuda/tests/cudapy/test_bfloat16_bindings.py +81 -1
- numba_cuda/numba/cuda/tests/cudapy/test_caching.py +1 -3
- numba_cuda/numba/cuda/tests/cudapy/test_complex.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_constmem.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_cooperative_groups.py +2 -3
- numba_cuda/numba/cuda/tests/cudapy/test_copy_propagate.py +130 -0
- numba_cuda/numba/cuda/tests/cudapy/test_datetime.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_debug.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_debuginfo.py +293 -4
- numba_cuda/numba/cuda/tests/cudapy/test_debuginfo_types.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_dispatcher.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_errors.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_exception.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_extending.py +2 -1
- numba_cuda/numba/cuda/tests/cudapy/test_inline.py +18 -8
- numba_cuda/numba/cuda/tests/cudapy/test_intrinsics.py +23 -21
- numba_cuda/numba/cuda/tests/cudapy/test_ir_utils.py +10 -37
- numba_cuda/numba/cuda/tests/cudapy/test_laplace.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_math.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_matmul.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_operator.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_print.py +20 -0
- numba_cuda/numba/cuda/tests/cudapy/test_record_dtype.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_reduction.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_serialize.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_sm.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_ssa.py +453 -0
- numba_cuda/numba/cuda/tests/cudapy/test_sync.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_typeinfer.py +538 -0
- numba_cuda/numba/cuda/tests/cudapy/test_ufuncs.py +263 -2
- numba_cuda/numba/cuda/tests/cudapy/test_userexc.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_vector_type.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_vectorize_decor.py +112 -6
- numba_cuda/numba/cuda/tests/cudapy/test_warning.py +1 -1
- numba_cuda/numba/cuda/tests/cudapy/test_warp_ops.py +1 -1
- numba_cuda/numba/cuda/tests/doc_examples/test_cg.py +0 -2
- numba_cuda/numba/cuda/tests/doc_examples/test_ffi.py +3 -2
- numba_cuda/numba/cuda/tests/doc_examples/test_laplace.py +0 -2
- numba_cuda/numba/cuda/tests/doc_examples/test_sessionize.py +0 -2
- numba_cuda/numba/cuda/tests/nocuda/test_import.py +3 -1
- numba_cuda/numba/cuda/tests/nocuda/test_library_lookup.py +24 -12
- numba_cuda/numba/cuda/tests/nrt/test_nrt.py +2 -1
- numba_cuda/numba/cuda/tests/support.py +55 -15
- numba_cuda/numba/cuda/tests/test_tracing.py +200 -0
- numba_cuda/numba/cuda/types.py +56 -0
- numba_cuda/numba/cuda/typing/__init__.py +9 -1
- numba_cuda/numba/cuda/typing/cffi_utils.py +55 -0
- numba_cuda/numba/cuda/typing/context.py +751 -0
- numba_cuda/numba/cuda/typing/enumdecl.py +74 -0
- numba_cuda/numba/cuda/typing/npydecl.py +658 -0
- numba_cuda/numba/cuda/typing/templates.py +7 -6
- numba_cuda/numba/cuda/ufuncs.py +3 -3
- numba_cuda/numba/cuda/utils.py +6 -112
- {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/METADATA +4 -3
- {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/RECORD +171 -116
- numba_cuda/numba/cuda/tests/cudadrv/test_mvc.py +0 -60
- {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/WHEEL +0 -0
- {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/licenses/LICENSE +0 -0
- {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/licenses/LICENSE.numba +0 -0
- {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: BSD-2-Clause
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Implement the cmath module functions.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import cmath
|
|
9
|
+
import math
|
|
10
|
+
|
|
11
|
+
from numba.core.imputils import impl_ret_untracked, Registry
|
|
12
|
+
from numba.core import types
|
|
13
|
+
from numba.core.typing import signature
|
|
14
|
+
from numba.cuda.cpython import mathimpl
|
|
15
|
+
from numba.core.extending import overload
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
registry = Registry("cmathimpl")
|
|
19
|
+
lower = registry.lower
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def is_nan(builder, z):
|
|
23
|
+
return builder.fcmp_unordered("uno", z.real, z.imag)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def is_inf(builder, z):
|
|
27
|
+
return builder.or_(
|
|
28
|
+
mathimpl.is_inf(builder, z.real), mathimpl.is_inf(builder, z.imag)
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def is_finite(builder, z):
|
|
33
|
+
return builder.and_(
|
|
34
|
+
mathimpl.is_finite(builder, z.real), mathimpl.is_finite(builder, z.imag)
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@lower(cmath.isnan, types.Complex)
|
|
39
|
+
def isnan_float_impl(context, builder, sig, args):
|
|
40
|
+
[typ] = sig.args
|
|
41
|
+
[value] = args
|
|
42
|
+
z = context.make_complex(builder, typ, value=value)
|
|
43
|
+
res = is_nan(builder, z)
|
|
44
|
+
return impl_ret_untracked(context, builder, sig.return_type, res)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@lower(cmath.isinf, types.Complex)
|
|
48
|
+
def isinf_float_impl(context, builder, sig, args):
|
|
49
|
+
[typ] = sig.args
|
|
50
|
+
[value] = args
|
|
51
|
+
z = context.make_complex(builder, typ, value=value)
|
|
52
|
+
res = is_inf(builder, z)
|
|
53
|
+
return impl_ret_untracked(context, builder, sig.return_type, res)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@lower(cmath.isfinite, types.Complex)
|
|
57
|
+
def isfinite_float_impl(context, builder, sig, args):
|
|
58
|
+
[typ] = sig.args
|
|
59
|
+
[value] = args
|
|
60
|
+
z = context.make_complex(builder, typ, value=value)
|
|
61
|
+
res = is_finite(builder, z)
|
|
62
|
+
return impl_ret_untracked(context, builder, sig.return_type, res)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@overload(cmath.rect, target="cuda")
|
|
66
|
+
def impl_cmath_rect(r, phi):
|
|
67
|
+
if all([isinstance(typ, types.Float) for typ in [r, phi]]):
|
|
68
|
+
|
|
69
|
+
def impl(r, phi):
|
|
70
|
+
if not math.isfinite(phi):
|
|
71
|
+
if not r:
|
|
72
|
+
# cmath.rect(0, phi={inf, nan}) = 0
|
|
73
|
+
return abs(r)
|
|
74
|
+
if math.isinf(r):
|
|
75
|
+
# cmath.rect(inf, phi={inf, nan}) = inf + j phi
|
|
76
|
+
return complex(r, phi)
|
|
77
|
+
real = math.cos(phi)
|
|
78
|
+
imag = math.sin(phi)
|
|
79
|
+
if real == 0.0 and math.isinf(r):
|
|
80
|
+
# 0 * inf would return NaN, we want to keep 0 but xor the sign
|
|
81
|
+
real /= r
|
|
82
|
+
else:
|
|
83
|
+
real *= r
|
|
84
|
+
if imag == 0.0 and math.isinf(r):
|
|
85
|
+
# ditto
|
|
86
|
+
imag /= r
|
|
87
|
+
else:
|
|
88
|
+
imag *= r
|
|
89
|
+
return complex(real, imag)
|
|
90
|
+
|
|
91
|
+
return impl
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def intrinsic_complex_unary(inner_func):
|
|
95
|
+
def wrapper(context, builder, sig, args):
|
|
96
|
+
[typ] = sig.args
|
|
97
|
+
[value] = args
|
|
98
|
+
z = context.make_complex(builder, typ, value=value)
|
|
99
|
+
x = z.real
|
|
100
|
+
y = z.imag
|
|
101
|
+
# Same as above: math.isfinite() is unavailable on 2.x so we precompute
|
|
102
|
+
# its value and pass it to the pure Python implementation.
|
|
103
|
+
x_is_finite = mathimpl.is_finite(builder, x)
|
|
104
|
+
y_is_finite = mathimpl.is_finite(builder, y)
|
|
105
|
+
inner_sig = signature(
|
|
106
|
+
sig.return_type, *(typ.underlying_float,) * 2 + (types.boolean,) * 2
|
|
107
|
+
)
|
|
108
|
+
res = context.compile_internal(
|
|
109
|
+
builder, inner_func, inner_sig, (x, y, x_is_finite, y_is_finite)
|
|
110
|
+
)
|
|
111
|
+
return impl_ret_untracked(context, builder, sig, res)
|
|
112
|
+
|
|
113
|
+
return wrapper
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
NAN = float("nan")
|
|
117
|
+
INF = float("inf")
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
@lower(cmath.exp, types.Complex)
|
|
121
|
+
@intrinsic_complex_unary
|
|
122
|
+
def exp_impl(x, y, x_is_finite, y_is_finite):
|
|
123
|
+
"""cmath.exp(x + y j)"""
|
|
124
|
+
if x_is_finite:
|
|
125
|
+
if y_is_finite:
|
|
126
|
+
c = math.cos(y)
|
|
127
|
+
s = math.sin(y)
|
|
128
|
+
r = math.exp(x)
|
|
129
|
+
return complex(r * c, r * s)
|
|
130
|
+
else:
|
|
131
|
+
return complex(NAN, NAN)
|
|
132
|
+
elif math.isnan(x):
|
|
133
|
+
if y:
|
|
134
|
+
return complex(x, x) # nan + j nan
|
|
135
|
+
else:
|
|
136
|
+
return complex(x, y) # nan + 0j
|
|
137
|
+
elif x > 0.0:
|
|
138
|
+
# x == +inf
|
|
139
|
+
if y_is_finite:
|
|
140
|
+
real = math.cos(y)
|
|
141
|
+
imag = math.sin(y)
|
|
142
|
+
# Avoid NaNs if math.cos(y) or math.sin(y) == 0
|
|
143
|
+
# (e.g. cmath.exp(inf + 0j) == inf + 0j)
|
|
144
|
+
if real != 0:
|
|
145
|
+
real *= x
|
|
146
|
+
if imag != 0:
|
|
147
|
+
imag *= x
|
|
148
|
+
return complex(real, imag)
|
|
149
|
+
else:
|
|
150
|
+
return complex(x, NAN)
|
|
151
|
+
else:
|
|
152
|
+
# x == -inf
|
|
153
|
+
if y_is_finite:
|
|
154
|
+
r = math.exp(x)
|
|
155
|
+
c = math.cos(y)
|
|
156
|
+
s = math.sin(y)
|
|
157
|
+
return complex(r * c, r * s)
|
|
158
|
+
else:
|
|
159
|
+
r = 0
|
|
160
|
+
return complex(r, r)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
@lower(cmath.log, types.Complex)
|
|
164
|
+
@intrinsic_complex_unary
|
|
165
|
+
def log_impl(x, y, x_is_finite, y_is_finite):
|
|
166
|
+
"""cmath.log(x + y j)"""
|
|
167
|
+
a = math.log(math.hypot(x, y))
|
|
168
|
+
b = math.atan2(y, x)
|
|
169
|
+
return complex(a, b)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@lower(cmath.log, types.Complex, types.Complex)
|
|
173
|
+
def log_base_impl(context, builder, sig, args):
|
|
174
|
+
"""cmath.log(z, base)"""
|
|
175
|
+
[z, base] = args
|
|
176
|
+
|
|
177
|
+
def log_base(z, base):
|
|
178
|
+
return cmath.log(z) / cmath.log(base)
|
|
179
|
+
|
|
180
|
+
res = context.compile_internal(builder, log_base, sig, args)
|
|
181
|
+
return impl_ret_untracked(context, builder, sig, res)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
@overload(cmath.log10, target="cuda")
|
|
185
|
+
def impl_cmath_log10(z):
|
|
186
|
+
if not isinstance(z, types.Complex):
|
|
187
|
+
return
|
|
188
|
+
|
|
189
|
+
LN_10 = 2.302585092994045684
|
|
190
|
+
|
|
191
|
+
def log10_impl(z):
|
|
192
|
+
"""cmath.log10(z)"""
|
|
193
|
+
z = cmath.log(z)
|
|
194
|
+
# This formula gives better results on +/-inf than cmath.log(z, 10)
|
|
195
|
+
# See http://bugs.python.org/issue22544
|
|
196
|
+
return complex(z.real / LN_10, z.imag / LN_10)
|
|
197
|
+
|
|
198
|
+
return log10_impl
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@overload(cmath.phase, target="cuda")
|
|
202
|
+
def phase_impl(x):
|
|
203
|
+
"""cmath.phase(x + y j)"""
|
|
204
|
+
|
|
205
|
+
if not isinstance(x, types.Complex):
|
|
206
|
+
return
|
|
207
|
+
|
|
208
|
+
def impl(x):
|
|
209
|
+
return math.atan2(x.imag, x.real)
|
|
210
|
+
|
|
211
|
+
return impl
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@overload(cmath.polar, target="cuda")
|
|
215
|
+
def polar_impl(x):
|
|
216
|
+
if not isinstance(x, types.Complex):
|
|
217
|
+
return
|
|
218
|
+
|
|
219
|
+
def impl(x):
|
|
220
|
+
r, i = x.real, x.imag
|
|
221
|
+
return math.hypot(r, i), math.atan2(i, r)
|
|
222
|
+
|
|
223
|
+
return impl
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
@lower(cmath.sqrt, types.Complex)
|
|
227
|
+
def sqrt_impl(context, builder, sig, args):
|
|
228
|
+
# We risk spurious overflow for components >= FLT_MAX / (1 + sqrt(2)).
|
|
229
|
+
|
|
230
|
+
SQRT2 = 1.414213562373095048801688724209698079e0
|
|
231
|
+
ONE_PLUS_SQRT2 = 1.0 + SQRT2
|
|
232
|
+
theargflt = sig.args[0].underlying_float
|
|
233
|
+
# Get a type specific maximum value so scaling for overflow is based on that
|
|
234
|
+
MAX = mathimpl.DBL_MAX if theargflt.bitwidth == 64 else mathimpl.FLT_MAX
|
|
235
|
+
# THRES will be double precision, should not impact typing as it's just
|
|
236
|
+
# used for comparison, there *may* be a few values near THRES which
|
|
237
|
+
# deviate from e.g. NumPy due to rounding that occurs in the computation
|
|
238
|
+
# of this value in the case of a 32bit argument.
|
|
239
|
+
THRES = MAX / ONE_PLUS_SQRT2
|
|
240
|
+
|
|
241
|
+
def sqrt_impl(z):
|
|
242
|
+
"""cmath.sqrt(z)"""
|
|
243
|
+
# This is NumPy's algorithm, see npy_csqrt() in npy_math_complex.c.src
|
|
244
|
+
a = z.real
|
|
245
|
+
b = z.imag
|
|
246
|
+
if a == 0.0 and b == 0.0:
|
|
247
|
+
return complex(abs(b), b)
|
|
248
|
+
if math.isinf(b):
|
|
249
|
+
return complex(abs(b), b)
|
|
250
|
+
if math.isnan(a):
|
|
251
|
+
return complex(a, a)
|
|
252
|
+
if math.isinf(a):
|
|
253
|
+
if a < 0.0:
|
|
254
|
+
return complex(abs(b - b), math.copysign(a, b))
|
|
255
|
+
else:
|
|
256
|
+
return complex(a, math.copysign(b - b, b))
|
|
257
|
+
|
|
258
|
+
# The remaining special case (b is NaN) is handled just fine by
|
|
259
|
+
# the normal code path below.
|
|
260
|
+
|
|
261
|
+
# Scale to avoid overflow
|
|
262
|
+
if abs(a) >= THRES or abs(b) >= THRES:
|
|
263
|
+
a *= 0.25
|
|
264
|
+
b *= 0.25
|
|
265
|
+
scale = True
|
|
266
|
+
else:
|
|
267
|
+
scale = False
|
|
268
|
+
# Algorithm 312, CACM vol 10, Oct 1967
|
|
269
|
+
if a >= 0:
|
|
270
|
+
t = math.sqrt((a + math.hypot(a, b)) * 0.5)
|
|
271
|
+
real = t
|
|
272
|
+
imag = b / (2 * t)
|
|
273
|
+
else:
|
|
274
|
+
t = math.sqrt((-a + math.hypot(a, b)) * 0.5)
|
|
275
|
+
real = abs(b) / (2 * t)
|
|
276
|
+
imag = math.copysign(t, b)
|
|
277
|
+
# Rescale
|
|
278
|
+
if scale:
|
|
279
|
+
return complex(real * 2, imag)
|
|
280
|
+
else:
|
|
281
|
+
return complex(real, imag)
|
|
282
|
+
|
|
283
|
+
res = context.compile_internal(builder, sqrt_impl, sig, args)
|
|
284
|
+
return impl_ret_untracked(context, builder, sig, res)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
@lower(cmath.cos, types.Complex)
|
|
288
|
+
def cos_impl(context, builder, sig, args):
|
|
289
|
+
def cos_impl(z):
|
|
290
|
+
"""cmath.cos(z) = cmath.cosh(z j)"""
|
|
291
|
+
return cmath.cosh(complex(-z.imag, z.real))
|
|
292
|
+
|
|
293
|
+
res = context.compile_internal(builder, cos_impl, sig, args)
|
|
294
|
+
return impl_ret_untracked(context, builder, sig, res)
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
@overload(cmath.cosh, target="cuda")
|
|
298
|
+
def impl_cmath_cosh(z):
|
|
299
|
+
if not isinstance(z, types.Complex):
|
|
300
|
+
return
|
|
301
|
+
|
|
302
|
+
def cosh_impl(z):
|
|
303
|
+
"""cmath.cosh(z)"""
|
|
304
|
+
x = z.real
|
|
305
|
+
y = z.imag
|
|
306
|
+
if math.isinf(x):
|
|
307
|
+
if math.isnan(y):
|
|
308
|
+
# x = +inf, y = NaN => cmath.cosh(x + y j) = inf + Nan * j
|
|
309
|
+
real = abs(x)
|
|
310
|
+
imag = y
|
|
311
|
+
elif y == 0.0:
|
|
312
|
+
# x = +inf, y = 0 => cmath.cosh(x + y j) = inf + 0j
|
|
313
|
+
real = abs(x)
|
|
314
|
+
imag = y
|
|
315
|
+
else:
|
|
316
|
+
real = math.copysign(x, math.cos(y))
|
|
317
|
+
imag = math.copysign(x, math.sin(y))
|
|
318
|
+
if x < 0.0:
|
|
319
|
+
# x = -inf => negate imaginary part of result
|
|
320
|
+
imag = -imag
|
|
321
|
+
return complex(real, imag)
|
|
322
|
+
return complex(math.cos(y) * math.cosh(x), math.sin(y) * math.sinh(x))
|
|
323
|
+
|
|
324
|
+
return cosh_impl
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
@lower(cmath.sin, types.Complex)
|
|
328
|
+
def sin_impl(context, builder, sig, args):
|
|
329
|
+
def sin_impl(z):
|
|
330
|
+
"""cmath.sin(z) = -j * cmath.sinh(z j)"""
|
|
331
|
+
r = cmath.sinh(complex(-z.imag, z.real))
|
|
332
|
+
return complex(r.imag, -r.real)
|
|
333
|
+
|
|
334
|
+
res = context.compile_internal(builder, sin_impl, sig, args)
|
|
335
|
+
return impl_ret_untracked(context, builder, sig, res)
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
@overload(cmath.sinh, target="cuda")
|
|
339
|
+
def impl_cmath_sinh(z):
|
|
340
|
+
if not isinstance(z, types.Complex):
|
|
341
|
+
return
|
|
342
|
+
|
|
343
|
+
def sinh_impl(z):
|
|
344
|
+
"""cmath.sinh(z)"""
|
|
345
|
+
x = z.real
|
|
346
|
+
y = z.imag
|
|
347
|
+
if math.isinf(x):
|
|
348
|
+
if math.isnan(y):
|
|
349
|
+
# x = +/-inf, y = NaN => cmath.sinh(x + y j) = x + NaN * j
|
|
350
|
+
real = x
|
|
351
|
+
imag = y
|
|
352
|
+
else:
|
|
353
|
+
real = math.cos(y)
|
|
354
|
+
imag = math.sin(y)
|
|
355
|
+
if real != 0.0:
|
|
356
|
+
real *= x
|
|
357
|
+
if imag != 0.0:
|
|
358
|
+
imag *= abs(x)
|
|
359
|
+
return complex(real, imag)
|
|
360
|
+
return complex(math.cos(y) * math.sinh(x), math.sin(y) * math.cosh(x))
|
|
361
|
+
|
|
362
|
+
return sinh_impl
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
@lower(cmath.tan, types.Complex)
|
|
366
|
+
def tan_impl(context, builder, sig, args):
|
|
367
|
+
def tan_impl(z):
|
|
368
|
+
"""cmath.tan(z) = -j * cmath.tanh(z j)"""
|
|
369
|
+
r = cmath.tanh(complex(-z.imag, z.real))
|
|
370
|
+
return complex(r.imag, -r.real)
|
|
371
|
+
|
|
372
|
+
res = context.compile_internal(builder, tan_impl, sig, args)
|
|
373
|
+
return impl_ret_untracked(context, builder, sig, res)
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
@overload(cmath.tanh, target="cuda")
|
|
377
|
+
def impl_cmath_tanh(z):
|
|
378
|
+
if not isinstance(z, types.Complex):
|
|
379
|
+
return
|
|
380
|
+
|
|
381
|
+
def tanh_impl(z):
|
|
382
|
+
"""cmath.tanh(z)"""
|
|
383
|
+
x = z.real
|
|
384
|
+
y = z.imag
|
|
385
|
+
if math.isinf(x):
|
|
386
|
+
real = math.copysign(1.0, x)
|
|
387
|
+
if math.isinf(y):
|
|
388
|
+
imag = 0.0
|
|
389
|
+
else:
|
|
390
|
+
imag = math.copysign(0.0, math.sin(2.0 * y))
|
|
391
|
+
return complex(real, imag)
|
|
392
|
+
# This is CPython's algorithm (see c_tanh() in cmathmodule.c).
|
|
393
|
+
# XXX how to force float constants into single precision?
|
|
394
|
+
tx = math.tanh(x)
|
|
395
|
+
ty = math.tan(y)
|
|
396
|
+
cx = 1.0 / math.cosh(x)
|
|
397
|
+
txty = tx * ty
|
|
398
|
+
denom = 1.0 + txty * txty
|
|
399
|
+
return complex(tx * (1.0 + ty * ty) / denom, ((ty / denom) * cx) * cx)
|
|
400
|
+
|
|
401
|
+
return tanh_impl
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
@lower(cmath.acos, types.Complex)
|
|
405
|
+
def acos_impl(context, builder, sig, args):
|
|
406
|
+
LN_4 = math.log(4)
|
|
407
|
+
THRES = mathimpl.FLT_MAX / 4
|
|
408
|
+
|
|
409
|
+
def acos_impl(z):
|
|
410
|
+
"""cmath.acos(z)"""
|
|
411
|
+
# CPython's algorithm (see c_acos() in cmathmodule.c)
|
|
412
|
+
if abs(z.real) > THRES or abs(z.imag) > THRES:
|
|
413
|
+
# Avoid unnecessary overflow for large arguments
|
|
414
|
+
# (also handles infinities gracefully)
|
|
415
|
+
real = math.atan2(abs(z.imag), z.real)
|
|
416
|
+
imag = math.copysign(
|
|
417
|
+
math.log(math.hypot(z.real * 0.5, z.imag * 0.5)) + LN_4, -z.imag
|
|
418
|
+
)
|
|
419
|
+
return complex(real, imag)
|
|
420
|
+
else:
|
|
421
|
+
s1 = cmath.sqrt(complex(1.0 - z.real, -z.imag))
|
|
422
|
+
s2 = cmath.sqrt(complex(1.0 + z.real, z.imag))
|
|
423
|
+
real = 2.0 * math.atan2(s1.real, s2.real)
|
|
424
|
+
imag = math.asinh(s2.real * s1.imag - s2.imag * s1.real)
|
|
425
|
+
return complex(real, imag)
|
|
426
|
+
|
|
427
|
+
res = context.compile_internal(builder, acos_impl, sig, args)
|
|
428
|
+
return impl_ret_untracked(context, builder, sig, res)
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
@overload(cmath.acosh, target="cuda")
|
|
432
|
+
def impl_cmath_acosh(z):
|
|
433
|
+
if not isinstance(z, types.Complex):
|
|
434
|
+
return
|
|
435
|
+
|
|
436
|
+
LN_4 = math.log(4)
|
|
437
|
+
THRES = mathimpl.FLT_MAX / 4
|
|
438
|
+
|
|
439
|
+
def acosh_impl(z):
|
|
440
|
+
"""cmath.acosh(z)"""
|
|
441
|
+
# CPython's algorithm (see c_acosh() in cmathmodule.c)
|
|
442
|
+
if abs(z.real) > THRES or abs(z.imag) > THRES:
|
|
443
|
+
# Avoid unnecessary overflow for large arguments
|
|
444
|
+
# (also handles infinities gracefully)
|
|
445
|
+
real = math.log(math.hypot(z.real * 0.5, z.imag * 0.5)) + LN_4
|
|
446
|
+
imag = math.atan2(z.imag, z.real)
|
|
447
|
+
return complex(real, imag)
|
|
448
|
+
else:
|
|
449
|
+
s1 = cmath.sqrt(complex(z.real - 1.0, z.imag))
|
|
450
|
+
s2 = cmath.sqrt(complex(z.real + 1.0, z.imag))
|
|
451
|
+
real = math.asinh(s1.real * s2.real + s1.imag * s2.imag)
|
|
452
|
+
imag = 2.0 * math.atan2(s1.imag, s2.real)
|
|
453
|
+
return complex(real, imag)
|
|
454
|
+
# Condensed formula (NumPy)
|
|
455
|
+
# return cmath.log(z + cmath.sqrt(z + 1.) * cmath.sqrt(z - 1.))
|
|
456
|
+
|
|
457
|
+
return acosh_impl
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
@lower(cmath.asinh, types.Complex)
|
|
461
|
+
def asinh_impl(context, builder, sig, args):
|
|
462
|
+
LN_4 = math.log(4)
|
|
463
|
+
THRES = mathimpl.FLT_MAX / 4
|
|
464
|
+
|
|
465
|
+
def asinh_impl(z):
|
|
466
|
+
"""cmath.asinh(z)"""
|
|
467
|
+
# CPython's algorithm (see c_asinh() in cmathmodule.c)
|
|
468
|
+
if abs(z.real) > THRES or abs(z.imag) > THRES:
|
|
469
|
+
real = math.copysign(
|
|
470
|
+
math.log(math.hypot(z.real * 0.5, z.imag * 0.5)) + LN_4, z.real
|
|
471
|
+
)
|
|
472
|
+
imag = math.atan2(z.imag, abs(z.real))
|
|
473
|
+
return complex(real, imag)
|
|
474
|
+
else:
|
|
475
|
+
s1 = cmath.sqrt(complex(1.0 + z.imag, -z.real))
|
|
476
|
+
s2 = cmath.sqrt(complex(1.0 - z.imag, z.real))
|
|
477
|
+
real = math.asinh(s1.real * s2.imag - s2.real * s1.imag)
|
|
478
|
+
imag = math.atan2(z.imag, s1.real * s2.real - s1.imag * s2.imag)
|
|
479
|
+
return complex(real, imag)
|
|
480
|
+
|
|
481
|
+
res = context.compile_internal(builder, asinh_impl, sig, args)
|
|
482
|
+
return impl_ret_untracked(context, builder, sig, res)
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
@lower(cmath.asin, types.Complex)
|
|
486
|
+
def asin_impl(context, builder, sig, args):
|
|
487
|
+
def asin_impl(z):
|
|
488
|
+
"""cmath.asin(z) = -j * cmath.asinh(z j)"""
|
|
489
|
+
r = cmath.asinh(complex(-z.imag, z.real))
|
|
490
|
+
return complex(r.imag, -r.real)
|
|
491
|
+
|
|
492
|
+
res = context.compile_internal(builder, asin_impl, sig, args)
|
|
493
|
+
return impl_ret_untracked(context, builder, sig, res)
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
@lower(cmath.atan, types.Complex)
|
|
497
|
+
def atan_impl(context, builder, sig, args):
|
|
498
|
+
def atan_impl(z):
|
|
499
|
+
"""cmath.atan(z) = -j * cmath.atanh(z j)"""
|
|
500
|
+
r = cmath.atanh(complex(-z.imag, z.real))
|
|
501
|
+
if math.isinf(z.real) and math.isnan(z.imag):
|
|
502
|
+
# XXX this is odd but necessary
|
|
503
|
+
return complex(r.imag, r.real)
|
|
504
|
+
else:
|
|
505
|
+
return complex(r.imag, -r.real)
|
|
506
|
+
|
|
507
|
+
res = context.compile_internal(builder, atan_impl, sig, args)
|
|
508
|
+
return impl_ret_untracked(context, builder, sig, res)
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
@lower(cmath.atanh, types.Complex)
|
|
512
|
+
def atanh_impl(context, builder, sig, args):
|
|
513
|
+
THRES_LARGE = math.sqrt(mathimpl.FLT_MAX / 4)
|
|
514
|
+
THRES_SMALL = math.sqrt(mathimpl.FLT_MIN)
|
|
515
|
+
PI_12 = math.pi / 2
|
|
516
|
+
|
|
517
|
+
def atanh_impl(z):
|
|
518
|
+
"""cmath.atanh(z)"""
|
|
519
|
+
# CPython's algorithm (see c_atanh() in cmathmodule.c)
|
|
520
|
+
if z.real < 0.0:
|
|
521
|
+
# Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z).
|
|
522
|
+
negate = True
|
|
523
|
+
z = -z
|
|
524
|
+
else:
|
|
525
|
+
negate = False
|
|
526
|
+
|
|
527
|
+
ay = abs(z.imag)
|
|
528
|
+
if math.isnan(z.real) or z.real > THRES_LARGE or ay > THRES_LARGE:
|
|
529
|
+
if math.isinf(z.imag):
|
|
530
|
+
real = math.copysign(0.0, z.real)
|
|
531
|
+
elif math.isinf(z.real):
|
|
532
|
+
real = 0.0
|
|
533
|
+
else:
|
|
534
|
+
# may be safe from overflow, depending on hypot's implementation...
|
|
535
|
+
h = math.hypot(z.real * 0.5, z.imag * 0.5)
|
|
536
|
+
real = z.real / 4.0 / h / h
|
|
537
|
+
imag = -math.copysign(PI_12, -z.imag)
|
|
538
|
+
elif z.real == 1.0 and ay < THRES_SMALL:
|
|
539
|
+
# C99 standard says: atanh(1+/-0.) should be inf +/- 0j
|
|
540
|
+
if ay == 0.0:
|
|
541
|
+
real = INF
|
|
542
|
+
imag = z.imag
|
|
543
|
+
else:
|
|
544
|
+
real = -math.log(math.sqrt(ay) / math.sqrt(math.hypot(ay, 2.0)))
|
|
545
|
+
imag = math.copysign(math.atan2(2.0, -ay) / 2, z.imag)
|
|
546
|
+
else:
|
|
547
|
+
sqay = ay * ay
|
|
548
|
+
zr1 = 1 - z.real
|
|
549
|
+
real = math.log1p(4.0 * z.real / (zr1 * zr1 + sqay)) * 0.25
|
|
550
|
+
imag = -math.atan2(-2.0 * z.imag, zr1 * (1 + z.real) - sqay) * 0.5
|
|
551
|
+
|
|
552
|
+
if math.isnan(z.imag):
|
|
553
|
+
imag = NAN
|
|
554
|
+
if negate:
|
|
555
|
+
return complex(-real, -imag)
|
|
556
|
+
else:
|
|
557
|
+
return complex(real, imag)
|
|
558
|
+
|
|
559
|
+
res = context.compile_internal(builder, atanh_impl, sig, args)
|
|
560
|
+
return impl_ret_untracked(context, builder, sig, res)
|