numba-cuda 0.21.1__cp313-cp313-win_amd64.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.
Files changed (488) hide show
  1. _numba_cuda_redirector.pth +4 -0
  2. _numba_cuda_redirector.py +89 -0
  3. numba_cuda/VERSION +1 -0
  4. numba_cuda/__init__.py +6 -0
  5. numba_cuda/_version.py +11 -0
  6. numba_cuda/numba/cuda/__init__.py +70 -0
  7. numba_cuda/numba/cuda/_internal/cuda_bf16.py +16394 -0
  8. numba_cuda/numba/cuda/_internal/cuda_fp16.py +8112 -0
  9. numba_cuda/numba/cuda/api.py +577 -0
  10. numba_cuda/numba/cuda/api_util.py +76 -0
  11. numba_cuda/numba/cuda/args.py +72 -0
  12. numba_cuda/numba/cuda/bf16.py +397 -0
  13. numba_cuda/numba/cuda/cache_hints.py +287 -0
  14. numba_cuda/numba/cuda/cext/__init__.py +2 -0
  15. numba_cuda/numba/cuda/cext/_devicearray.cp313-win_amd64.pyd +0 -0
  16. numba_cuda/numba/cuda/cext/_devicearray.cpp +159 -0
  17. numba_cuda/numba/cuda/cext/_devicearray.h +29 -0
  18. numba_cuda/numba/cuda/cext/_dispatcher.cp313-win_amd64.pyd +0 -0
  19. numba_cuda/numba/cuda/cext/_dispatcher.cpp +1098 -0
  20. numba_cuda/numba/cuda/cext/_hashtable.cpp +532 -0
  21. numba_cuda/numba/cuda/cext/_hashtable.h +135 -0
  22. numba_cuda/numba/cuda/cext/_helperlib.c +71 -0
  23. numba_cuda/numba/cuda/cext/_helperlib.cp313-win_amd64.pyd +0 -0
  24. numba_cuda/numba/cuda/cext/_helpermod.c +82 -0
  25. numba_cuda/numba/cuda/cext/_pymodule.h +38 -0
  26. numba_cuda/numba/cuda/cext/_typeconv.cp313-win_amd64.pyd +0 -0
  27. numba_cuda/numba/cuda/cext/_typeconv.cpp +206 -0
  28. numba_cuda/numba/cuda/cext/_typeof.cpp +1159 -0
  29. numba_cuda/numba/cuda/cext/_typeof.h +19 -0
  30. numba_cuda/numba/cuda/cext/capsulethunk.h +111 -0
  31. numba_cuda/numba/cuda/cext/mviewbuf.c +385 -0
  32. numba_cuda/numba/cuda/cext/mviewbuf.cp313-win_amd64.pyd +0 -0
  33. numba_cuda/numba/cuda/cext/typeconv.cpp +212 -0
  34. numba_cuda/numba/cuda/cext/typeconv.hpp +101 -0
  35. numba_cuda/numba/cuda/cg.py +67 -0
  36. numba_cuda/numba/cuda/cgutils.py +1294 -0
  37. numba_cuda/numba/cuda/cloudpickle/__init__.py +21 -0
  38. numba_cuda/numba/cuda/cloudpickle/cloudpickle.py +1598 -0
  39. numba_cuda/numba/cuda/cloudpickle/cloudpickle_fast.py +17 -0
  40. numba_cuda/numba/cuda/codegen.py +541 -0
  41. numba_cuda/numba/cuda/compiler.py +1396 -0
  42. numba_cuda/numba/cuda/core/analysis.py +758 -0
  43. numba_cuda/numba/cuda/core/annotations/__init__.py +0 -0
  44. numba_cuda/numba/cuda/core/annotations/pretty_annotate.py +288 -0
  45. numba_cuda/numba/cuda/core/annotations/type_annotations.py +305 -0
  46. numba_cuda/numba/cuda/core/base.py +1332 -0
  47. numba_cuda/numba/cuda/core/boxing.py +1411 -0
  48. numba_cuda/numba/cuda/core/bytecode.py +728 -0
  49. numba_cuda/numba/cuda/core/byteflow.py +2346 -0
  50. numba_cuda/numba/cuda/core/caching.py +744 -0
  51. numba_cuda/numba/cuda/core/callconv.py +392 -0
  52. numba_cuda/numba/cuda/core/codegen.py +171 -0
  53. numba_cuda/numba/cuda/core/compiler.py +199 -0
  54. numba_cuda/numba/cuda/core/compiler_lock.py +85 -0
  55. numba_cuda/numba/cuda/core/compiler_machinery.py +497 -0
  56. numba_cuda/numba/cuda/core/config.py +650 -0
  57. numba_cuda/numba/cuda/core/consts.py +124 -0
  58. numba_cuda/numba/cuda/core/controlflow.py +989 -0
  59. numba_cuda/numba/cuda/core/entrypoints.py +57 -0
  60. numba_cuda/numba/cuda/core/environment.py +66 -0
  61. numba_cuda/numba/cuda/core/errors.py +917 -0
  62. numba_cuda/numba/cuda/core/event.py +511 -0
  63. numba_cuda/numba/cuda/core/funcdesc.py +330 -0
  64. numba_cuda/numba/cuda/core/generators.py +387 -0
  65. numba_cuda/numba/cuda/core/imputils.py +509 -0
  66. numba_cuda/numba/cuda/core/inline_closurecall.py +1787 -0
  67. numba_cuda/numba/cuda/core/interpreter.py +3617 -0
  68. numba_cuda/numba/cuda/core/ir.py +1812 -0
  69. numba_cuda/numba/cuda/core/ir_utils.py +2638 -0
  70. numba_cuda/numba/cuda/core/optional.py +129 -0
  71. numba_cuda/numba/cuda/core/options.py +262 -0
  72. numba_cuda/numba/cuda/core/postproc.py +249 -0
  73. numba_cuda/numba/cuda/core/pythonapi.py +1859 -0
  74. numba_cuda/numba/cuda/core/registry.py +46 -0
  75. numba_cuda/numba/cuda/core/removerefctpass.py +123 -0
  76. numba_cuda/numba/cuda/core/rewrites/__init__.py +26 -0
  77. numba_cuda/numba/cuda/core/rewrites/ir_print.py +91 -0
  78. numba_cuda/numba/cuda/core/rewrites/registry.py +104 -0
  79. numba_cuda/numba/cuda/core/rewrites/static_binop.py +41 -0
  80. numba_cuda/numba/cuda/core/rewrites/static_getitem.py +189 -0
  81. numba_cuda/numba/cuda/core/rewrites/static_raise.py +100 -0
  82. numba_cuda/numba/cuda/core/sigutils.py +68 -0
  83. numba_cuda/numba/cuda/core/ssa.py +498 -0
  84. numba_cuda/numba/cuda/core/targetconfig.py +330 -0
  85. numba_cuda/numba/cuda/core/tracing.py +231 -0
  86. numba_cuda/numba/cuda/core/transforms.py +956 -0
  87. numba_cuda/numba/cuda/core/typed_passes.py +867 -0
  88. numba_cuda/numba/cuda/core/typeinfer.py +1950 -0
  89. numba_cuda/numba/cuda/core/unsafe/__init__.py +0 -0
  90. numba_cuda/numba/cuda/core/unsafe/bytes.py +67 -0
  91. numba_cuda/numba/cuda/core/unsafe/eh.py +67 -0
  92. numba_cuda/numba/cuda/core/unsafe/refcount.py +98 -0
  93. numba_cuda/numba/cuda/core/untyped_passes.py +1979 -0
  94. numba_cuda/numba/cuda/cpython/builtins.py +1153 -0
  95. numba_cuda/numba/cuda/cpython/charseq.py +1218 -0
  96. numba_cuda/numba/cuda/cpython/cmathimpl.py +560 -0
  97. numba_cuda/numba/cuda/cpython/enumimpl.py +103 -0
  98. numba_cuda/numba/cuda/cpython/iterators.py +167 -0
  99. numba_cuda/numba/cuda/cpython/listobj.py +1326 -0
  100. numba_cuda/numba/cuda/cpython/mathimpl.py +499 -0
  101. numba_cuda/numba/cuda/cpython/numbers.py +1475 -0
  102. numba_cuda/numba/cuda/cpython/rangeobj.py +289 -0
  103. numba_cuda/numba/cuda/cpython/slicing.py +322 -0
  104. numba_cuda/numba/cuda/cpython/tupleobj.py +456 -0
  105. numba_cuda/numba/cuda/cpython/unicode.py +2865 -0
  106. numba_cuda/numba/cuda/cpython/unicode_support.py +1597 -0
  107. numba_cuda/numba/cuda/cpython/unsafe/__init__.py +0 -0
  108. numba_cuda/numba/cuda/cpython/unsafe/numbers.py +64 -0
  109. numba_cuda/numba/cuda/cpython/unsafe/tuple.py +92 -0
  110. numba_cuda/numba/cuda/cuda_paths.py +691 -0
  111. numba_cuda/numba/cuda/cudadecl.py +556 -0
  112. numba_cuda/numba/cuda/cudadrv/__init__.py +14 -0
  113. numba_cuda/numba/cuda/cudadrv/devicearray.py +951 -0
  114. numba_cuda/numba/cuda/cudadrv/devices.py +249 -0
  115. numba_cuda/numba/cuda/cudadrv/driver.py +3222 -0
  116. numba_cuda/numba/cuda/cudadrv/drvapi.py +435 -0
  117. numba_cuda/numba/cuda/cudadrv/dummyarray.py +558 -0
  118. numba_cuda/numba/cuda/cudadrv/enums.py +613 -0
  119. numba_cuda/numba/cuda/cudadrv/error.py +48 -0
  120. numba_cuda/numba/cuda/cudadrv/libs.py +220 -0
  121. numba_cuda/numba/cuda/cudadrv/linkable_code.py +184 -0
  122. numba_cuda/numba/cuda/cudadrv/mappings.py +14 -0
  123. numba_cuda/numba/cuda/cudadrv/ndarray.py +26 -0
  124. numba_cuda/numba/cuda/cudadrv/nvrtc.py +193 -0
  125. numba_cuda/numba/cuda/cudadrv/nvvm.py +756 -0
  126. numba_cuda/numba/cuda/cudadrv/rtapi.py +13 -0
  127. numba_cuda/numba/cuda/cudadrv/runtime.py +34 -0
  128. numba_cuda/numba/cuda/cudaimpl.py +995 -0
  129. numba_cuda/numba/cuda/cudamath.py +149 -0
  130. numba_cuda/numba/cuda/datamodel/__init__.py +7 -0
  131. numba_cuda/numba/cuda/datamodel/cuda_manager.py +66 -0
  132. numba_cuda/numba/cuda/datamodel/cuda_models.py +1446 -0
  133. numba_cuda/numba/cuda/datamodel/cuda_packer.py +224 -0
  134. numba_cuda/numba/cuda/datamodel/cuda_registry.py +22 -0
  135. numba_cuda/numba/cuda/datamodel/cuda_testing.py +153 -0
  136. numba_cuda/numba/cuda/datamodel/manager.py +11 -0
  137. numba_cuda/numba/cuda/datamodel/models.py +9 -0
  138. numba_cuda/numba/cuda/datamodel/packer.py +9 -0
  139. numba_cuda/numba/cuda/datamodel/registry.py +11 -0
  140. numba_cuda/numba/cuda/datamodel/testing.py +11 -0
  141. numba_cuda/numba/cuda/debuginfo.py +903 -0
  142. numba_cuda/numba/cuda/decorators.py +294 -0
  143. numba_cuda/numba/cuda/descriptor.py +35 -0
  144. numba_cuda/numba/cuda/device_init.py +158 -0
  145. numba_cuda/numba/cuda/deviceufunc.py +1021 -0
  146. numba_cuda/numba/cuda/dispatcher.py +2463 -0
  147. numba_cuda/numba/cuda/errors.py +72 -0
  148. numba_cuda/numba/cuda/extending.py +697 -0
  149. numba_cuda/numba/cuda/flags.py +178 -0
  150. numba_cuda/numba/cuda/fp16.py +357 -0
  151. numba_cuda/numba/cuda/include/12/cuda_bf16.h +5118 -0
  152. numba_cuda/numba/cuda/include/12/cuda_bf16.hpp +3865 -0
  153. numba_cuda/numba/cuda/include/12/cuda_fp16.h +5363 -0
  154. numba_cuda/numba/cuda/include/12/cuda_fp16.hpp +3483 -0
  155. numba_cuda/numba/cuda/include/13/cuda_bf16.h +5118 -0
  156. numba_cuda/numba/cuda/include/13/cuda_bf16.hpp +3865 -0
  157. numba_cuda/numba/cuda/include/13/cuda_fp16.h +5363 -0
  158. numba_cuda/numba/cuda/include/13/cuda_fp16.hpp +3483 -0
  159. numba_cuda/numba/cuda/initialize.py +24 -0
  160. numba_cuda/numba/cuda/intrinsic_wrapper.py +41 -0
  161. numba_cuda/numba/cuda/intrinsics.py +382 -0
  162. numba_cuda/numba/cuda/itanium_mangler.py +214 -0
  163. numba_cuda/numba/cuda/kernels/__init__.py +2 -0
  164. numba_cuda/numba/cuda/kernels/reduction.py +265 -0
  165. numba_cuda/numba/cuda/kernels/transpose.py +65 -0
  166. numba_cuda/numba/cuda/libdevice.py +3386 -0
  167. numba_cuda/numba/cuda/libdevicedecl.py +20 -0
  168. numba_cuda/numba/cuda/libdevicefuncs.py +1060 -0
  169. numba_cuda/numba/cuda/libdeviceimpl.py +88 -0
  170. numba_cuda/numba/cuda/locks.py +19 -0
  171. numba_cuda/numba/cuda/lowering.py +1951 -0
  172. numba_cuda/numba/cuda/mathimpl.py +374 -0
  173. numba_cuda/numba/cuda/memory_management/__init__.py +4 -0
  174. numba_cuda/numba/cuda/memory_management/memsys.cu +99 -0
  175. numba_cuda/numba/cuda/memory_management/memsys.cuh +22 -0
  176. numba_cuda/numba/cuda/memory_management/nrt.cu +212 -0
  177. numba_cuda/numba/cuda/memory_management/nrt.cuh +48 -0
  178. numba_cuda/numba/cuda/memory_management/nrt.py +390 -0
  179. numba_cuda/numba/cuda/memory_management/nrt_context.py +438 -0
  180. numba_cuda/numba/cuda/misc/appdirs.py +594 -0
  181. numba_cuda/numba/cuda/misc/cffiimpl.py +24 -0
  182. numba_cuda/numba/cuda/misc/coverage_support.py +43 -0
  183. numba_cuda/numba/cuda/misc/dump_style.py +41 -0
  184. numba_cuda/numba/cuda/misc/findlib.py +75 -0
  185. numba_cuda/numba/cuda/misc/firstlinefinder.py +96 -0
  186. numba_cuda/numba/cuda/misc/gdb_hook.py +240 -0
  187. numba_cuda/numba/cuda/misc/literal.py +28 -0
  188. numba_cuda/numba/cuda/misc/llvm_pass_timings.py +412 -0
  189. numba_cuda/numba/cuda/misc/special.py +94 -0
  190. numba_cuda/numba/cuda/models.py +56 -0
  191. numba_cuda/numba/cuda/np/arraymath.py +5130 -0
  192. numba_cuda/numba/cuda/np/arrayobj.py +7635 -0
  193. numba_cuda/numba/cuda/np/extensions.py +11 -0
  194. numba_cuda/numba/cuda/np/linalg.py +3087 -0
  195. numba_cuda/numba/cuda/np/math/__init__.py +0 -0
  196. numba_cuda/numba/cuda/np/math/cmathimpl.py +558 -0
  197. numba_cuda/numba/cuda/np/math/mathimpl.py +487 -0
  198. numba_cuda/numba/cuda/np/math/numbers.py +1461 -0
  199. numba_cuda/numba/cuda/np/npdatetime.py +969 -0
  200. numba_cuda/numba/cuda/np/npdatetime_helpers.py +217 -0
  201. numba_cuda/numba/cuda/np/npyfuncs.py +1808 -0
  202. numba_cuda/numba/cuda/np/npyimpl.py +1027 -0
  203. numba_cuda/numba/cuda/np/numpy_support.py +798 -0
  204. numba_cuda/numba/cuda/np/polynomial/__init__.py +4 -0
  205. numba_cuda/numba/cuda/np/polynomial/polynomial_core.py +242 -0
  206. numba_cuda/numba/cuda/np/polynomial/polynomial_functions.py +380 -0
  207. numba_cuda/numba/cuda/np/ufunc/__init__.py +4 -0
  208. numba_cuda/numba/cuda/np/ufunc/decorators.py +203 -0
  209. numba_cuda/numba/cuda/np/ufunc/sigparse.py +68 -0
  210. numba_cuda/numba/cuda/np/ufunc/ufuncbuilder.py +65 -0
  211. numba_cuda/numba/cuda/np/ufunc_db.py +1282 -0
  212. numba_cuda/numba/cuda/np/unsafe/__init__.py +0 -0
  213. numba_cuda/numba/cuda/np/unsafe/ndarray.py +84 -0
  214. numba_cuda/numba/cuda/nvvmutils.py +254 -0
  215. numba_cuda/numba/cuda/printimpl.py +126 -0
  216. numba_cuda/numba/cuda/random.py +308 -0
  217. numba_cuda/numba/cuda/reshape_funcs.cu +156 -0
  218. numba_cuda/numba/cuda/serialize.py +267 -0
  219. numba_cuda/numba/cuda/simulator/__init__.py +63 -0
  220. numba_cuda/numba/cuda/simulator/_internal/__init__.py +4 -0
  221. numba_cuda/numba/cuda/simulator/_internal/cuda_bf16.py +2 -0
  222. numba_cuda/numba/cuda/simulator/api.py +179 -0
  223. numba_cuda/numba/cuda/simulator/bf16.py +4 -0
  224. numba_cuda/numba/cuda/simulator/compiler.py +38 -0
  225. numba_cuda/numba/cuda/simulator/cudadrv/__init__.py +11 -0
  226. numba_cuda/numba/cuda/simulator/cudadrv/devicearray.py +462 -0
  227. numba_cuda/numba/cuda/simulator/cudadrv/devices.py +122 -0
  228. numba_cuda/numba/cuda/simulator/cudadrv/driver.py +66 -0
  229. numba_cuda/numba/cuda/simulator/cudadrv/drvapi.py +7 -0
  230. numba_cuda/numba/cuda/simulator/cudadrv/dummyarray.py +7 -0
  231. numba_cuda/numba/cuda/simulator/cudadrv/error.py +10 -0
  232. numba_cuda/numba/cuda/simulator/cudadrv/libs.py +10 -0
  233. numba_cuda/numba/cuda/simulator/cudadrv/linkable_code.py +61 -0
  234. numba_cuda/numba/cuda/simulator/cudadrv/nvrtc.py +11 -0
  235. numba_cuda/numba/cuda/simulator/cudadrv/nvvm.py +32 -0
  236. numba_cuda/numba/cuda/simulator/cudadrv/runtime.py +22 -0
  237. numba_cuda/numba/cuda/simulator/dispatcher.py +11 -0
  238. numba_cuda/numba/cuda/simulator/kernel.py +320 -0
  239. numba_cuda/numba/cuda/simulator/kernelapi.py +509 -0
  240. numba_cuda/numba/cuda/simulator/memory_management/__init__.py +4 -0
  241. numba_cuda/numba/cuda/simulator/memory_management/nrt.py +21 -0
  242. numba_cuda/numba/cuda/simulator/reduction.py +19 -0
  243. numba_cuda/numba/cuda/simulator/tests/support.py +4 -0
  244. numba_cuda/numba/cuda/simulator/vector_types.py +65 -0
  245. numba_cuda/numba/cuda/simulator_init.py +18 -0
  246. numba_cuda/numba/cuda/stubs.py +635 -0
  247. numba_cuda/numba/cuda/target.py +505 -0
  248. numba_cuda/numba/cuda/testing.py +347 -0
  249. numba_cuda/numba/cuda/tests/__init__.py +62 -0
  250. numba_cuda/numba/cuda/tests/benchmarks/__init__.py +0 -0
  251. numba_cuda/numba/cuda/tests/benchmarks/test_kernel_launch.py +119 -0
  252. numba_cuda/numba/cuda/tests/cloudpickle_main_class.py +9 -0
  253. numba_cuda/numba/cuda/tests/core/serialize_usecases.py +113 -0
  254. numba_cuda/numba/cuda/tests/core/test_itanium_mangler.py +83 -0
  255. numba_cuda/numba/cuda/tests/core/test_serialize.py +371 -0
  256. numba_cuda/numba/cuda/tests/cudadrv/__init__.py +9 -0
  257. numba_cuda/numba/cuda/tests/cudadrv/test_array_attr.py +147 -0
  258. numba_cuda/numba/cuda/tests/cudadrv/test_context_stack.py +161 -0
  259. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_array_slicing.py +397 -0
  260. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_auto_context.py +24 -0
  261. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_devicerecord.py +180 -0
  262. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_driver.py +313 -0
  263. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_memory.py +187 -0
  264. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_ndarray.py +621 -0
  265. numba_cuda/numba/cuda/tests/cudadrv/test_deallocations.py +247 -0
  266. numba_cuda/numba/cuda/tests/cudadrv/test_detect.py +100 -0
  267. numba_cuda/numba/cuda/tests/cudadrv/test_emm_plugins.py +198 -0
  268. numba_cuda/numba/cuda/tests/cudadrv/test_events.py +53 -0
  269. numba_cuda/numba/cuda/tests/cudadrv/test_host_alloc.py +72 -0
  270. numba_cuda/numba/cuda/tests/cudadrv/test_init.py +138 -0
  271. numba_cuda/numba/cuda/tests/cudadrv/test_inline_ptx.py +43 -0
  272. numba_cuda/numba/cuda/tests/cudadrv/test_is_fp16.py +15 -0
  273. numba_cuda/numba/cuda/tests/cudadrv/test_linkable_code.py +58 -0
  274. numba_cuda/numba/cuda/tests/cudadrv/test_linker.py +348 -0
  275. numba_cuda/numba/cuda/tests/cudadrv/test_managed_alloc.py +128 -0
  276. numba_cuda/numba/cuda/tests/cudadrv/test_module_callbacks.py +301 -0
  277. numba_cuda/numba/cuda/tests/cudadrv/test_nvjitlink.py +174 -0
  278. numba_cuda/numba/cuda/tests/cudadrv/test_nvrtc.py +28 -0
  279. numba_cuda/numba/cuda/tests/cudadrv/test_nvvm_driver.py +185 -0
  280. numba_cuda/numba/cuda/tests/cudadrv/test_pinned.py +39 -0
  281. numba_cuda/numba/cuda/tests/cudadrv/test_profiler.py +23 -0
  282. numba_cuda/numba/cuda/tests/cudadrv/test_reset_device.py +38 -0
  283. numba_cuda/numba/cuda/tests/cudadrv/test_runtime.py +48 -0
  284. numba_cuda/numba/cuda/tests/cudadrv/test_select_device.py +44 -0
  285. numba_cuda/numba/cuda/tests/cudadrv/test_streams.py +127 -0
  286. numba_cuda/numba/cuda/tests/cudapy/__init__.py +9 -0
  287. numba_cuda/numba/cuda/tests/cudapy/cache_usecases.py +231 -0
  288. numba_cuda/numba/cuda/tests/cudapy/cache_with_cpu_usecases.py +50 -0
  289. numba_cuda/numba/cuda/tests/cudapy/cg_cache_usecases.py +36 -0
  290. numba_cuda/numba/cuda/tests/cudapy/complex_usecases.py +116 -0
  291. numba_cuda/numba/cuda/tests/cudapy/enum_usecases.py +59 -0
  292. numba_cuda/numba/cuda/tests/cudapy/extensions_usecases.py +62 -0
  293. numba_cuda/numba/cuda/tests/cudapy/jitlink.ptx +28 -0
  294. numba_cuda/numba/cuda/tests/cudapy/overload_usecases.py +33 -0
  295. numba_cuda/numba/cuda/tests/cudapy/recursion_usecases.py +104 -0
  296. numba_cuda/numba/cuda/tests/cudapy/test_alignment.py +47 -0
  297. numba_cuda/numba/cuda/tests/cudapy/test_analysis.py +1122 -0
  298. numba_cuda/numba/cuda/tests/cudapy/test_array.py +344 -0
  299. numba_cuda/numba/cuda/tests/cudapy/test_array_alignment.py +268 -0
  300. numba_cuda/numba/cuda/tests/cudapy/test_array_args.py +203 -0
  301. numba_cuda/numba/cuda/tests/cudapy/test_array_methods.py +63 -0
  302. numba_cuda/numba/cuda/tests/cudapy/test_array_reductions.py +360 -0
  303. numba_cuda/numba/cuda/tests/cudapy/test_atomics.py +1815 -0
  304. numba_cuda/numba/cuda/tests/cudapy/test_bfloat16.py +599 -0
  305. numba_cuda/numba/cuda/tests/cudapy/test_bfloat16_bindings.py +377 -0
  306. numba_cuda/numba/cuda/tests/cudapy/test_blackscholes.py +160 -0
  307. numba_cuda/numba/cuda/tests/cudapy/test_boolean.py +27 -0
  308. numba_cuda/numba/cuda/tests/cudapy/test_byteflow.py +98 -0
  309. numba_cuda/numba/cuda/tests/cudapy/test_cache_hints.py +210 -0
  310. numba_cuda/numba/cuda/tests/cudapy/test_caching.py +683 -0
  311. numba_cuda/numba/cuda/tests/cudapy/test_casting.py +265 -0
  312. numba_cuda/numba/cuda/tests/cudapy/test_cffi.py +42 -0
  313. numba_cuda/numba/cuda/tests/cudapy/test_compiler.py +718 -0
  314. numba_cuda/numba/cuda/tests/cudapy/test_complex.py +370 -0
  315. numba_cuda/numba/cuda/tests/cudapy/test_complex_kernel.py +23 -0
  316. numba_cuda/numba/cuda/tests/cudapy/test_const_string.py +142 -0
  317. numba_cuda/numba/cuda/tests/cudapy/test_constmem.py +178 -0
  318. numba_cuda/numba/cuda/tests/cudapy/test_cooperative_groups.py +193 -0
  319. numba_cuda/numba/cuda/tests/cudapy/test_copy_propagate.py +131 -0
  320. numba_cuda/numba/cuda/tests/cudapy/test_cuda_array_interface.py +438 -0
  321. numba_cuda/numba/cuda/tests/cudapy/test_cuda_jit_no_types.py +94 -0
  322. numba_cuda/numba/cuda/tests/cudapy/test_datetime.py +101 -0
  323. numba_cuda/numba/cuda/tests/cudapy/test_debug.py +105 -0
  324. numba_cuda/numba/cuda/tests/cudapy/test_debuginfo.py +889 -0
  325. numba_cuda/numba/cuda/tests/cudapy/test_debuginfo_types.py +476 -0
  326. numba_cuda/numba/cuda/tests/cudapy/test_device_func.py +500 -0
  327. numba_cuda/numba/cuda/tests/cudapy/test_dispatcher.py +820 -0
  328. numba_cuda/numba/cuda/tests/cudapy/test_enums.py +152 -0
  329. numba_cuda/numba/cuda/tests/cudapy/test_errors.py +111 -0
  330. numba_cuda/numba/cuda/tests/cudapy/test_exception.py +170 -0
  331. numba_cuda/numba/cuda/tests/cudapy/test_extending.py +1088 -0
  332. numba_cuda/numba/cuda/tests/cudapy/test_extending_types.py +71 -0
  333. numba_cuda/numba/cuda/tests/cudapy/test_fastmath.py +265 -0
  334. numba_cuda/numba/cuda/tests/cudapy/test_flow_control.py +1433 -0
  335. numba_cuda/numba/cuda/tests/cudapy/test_forall.py +57 -0
  336. numba_cuda/numba/cuda/tests/cudapy/test_freevar.py +34 -0
  337. numba_cuda/numba/cuda/tests/cudapy/test_frexp_ldexp.py +69 -0
  338. numba_cuda/numba/cuda/tests/cudapy/test_globals.py +62 -0
  339. numba_cuda/numba/cuda/tests/cudapy/test_gufunc.py +474 -0
  340. numba_cuda/numba/cuda/tests/cudapy/test_gufunc_scalar.py +167 -0
  341. numba_cuda/numba/cuda/tests/cudapy/test_gufunc_scheduling.py +92 -0
  342. numba_cuda/numba/cuda/tests/cudapy/test_idiv.py +39 -0
  343. numba_cuda/numba/cuda/tests/cudapy/test_inline.py +170 -0
  344. numba_cuda/numba/cuda/tests/cudapy/test_inspect.py +255 -0
  345. numba_cuda/numba/cuda/tests/cudapy/test_intrinsics.py +1219 -0
  346. numba_cuda/numba/cuda/tests/cudapy/test_ipc.py +263 -0
  347. numba_cuda/numba/cuda/tests/cudapy/test_ir.py +598 -0
  348. numba_cuda/numba/cuda/tests/cudapy/test_ir_utils.py +276 -0
  349. numba_cuda/numba/cuda/tests/cudapy/test_iterators.py +101 -0
  350. numba_cuda/numba/cuda/tests/cudapy/test_lang.py +68 -0
  351. numba_cuda/numba/cuda/tests/cudapy/test_laplace.py +123 -0
  352. numba_cuda/numba/cuda/tests/cudapy/test_libdevice.py +194 -0
  353. numba_cuda/numba/cuda/tests/cudapy/test_lineinfo.py +220 -0
  354. numba_cuda/numba/cuda/tests/cudapy/test_localmem.py +173 -0
  355. numba_cuda/numba/cuda/tests/cudapy/test_make_function_to_jit_function.py +364 -0
  356. numba_cuda/numba/cuda/tests/cudapy/test_mandel.py +47 -0
  357. numba_cuda/numba/cuda/tests/cudapy/test_math.py +842 -0
  358. numba_cuda/numba/cuda/tests/cudapy/test_matmul.py +76 -0
  359. numba_cuda/numba/cuda/tests/cudapy/test_minmax.py +78 -0
  360. numba_cuda/numba/cuda/tests/cudapy/test_montecarlo.py +25 -0
  361. numba_cuda/numba/cuda/tests/cudapy/test_multigpu.py +145 -0
  362. numba_cuda/numba/cuda/tests/cudapy/test_multiprocessing.py +39 -0
  363. numba_cuda/numba/cuda/tests/cudapy/test_multithreads.py +82 -0
  364. numba_cuda/numba/cuda/tests/cudapy/test_nondet.py +53 -0
  365. numba_cuda/numba/cuda/tests/cudapy/test_operator.py +504 -0
  366. numba_cuda/numba/cuda/tests/cudapy/test_optimization.py +93 -0
  367. numba_cuda/numba/cuda/tests/cudapy/test_overload.py +402 -0
  368. numba_cuda/numba/cuda/tests/cudapy/test_powi.py +128 -0
  369. numba_cuda/numba/cuda/tests/cudapy/test_print.py +193 -0
  370. numba_cuda/numba/cuda/tests/cudapy/test_py2_div_issue.py +37 -0
  371. numba_cuda/numba/cuda/tests/cudapy/test_random.py +117 -0
  372. numba_cuda/numba/cuda/tests/cudapy/test_record_dtype.py +614 -0
  373. numba_cuda/numba/cuda/tests/cudapy/test_recursion.py +130 -0
  374. numba_cuda/numba/cuda/tests/cudapy/test_reduction.py +94 -0
  375. numba_cuda/numba/cuda/tests/cudapy/test_retrieve_autoconverted_arrays.py +83 -0
  376. numba_cuda/numba/cuda/tests/cudapy/test_serialize.py +86 -0
  377. numba_cuda/numba/cuda/tests/cudapy/test_slicing.py +40 -0
  378. numba_cuda/numba/cuda/tests/cudapy/test_sm.py +457 -0
  379. numba_cuda/numba/cuda/tests/cudapy/test_sm_creation.py +233 -0
  380. numba_cuda/numba/cuda/tests/cudapy/test_ssa.py +454 -0
  381. numba_cuda/numba/cuda/tests/cudapy/test_stream_api.py +56 -0
  382. numba_cuda/numba/cuda/tests/cudapy/test_sync.py +277 -0
  383. numba_cuda/numba/cuda/tests/cudapy/test_tracing.py +200 -0
  384. numba_cuda/numba/cuda/tests/cudapy/test_transpose.py +90 -0
  385. numba_cuda/numba/cuda/tests/cudapy/test_typeconv.py +333 -0
  386. numba_cuda/numba/cuda/tests/cudapy/test_typeinfer.py +538 -0
  387. numba_cuda/numba/cuda/tests/cudapy/test_ufuncs.py +585 -0
  388. numba_cuda/numba/cuda/tests/cudapy/test_userexc.py +42 -0
  389. numba_cuda/numba/cuda/tests/cudapy/test_vector_type.py +485 -0
  390. numba_cuda/numba/cuda/tests/cudapy/test_vectorize.py +312 -0
  391. numba_cuda/numba/cuda/tests/cudapy/test_vectorize_complex.py +23 -0
  392. numba_cuda/numba/cuda/tests/cudapy/test_vectorize_decor.py +183 -0
  393. numba_cuda/numba/cuda/tests/cudapy/test_vectorize_device.py +40 -0
  394. numba_cuda/numba/cuda/tests/cudapy/test_vectorize_scalar_arg.py +40 -0
  395. numba_cuda/numba/cuda/tests/cudapy/test_warning.py +206 -0
  396. numba_cuda/numba/cuda/tests/cudapy/test_warp_ops.py +331 -0
  397. numba_cuda/numba/cuda/tests/cudasim/__init__.py +9 -0
  398. numba_cuda/numba/cuda/tests/cudasim/support.py +9 -0
  399. numba_cuda/numba/cuda/tests/cudasim/test_cudasim_issues.py +111 -0
  400. numba_cuda/numba/cuda/tests/data/__init__.py +2 -0
  401. numba_cuda/numba/cuda/tests/data/cta_barrier.cu +28 -0
  402. numba_cuda/numba/cuda/tests/data/cuda_include.cu +10 -0
  403. numba_cuda/numba/cuda/tests/data/error.cu +12 -0
  404. numba_cuda/numba/cuda/tests/data/include/add.cuh +8 -0
  405. numba_cuda/numba/cuda/tests/data/jitlink.cu +28 -0
  406. numba_cuda/numba/cuda/tests/data/jitlink.ptx +49 -0
  407. numba_cuda/numba/cuda/tests/data/warn.cu +12 -0
  408. numba_cuda/numba/cuda/tests/doc_examples/__init__.py +9 -0
  409. numba_cuda/numba/cuda/tests/doc_examples/ffi/__init__.py +2 -0
  410. numba_cuda/numba/cuda/tests/doc_examples/ffi/functions.cu +54 -0
  411. numba_cuda/numba/cuda/tests/doc_examples/ffi/include/mul.cuh +8 -0
  412. numba_cuda/numba/cuda/tests/doc_examples/ffi/saxpy.cu +14 -0
  413. numba_cuda/numba/cuda/tests/doc_examples/test_cg.py +86 -0
  414. numba_cuda/numba/cuda/tests/doc_examples/test_cpointer.py +68 -0
  415. numba_cuda/numba/cuda/tests/doc_examples/test_cpu_gpu_compat.py +81 -0
  416. numba_cuda/numba/cuda/tests/doc_examples/test_ffi.py +141 -0
  417. numba_cuda/numba/cuda/tests/doc_examples/test_laplace.py +160 -0
  418. numba_cuda/numba/cuda/tests/doc_examples/test_matmul.py +180 -0
  419. numba_cuda/numba/cuda/tests/doc_examples/test_montecarlo.py +119 -0
  420. numba_cuda/numba/cuda/tests/doc_examples/test_random.py +66 -0
  421. numba_cuda/numba/cuda/tests/doc_examples/test_reduction.py +80 -0
  422. numba_cuda/numba/cuda/tests/doc_examples/test_sessionize.py +206 -0
  423. numba_cuda/numba/cuda/tests/doc_examples/test_ufunc.py +53 -0
  424. numba_cuda/numba/cuda/tests/doc_examples/test_vecadd.py +76 -0
  425. numba_cuda/numba/cuda/tests/nocuda/__init__.py +9 -0
  426. numba_cuda/numba/cuda/tests/nocuda/test_dummyarray.py +391 -0
  427. numba_cuda/numba/cuda/tests/nocuda/test_function_resolution.py +48 -0
  428. numba_cuda/numba/cuda/tests/nocuda/test_import.py +63 -0
  429. numba_cuda/numba/cuda/tests/nocuda/test_library_lookup.py +252 -0
  430. numba_cuda/numba/cuda/tests/nocuda/test_nvvm.py +59 -0
  431. numba_cuda/numba/cuda/tests/nrt/__init__.py +9 -0
  432. numba_cuda/numba/cuda/tests/nrt/test_nrt.py +387 -0
  433. numba_cuda/numba/cuda/tests/nrt/test_nrt_refct.py +124 -0
  434. numba_cuda/numba/cuda/tests/support.py +900 -0
  435. numba_cuda/numba/cuda/typeconv/__init__.py +4 -0
  436. numba_cuda/numba/cuda/typeconv/castgraph.py +137 -0
  437. numba_cuda/numba/cuda/typeconv/rules.py +63 -0
  438. numba_cuda/numba/cuda/typeconv/typeconv.py +121 -0
  439. numba_cuda/numba/cuda/types/__init__.py +233 -0
  440. numba_cuda/numba/cuda/types/__init__.pyi +167 -0
  441. numba_cuda/numba/cuda/types/abstract.py +9 -0
  442. numba_cuda/numba/cuda/types/common.py +9 -0
  443. numba_cuda/numba/cuda/types/containers.py +9 -0
  444. numba_cuda/numba/cuda/types/cuda_abstract.py +533 -0
  445. numba_cuda/numba/cuda/types/cuda_common.py +110 -0
  446. numba_cuda/numba/cuda/types/cuda_containers.py +971 -0
  447. numba_cuda/numba/cuda/types/cuda_function_type.py +230 -0
  448. numba_cuda/numba/cuda/types/cuda_functions.py +798 -0
  449. numba_cuda/numba/cuda/types/cuda_iterators.py +120 -0
  450. numba_cuda/numba/cuda/types/cuda_misc.py +569 -0
  451. numba_cuda/numba/cuda/types/cuda_npytypes.py +690 -0
  452. numba_cuda/numba/cuda/types/cuda_scalars.py +280 -0
  453. numba_cuda/numba/cuda/types/ext_types.py +101 -0
  454. numba_cuda/numba/cuda/types/function_type.py +11 -0
  455. numba_cuda/numba/cuda/types/functions.py +9 -0
  456. numba_cuda/numba/cuda/types/iterators.py +9 -0
  457. numba_cuda/numba/cuda/types/misc.py +9 -0
  458. numba_cuda/numba/cuda/types/npytypes.py +9 -0
  459. numba_cuda/numba/cuda/types/scalars.py +9 -0
  460. numba_cuda/numba/cuda/typing/__init__.py +19 -0
  461. numba_cuda/numba/cuda/typing/arraydecl.py +939 -0
  462. numba_cuda/numba/cuda/typing/asnumbatype.py +130 -0
  463. numba_cuda/numba/cuda/typing/bufproto.py +70 -0
  464. numba_cuda/numba/cuda/typing/builtins.py +1209 -0
  465. numba_cuda/numba/cuda/typing/cffi_utils.py +219 -0
  466. numba_cuda/numba/cuda/typing/cmathdecl.py +47 -0
  467. numba_cuda/numba/cuda/typing/collections.py +138 -0
  468. numba_cuda/numba/cuda/typing/context.py +782 -0
  469. numba_cuda/numba/cuda/typing/ctypes_utils.py +125 -0
  470. numba_cuda/numba/cuda/typing/dictdecl.py +63 -0
  471. numba_cuda/numba/cuda/typing/enumdecl.py +74 -0
  472. numba_cuda/numba/cuda/typing/listdecl.py +147 -0
  473. numba_cuda/numba/cuda/typing/mathdecl.py +158 -0
  474. numba_cuda/numba/cuda/typing/npdatetime.py +322 -0
  475. numba_cuda/numba/cuda/typing/npydecl.py +749 -0
  476. numba_cuda/numba/cuda/typing/setdecl.py +115 -0
  477. numba_cuda/numba/cuda/typing/templates.py +1446 -0
  478. numba_cuda/numba/cuda/typing/typeof.py +301 -0
  479. numba_cuda/numba/cuda/ufuncs.py +746 -0
  480. numba_cuda/numba/cuda/utils.py +724 -0
  481. numba_cuda/numba/cuda/vector_types.py +214 -0
  482. numba_cuda/numba/cuda/vectorizers.py +260 -0
  483. numba_cuda-0.21.1.dist-info/METADATA +109 -0
  484. numba_cuda-0.21.1.dist-info/RECORD +488 -0
  485. numba_cuda-0.21.1.dist-info/WHEEL +5 -0
  486. numba_cuda-0.21.1.dist-info/licenses/LICENSE +26 -0
  487. numba_cuda-0.21.1.dist-info/licenses/LICENSE.numba +24 -0
  488. numba_cuda-0.21.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1808 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: BSD-2-Clause
3
+
4
+ """Codegen for functions used as kernels in NumPy functions
5
+
6
+ Typically, the kernels of several ufuncs that can't map directly to
7
+ Python builtins
8
+ """
9
+
10
+ import math
11
+
12
+ import llvmlite.ir
13
+ import numpy as np
14
+
15
+ from numba.cuda.extending import overload
16
+ from numba.cuda.core.imputils import impl_ret_untracked
17
+ from numba.cuda import types
18
+ from numba.cuda.core import errors
19
+ from numba.cuda import cgutils, typing
20
+ from numba.cuda.np import npdatetime
21
+ from numba.cuda.extending import register_jitable
22
+ from numba.cuda.np.math import cmathimpl, mathimpl, numbers
23
+ from numba.cuda.np.numpy_support import numpy_version
24
+
25
+ # some NumPy constants. Note that we could generate some of them using
26
+ # the math library, but having the values copied from npy_math seems to
27
+ # yield more accurate results
28
+ _NPY_LOG2E = 1.442695040888963407359924681001892137 # math.log(math.e, 2)
29
+ _NPY_LOG10E = 0.434294481903251827651128918916605082 # math.log(math.e, 10)
30
+ _NPY_LOGE2 = 0.693147180559945309417232121458176568 # math.log(2)
31
+
32
+
33
+ def _check_arity_and_homogeneity(sig, args, arity, return_type=None):
34
+ """checks that the following are true:
35
+ - args and sig.args have arg_count elements
36
+ - all input types are homogeneous
37
+ - return type is 'return_type' if provided, otherwise it must be
38
+ homogeneous with the input types.
39
+ """
40
+ assert len(args) == arity
41
+ assert len(sig.args) == arity
42
+ ty = sig.args[0]
43
+ if return_type is None:
44
+ return_type = ty
45
+ # must have homogeneous args
46
+ if not (
47
+ all(arg == ty for arg in sig.args) and sig.return_type == return_type
48
+ ):
49
+ import inspect
50
+
51
+ fname = inspect.currentframe().f_back.f_code.co_name
52
+ msg = "{0} called with invalid types: {1}".format(fname, sig)
53
+ assert False, msg
54
+
55
+
56
+ cast_arg_ty = types.float64
57
+
58
+
59
+ def _call_func_by_name_with_cast(
60
+ context, builder, sig, args, func_name, ty=cast_arg_ty
61
+ ):
62
+ # it is quite common in NumPy to have loops implemented as a call
63
+ # to the double version of the function, wrapped in casts. This
64
+ # helper function facilitates that.
65
+ mod = builder.module
66
+ lty = context.get_argument_type(ty)
67
+ fnty = llvmlite.ir.FunctionType(lty, [lty] * len(sig.args))
68
+ fn = cgutils.insert_pure_function(mod, fnty, name=func_name)
69
+ cast_args = [
70
+ context.cast(builder, arg, argty, ty)
71
+ for arg, argty in zip(args, sig.args)
72
+ ]
73
+
74
+ result = builder.call(fn, cast_args)
75
+ return context.cast(builder, result, types.float64, sig.return_type)
76
+
77
+
78
+ def _dispatch_func_by_name_type(context, builder, sig, args, table, user_name):
79
+ # for most cases the functions are homogeneous on all their types.
80
+ # this code dispatches on the first argument type as it is the most useful
81
+ # for our uses (all cases but ldexp are homogeneous in all types, and
82
+ # dispatching on the first argument type works of ldexp as well)
83
+ #
84
+ # assumes that the function pointed by func_name has the type
85
+ # signature sig (but needs translation to llvm types).
86
+
87
+ ty = sig.args[0]
88
+ try:
89
+ func_name = table[ty]
90
+ except KeyError as e:
91
+ msg = "No {0} function for real type {1}".format(user_name, str(e))
92
+ raise errors.LoweringError(msg)
93
+
94
+ mod = builder.module
95
+ if ty in types.complex_domain:
96
+ # In numba struct types are always passed by pointer. So the call has to
97
+ # be transformed from "result = func(ops...)" to "func(&result, ops...).
98
+ # note that the result value pointer as first argument is the convention
99
+ # used by numba.
100
+
101
+ # First, prepare the return value
102
+ out = context.make_complex(builder, ty)
103
+ ptrargs = [cgutils.alloca_once_value(builder, arg) for arg in args]
104
+ call_args = [out._getpointer()] + ptrargs
105
+ # get_value_as_argument for struct types like complex allocate stack space
106
+ # and initialize with the value, the return value is the pointer to that
107
+ # allocated space (ie: pointer to a copy of the value in the stack).
108
+ # get_argument_type returns a pointer to the struct type in consonance.
109
+ call_argtys = [ty] + list(sig.args)
110
+ call_argltys = [
111
+ context.get_value_type(ty).as_pointer() for ty in call_argtys
112
+ ]
113
+ fnty = llvmlite.ir.FunctionType(llvmlite.ir.VoidType(), call_argltys)
114
+ # Note: the function isn't pure here (it writes to its pointer args)
115
+ fn = cgutils.get_or_insert_function(mod, fnty, func_name)
116
+ builder.call(fn, call_args)
117
+ retval = builder.load(call_args[0])
118
+ else:
119
+ argtypes = [context.get_argument_type(aty) for aty in sig.args]
120
+ restype = context.get_argument_type(sig.return_type)
121
+ fnty = llvmlite.ir.FunctionType(restype, argtypes)
122
+ fn = cgutils.insert_pure_function(mod, fnty, name=func_name)
123
+ retval = context.call_external_function(builder, fn, sig.args, args)
124
+ return retval
125
+
126
+
127
+ ########################################################################
128
+ # Division kernels inspired by NumPy loops.c.src code
129
+ #
130
+ # The builtins are not applicable as they rely on a test for zero in the
131
+ # denominator. If it is zero the appropriate exception is raised.
132
+ # In NumPy, a division by zero does not raise an exception, but instead
133
+ # generated a known value. Note that a division by zero in any of the
134
+ # operations of a vector may raise an exception or issue a warning
135
+ # depending on the np.seterr configuration. This is not supported
136
+ # right now (and in any case, it won't be handled by these functions
137
+ # either)
138
+
139
+
140
+ def np_int_sdiv_impl(context, builder, sig, args):
141
+ # based on the actual code in NumPy loops.c.src for signed integer types
142
+ _check_arity_and_homogeneity(sig, args, 2)
143
+
144
+ num, den = args
145
+ ty = sig.args[0] # any arg type will do, homogeneous
146
+
147
+ ZERO = context.get_constant(ty, 0)
148
+ MINUS_ONE = context.get_constant(ty, -1)
149
+ MIN_INT = context.get_constant(ty, 1 << (den.type.width - 1))
150
+ den_is_zero = builder.icmp_unsigned("==", ZERO, den)
151
+ den_is_minus_one = builder.icmp_unsigned("==", MINUS_ONE, den)
152
+ num_is_min_int = builder.icmp_unsigned("==", MIN_INT, num)
153
+ could_cause_sigfpe = builder.and_(den_is_minus_one, num_is_min_int)
154
+ force_zero = builder.or_(den_is_zero, could_cause_sigfpe)
155
+ with builder.if_else(force_zero, likely=False) as (then, otherwise):
156
+ with then:
157
+ bb_then = builder.basic_block
158
+ with otherwise:
159
+ bb_otherwise = builder.basic_block
160
+ div = builder.sdiv(num, den)
161
+ mod = builder.srem(num, den)
162
+ num_gt_zero = builder.icmp_signed(">", num, ZERO)
163
+ den_gt_zero = builder.icmp_signed(">", den, ZERO)
164
+ not_same_sign = builder.xor(num_gt_zero, den_gt_zero)
165
+ mod_not_zero = builder.icmp_unsigned("!=", mod, ZERO)
166
+ needs_fixing = builder.and_(not_same_sign, mod_not_zero)
167
+ fix_value = builder.select(needs_fixing, MINUS_ONE, ZERO)
168
+ result_otherwise = builder.add(div, fix_value)
169
+
170
+ result = builder.phi(ZERO.type)
171
+ result.add_incoming(ZERO, bb_then)
172
+ result.add_incoming(result_otherwise, bb_otherwise)
173
+
174
+ return result
175
+
176
+
177
+ def np_int_srem_impl(context, builder, sig, args):
178
+ # based on the actual code in NumPy loops.c.src for signed integers
179
+ _check_arity_and_homogeneity(sig, args, 2)
180
+
181
+ num, den = args
182
+ ty = sig.args[0] # any arg type will do, homogeneous
183
+
184
+ ZERO = context.get_constant(ty, 0)
185
+ den_not_zero = builder.icmp_unsigned("!=", ZERO, den)
186
+ bb_no_if = builder.basic_block
187
+ with cgutils.if_unlikely(builder, den_not_zero):
188
+ bb_if = builder.basic_block
189
+ mod = builder.srem(num, den)
190
+ num_gt_zero = builder.icmp_signed(">", num, ZERO)
191
+ den_gt_zero = builder.icmp_signed(">", den, ZERO)
192
+ not_same_sign = builder.xor(num_gt_zero, den_gt_zero)
193
+ mod_not_zero = builder.icmp_unsigned("!=", mod, ZERO)
194
+ needs_fixing = builder.and_(not_same_sign, mod_not_zero)
195
+ fix_value = builder.select(needs_fixing, den, ZERO)
196
+ final_mod = builder.add(fix_value, mod)
197
+
198
+ result = builder.phi(ZERO.type)
199
+ result.add_incoming(ZERO, bb_no_if)
200
+ result.add_incoming(final_mod, bb_if)
201
+
202
+ return result
203
+
204
+
205
+ def np_int_sdivrem_impl(context, builder, sig, args):
206
+ div = np_int_sdiv_impl(
207
+ context, builder, sig.return_type[0](*sig.args), args
208
+ )
209
+ rem = np_int_srem_impl(
210
+ context, builder, sig.return_type[1](*sig.args), args
211
+ )
212
+ return context.make_tuple(builder, sig.return_type, [div, rem])
213
+
214
+
215
+ def np_int_udiv_impl(context, builder, sig, args):
216
+ _check_arity_and_homogeneity(sig, args, 2)
217
+
218
+ num, den = args
219
+ ty = sig.args[0] # any arg type will do, homogeneous
220
+
221
+ ZERO = context.get_constant(ty, 0)
222
+ div_by_zero = builder.icmp_unsigned("==", ZERO, den)
223
+ with builder.if_else(div_by_zero, likely=False) as (then, otherwise):
224
+ with then:
225
+ # division by zero
226
+ bb_then = builder.basic_block
227
+ with otherwise:
228
+ # divide!
229
+ div = builder.udiv(num, den)
230
+ bb_otherwise = builder.basic_block
231
+
232
+ result = builder.phi(ZERO.type)
233
+ result.add_incoming(ZERO, bb_then)
234
+ result.add_incoming(div, bb_otherwise)
235
+ return result
236
+
237
+
238
+ def np_int_urem_impl(context, builder, sig, args):
239
+ # based on the actual code in NumPy loops.c.src for signed integers
240
+ _check_arity_and_homogeneity(sig, args, 2)
241
+
242
+ num, den = args
243
+ ty = sig.args[0] # any arg type will do, homogeneous
244
+
245
+ ZERO = context.get_constant(ty, 0)
246
+ den_not_zero = builder.icmp_unsigned("!=", ZERO, den)
247
+ bb_no_if = builder.basic_block
248
+ with cgutils.if_unlikely(builder, den_not_zero):
249
+ bb_if = builder.basic_block
250
+ mod = builder.urem(num, den)
251
+
252
+ result = builder.phi(ZERO.type)
253
+ result.add_incoming(ZERO, bb_no_if)
254
+ result.add_incoming(mod, bb_if)
255
+
256
+ return result
257
+
258
+
259
+ def np_int_udivrem_impl(context, builder, sig, args):
260
+ div = np_int_udiv_impl(
261
+ context, builder, sig.return_type[0](*sig.args), args
262
+ )
263
+ rem = np_int_urem_impl(
264
+ context, builder, sig.return_type[1](*sig.args), args
265
+ )
266
+ return context.make_tuple(builder, sig.return_type, [div, rem])
267
+
268
+
269
+ # implementation of int_fmod is in fact the same as the unsigned remainder,
270
+ # that is: srem with a special case returning 0 when the denominator is 0.
271
+ np_int_fmod_impl = np_int_urem_impl
272
+
273
+
274
+ def np_real_div_impl(context, builder, sig, args):
275
+ # in NumPy real div has the same semantics as an fdiv for generating
276
+ # NANs, INF and NINF
277
+ _check_arity_and_homogeneity(sig, args, 2)
278
+ return builder.fdiv(*args)
279
+
280
+
281
+ def np_real_mod_impl(context, builder, sig, args):
282
+ # note: this maps to NumPy remainder, which has the same semantics as Python
283
+ # based on code in loops.c.src
284
+ _check_arity_and_homogeneity(sig, args, 2)
285
+ in1, in2 = args
286
+ ty = sig.args[0]
287
+
288
+ ZERO = context.get_constant(ty, 0.0)
289
+ res = builder.frem(in1, in2)
290
+ res_ne_zero = builder.fcmp_ordered("!=", res, ZERO)
291
+ den_lt_zero = builder.fcmp_ordered("<", in2, ZERO)
292
+ res_lt_zero = builder.fcmp_ordered("<", res, ZERO)
293
+ needs_fixing = builder.and_(
294
+ res_ne_zero, builder.xor(den_lt_zero, res_lt_zero)
295
+ )
296
+ fix_value = builder.select(needs_fixing, in2, ZERO)
297
+
298
+ return builder.fadd(res, fix_value)
299
+
300
+
301
+ def np_real_fmod_impl(context, builder, sig, args):
302
+ _check_arity_and_homogeneity(sig, args, 2)
303
+ return builder.frem(*args)
304
+
305
+
306
+ def _fabs(context, builder, arg):
307
+ ZERO = llvmlite.ir.Constant(arg.type, 0.0)
308
+ arg_negated = builder.fsub(ZERO, arg)
309
+ arg_is_negative = builder.fcmp_ordered("<", arg, ZERO)
310
+ return builder.select(arg_is_negative, arg_negated, arg)
311
+
312
+
313
+ def np_complex_div_impl(context, builder, sig, args):
314
+ # Extracted from numpy/core/src/umath/loops.c.src,
315
+ # inspired by complex_div_impl
316
+ # variables named coherent with loops.c.src
317
+ # This is implemented using the approach described in
318
+ # R.L. Smith. Algorithm 116: Complex division.
319
+ # Communications of the ACM, 5(8):435, 1962
320
+
321
+ in1, in2 = [
322
+ context.make_complex(builder, sig.args[0], value=arg) for arg in args
323
+ ]
324
+
325
+ in1r = in1.real # numerator.real
326
+ in1i = in1.imag # numerator.imag
327
+ in2r = in2.real # denominator.real
328
+ in2i = in2.imag # denominator.imag
329
+ ftype = in1r.type
330
+ assert all([i.type == ftype for i in [in1r, in1i, in2r, in2i]]), (
331
+ "mismatched types"
332
+ )
333
+ out = context.make_helper(builder, sig.return_type)
334
+
335
+ ZERO = llvmlite.ir.Constant(ftype, 0.0)
336
+ ONE = llvmlite.ir.Constant(ftype, 1.0)
337
+
338
+ # if abs(denominator.real) >= abs(denominator.imag)
339
+ in2r_abs = _fabs(context, builder, in2r)
340
+ in2i_abs = _fabs(context, builder, in2i)
341
+ in2r_abs_ge_in2i_abs = builder.fcmp_ordered(">=", in2r_abs, in2i_abs)
342
+ with builder.if_else(in2r_abs_ge_in2i_abs) as (then, otherwise):
343
+ with then:
344
+ # if abs(denominator.real) == 0 and abs(denominator.imag) == 0
345
+ in2r_is_zero = builder.fcmp_ordered("==", in2r_abs, ZERO)
346
+ in2i_is_zero = builder.fcmp_ordered("==", in2i_abs, ZERO)
347
+ in2_is_zero = builder.and_(in2r_is_zero, in2i_is_zero)
348
+ with builder.if_else(in2_is_zero) as (inn_then, inn_otherwise):
349
+ with inn_then:
350
+ # division by 0.
351
+ # fdiv generates the appropriate NAN/INF/NINF
352
+ out.real = builder.fdiv(in1r, in2r_abs)
353
+ out.imag = builder.fdiv(in1i, in2i_abs)
354
+ with inn_otherwise:
355
+ # general case for:
356
+ # abs(denominator.real) > abs(denominator.imag)
357
+ rat = builder.fdiv(in2i, in2r)
358
+ # scl = 1.0/(in2r + in2i*rat)
359
+ tmp1 = builder.fmul(in2i, rat)
360
+ tmp2 = builder.fadd(in2r, tmp1)
361
+ scl = builder.fdiv(ONE, tmp2)
362
+ # out.real = (in1r + in1i*rat)*scl
363
+ # out.imag = (in1i - in1r*rat)*scl
364
+ tmp3 = builder.fmul(in1i, rat)
365
+ tmp4 = builder.fmul(in1r, rat)
366
+ tmp5 = builder.fadd(in1r, tmp3)
367
+ tmp6 = builder.fsub(in1i, tmp4)
368
+ out.real = builder.fmul(tmp5, scl)
369
+ out.imag = builder.fmul(tmp6, scl)
370
+ with otherwise:
371
+ # general case for:
372
+ # abs(denominator.imag) > abs(denominator.real)
373
+ rat = builder.fdiv(in2r, in2i)
374
+ # scl = 1.0/(in2i + in2r*rat)
375
+ tmp1 = builder.fmul(in2r, rat)
376
+ tmp2 = builder.fadd(in2i, tmp1)
377
+ scl = builder.fdiv(ONE, tmp2)
378
+ # out.real = (in1r*rat + in1i)*scl
379
+ # out.imag = (in1i*rat - in1r)*scl
380
+ tmp3 = builder.fmul(in1r, rat)
381
+ tmp4 = builder.fmul(in1i, rat)
382
+ tmp5 = builder.fadd(tmp3, in1i)
383
+ tmp6 = builder.fsub(tmp4, in1r)
384
+ out.real = builder.fmul(tmp5, scl)
385
+ out.imag = builder.fmul(tmp6, scl)
386
+
387
+ return out._getvalue()
388
+
389
+
390
+ ########################################################################
391
+ # NumPy logaddexp
392
+
393
+
394
+ def _npy_logaddexp(x1, x2):
395
+ pass
396
+
397
+
398
+ def _generate_logaddexp(fnoverload, const, log1pfn, expfn):
399
+ # Code generation for logaddexp and logaddexp2 is based on:
400
+ # https://github.com/numpy/numpy/blob/12c2b7dd62fc0c14b81c8892ed5f4f59cc94d09c/numpy/core/src/npymath/npy_math_internal.h.src#L467-L507
401
+
402
+ @overload(fnoverload)
403
+ def ol_npy_logaddexp(x1, x2):
404
+ if x1 != x2:
405
+ return
406
+ shift = x1(const)
407
+
408
+ def impl(x1, x2):
409
+ x, y = x1, x2
410
+ if x == y:
411
+ # Handles infinities of the same sign without warnings
412
+ return x + shift
413
+ else:
414
+ tmp = x - y
415
+ if tmp > 0:
416
+ return x + log1pfn(expfn(-tmp))
417
+ elif tmp <= 0:
418
+ return y + log1pfn(expfn(tmp))
419
+ else:
420
+ # NaN
421
+ return tmp
422
+
423
+ return impl
424
+
425
+
426
+ def _npy_logaddexp(x1, x2):
427
+ pass
428
+
429
+
430
+ _generate_logaddexp(_npy_logaddexp, _NPY_LOGE2, np.log1p, np.exp)
431
+
432
+
433
+ def np_real_logaddexp_impl(context, builder, sig, args):
434
+ _check_arity_and_homogeneity(sig, args, 2)
435
+
436
+ fnty = context.typing_context.resolve_value_type(_npy_logaddexp)
437
+ sig = fnty.get_call_type(context.typing_context, (*sig.args,), {})
438
+ impl = context.get_function(fnty, sig)
439
+ return impl(builder, args)
440
+
441
+
442
+ ########################################################################
443
+ # NumPy logaddexp2
444
+ def _npy_logaddexp2(x1, x2):
445
+ pass
446
+
447
+
448
+ def npy_log2_1p(x):
449
+ pass
450
+
451
+
452
+ # The following npy_log2_1p function is a translation of:
453
+ # https://github.com/numpy/numpy/blob/12c2b7dd62fc0c14b81c8892ed5f4f59cc94d09c/numpy/core/src/npymath/npy_math_internal.h.src#L457-L460
454
+
455
+
456
+ @overload(npy_log2_1p)
457
+ def ol_npy_log2_1p(x):
458
+ LOG2E = x(_NPY_LOG2E)
459
+
460
+ def impl(x):
461
+ return LOG2E * np.log1p(x)
462
+
463
+ return impl
464
+
465
+
466
+ _generate_logaddexp(_npy_logaddexp2, 1.0, npy_log2_1p, np.exp2)
467
+
468
+
469
+ def np_real_logaddexp2_impl(context, builder, sig, args):
470
+ _check_arity_and_homogeneity(sig, args, 2)
471
+
472
+ fnty = context.typing_context.resolve_value_type(_npy_logaddexp2)
473
+ sig = fnty.get_call_type(context.typing_context, (*sig.args,), {})
474
+ impl = context.get_function(fnty, sig)
475
+ return impl(builder, args)
476
+
477
+
478
+ ########################################################################
479
+ # true div kernels
480
+
481
+
482
+ def np_int_truediv_impl(context, builder, sig, args):
483
+ # in NumPy we don't check for 0 denominator... fdiv handles div by
484
+ # 0 in the way NumPy expects..
485
+ # integer truediv always yields double
486
+ num, den = args
487
+ lltype = num.type
488
+ assert all(i.type == lltype for i in args), "must have homogeneous types"
489
+ numty, denty = sig.args
490
+
491
+ num = context.cast(builder, num, numty, types.float64)
492
+ den = context.cast(builder, den, denty, types.float64)
493
+
494
+ return builder.fdiv(num, den)
495
+
496
+
497
+ ########################################################################
498
+ # floor div kernels
499
+
500
+
501
+ def np_real_floor_div_impl(context, builder, sig, args):
502
+ res = np_real_div_impl(context, builder, sig, args)
503
+ s = typing.signature(sig.return_type, sig.return_type)
504
+ return np_real_floor_impl(context, builder, s, (res,))
505
+
506
+
507
+ def np_real_divmod_impl(context, builder, sig, args):
508
+ div = np_real_floor_div_impl(
509
+ context, builder, sig.return_type[0](*sig.args), args
510
+ )
511
+ rem = np_real_mod_impl(
512
+ context, builder, sig.return_type[1](*sig.args), args
513
+ )
514
+ return context.make_tuple(builder, sig.return_type, [div, rem])
515
+
516
+
517
+ def np_complex_floor_div_impl(context, builder, sig, args):
518
+ # this is based on the complex floor divide in Numpy's loops.c.src
519
+ # This is basically a full complex division with a complex floor
520
+ # applied.
521
+ # The complex floor seems to be defined as the real floor applied
522
+ # with the real part and zero in the imaginary part. Fully developed
523
+ # so it avoids computing anything related to the imaginary result.
524
+ float_kind = sig.args[0].underlying_float
525
+ floor_sig = typing.signature(float_kind, float_kind)
526
+
527
+ in1, in2 = [
528
+ context.make_complex(builder, sig.args[0], value=arg) for arg in args
529
+ ]
530
+
531
+ in1r = in1.real
532
+ in1i = in1.imag
533
+ in2r = in2.real
534
+ in2i = in2.imag
535
+ ftype = in1r.type
536
+ assert all([i.type == ftype for i in [in1r, in1i, in2r, in2i]]), (
537
+ "mismatched types"
538
+ )
539
+
540
+ ZERO = llvmlite.ir.Constant(ftype, 0.0)
541
+
542
+ out = context.make_helper(builder, sig.return_type)
543
+ out.imag = ZERO
544
+
545
+ in2r_abs = _fabs(context, builder, in2r)
546
+ in2i_abs = _fabs(context, builder, in2i)
547
+ in2r_abs_ge_in2i_abs = builder.fcmp_ordered(">=", in2r_abs, in2i_abs)
548
+
549
+ with builder.if_else(in2r_abs_ge_in2i_abs) as (then, otherwise):
550
+ with then:
551
+ rat = builder.fdiv(in2i, in2r)
552
+ # out.real = floor((in1r+in1i*rat)/(in2r + in2i*rat))
553
+ tmp1 = builder.fmul(in1i, rat)
554
+ tmp2 = builder.fmul(in2i, rat)
555
+ tmp3 = builder.fadd(in1r, tmp1)
556
+ tmp4 = builder.fadd(in2r, tmp2)
557
+ tmp5 = builder.fdiv(tmp3, tmp4)
558
+ out.real = np_real_floor_impl(context, builder, floor_sig, (tmp5,))
559
+ with otherwise:
560
+ rat = builder.fdiv(in2r, in2i)
561
+ # out.real = floor((in1i + in1r*rat)/(in2i + in2r*rat))
562
+ tmp1 = builder.fmul(in1r, rat)
563
+ tmp2 = builder.fmul(in2r, rat)
564
+ tmp3 = builder.fadd(in1i, tmp1)
565
+ tmp4 = builder.fadd(in2i, tmp2)
566
+ tmp5 = builder.fdiv(tmp3, tmp4)
567
+ out.real = np_real_floor_impl(context, builder, floor_sig, (tmp5,))
568
+ return out._getvalue()
569
+
570
+
571
+ ########################################################################
572
+ # numpy power funcs
573
+
574
+
575
+ def np_complex_power_impl(context, builder, sig, args):
576
+ _check_arity_and_homogeneity(sig, args, 2)
577
+
578
+ return numbers.complex_power_impl(context, builder, sig, args)
579
+
580
+
581
+ ########################################################################
582
+ # numpy float power funcs
583
+
584
+
585
+ def real_float_power_impl(context, builder, sig, args):
586
+ _check_arity_and_homogeneity(sig, args, 2)
587
+
588
+ return numbers.real_power_impl(context, builder, sig, args)
589
+
590
+
591
+ def np_complex_float_power_impl(context, builder, sig, args):
592
+ _check_arity_and_homogeneity(sig, args, 2)
593
+
594
+ return numbers.complex_power_impl(context, builder, sig, args)
595
+
596
+
597
+ ########################################################################
598
+ # numpy greatest common denominator
599
+
600
+
601
+ def np_gcd_impl(context, builder, sig, args):
602
+ _check_arity_and_homogeneity(sig, args, 2)
603
+ return mathimpl.gcd_impl(context, builder, sig, args)
604
+
605
+
606
+ ########################################################################
607
+ # numpy lowest common multiple
608
+
609
+
610
+ def np_lcm_impl(context, builder, sig, args):
611
+ xty, yty = sig.args
612
+ assert xty == yty == sig.return_type
613
+ x, y = args
614
+
615
+ def lcm(a, b):
616
+ """
617
+ Like gcd, heavily cribbed from Julia.
618
+ """
619
+ return 0 if a == 0 else abs(a * (b // np.gcd(b, a)))
620
+
621
+ res = context.compile_internal(builder, lcm, sig, args)
622
+ return impl_ret_untracked(context, builder, sig.return_type, res)
623
+
624
+
625
+ ########################################################################
626
+ # Numpy style complex sign
627
+
628
+
629
+ def np_complex_sign_impl(context, builder, sig, args):
630
+ # equivalent to complex sign in NumPy's sign
631
+ # but implemented via selects, balancing the 4 cases.
632
+ _check_arity_and_homogeneity(sig, args, 1)
633
+
634
+ if numpy_version >= (2, 0):
635
+ # NumPy >= 2.0.0
636
+ def complex_sign(z):
637
+ abs = math.hypot(z.real, z.imag)
638
+ if abs == 0:
639
+ return 0 + 0j
640
+ else:
641
+ return z / abs
642
+
643
+ res = context.compile_internal(builder, complex_sign, sig, args)
644
+ return impl_ret_untracked(context, builder, sig.return_type, res)
645
+ else:
646
+ op = args[0]
647
+ ty = sig.args[0]
648
+ result = context.make_complex(builder, ty)
649
+ float_ty = ty.underlying_float
650
+
651
+ ZERO = context.get_constant(float_ty, 0.0)
652
+ ONE = context.get_constant(float_ty, 1.0)
653
+ MINUS_ONE = context.get_constant(float_ty, -1.0)
654
+ NAN = context.get_constant(float_ty, float("nan"))
655
+
656
+ result.real = ZERO
657
+ result.imag = ZERO
658
+ cmp_sig = typing.signature(types.boolean, *[ty] * 2)
659
+ cmp_args = [op, result._getvalue()]
660
+ arg1_ge_arg2 = np_complex_ge_impl(context, builder, cmp_sig, cmp_args)
661
+ arg1_eq_arg2 = np_complex_eq_impl(context, builder, cmp_sig, cmp_args)
662
+ arg1_lt_arg2 = np_complex_lt_impl(context, builder, cmp_sig, cmp_args)
663
+
664
+ real_when_ge = builder.select(arg1_eq_arg2, ZERO, ONE)
665
+ real_when_nge = builder.select(arg1_lt_arg2, MINUS_ONE, NAN)
666
+ result.real = builder.select(arg1_ge_arg2, real_when_ge, real_when_nge)
667
+
668
+ return result._getvalue()
669
+
670
+
671
+ ########################################################################
672
+ # Numpy rint
673
+
674
+
675
+ def np_real_rint_impl(context, builder, sig, args):
676
+ _check_arity_and_homogeneity(sig, args, 1)
677
+
678
+ return mathimpl.call_fp_intrinsic(builder, "llvm.rint", args)
679
+
680
+
681
+ def np_complex_rint_impl(context, builder, sig, args):
682
+ # based on code in NumPy's funcs.inc.src
683
+ # rint of a complex number defined as rint of its real and imag
684
+ # parts
685
+ _check_arity_and_homogeneity(sig, args, 1)
686
+ ty = sig.args[0]
687
+ float_ty = ty.underlying_float
688
+ in1 = context.make_complex(builder, ty, value=args[0])
689
+ out = context.make_complex(builder, ty)
690
+
691
+ inner_sig = typing.signature(*[float_ty] * 2)
692
+ out.real = np_real_rint_impl(context, builder, inner_sig, [in1.real])
693
+ out.imag = np_real_rint_impl(context, builder, inner_sig, [in1.imag])
694
+ return out._getvalue()
695
+
696
+
697
+ ########################################################################
698
+ # NumPy exp
699
+
700
+
701
+ def np_real_exp_impl(context, builder, sig, args):
702
+ _check_arity_and_homogeneity(sig, args, 1)
703
+ return mathimpl.exp_impl(context, builder, sig, args)
704
+
705
+
706
+ def np_complex_exp_impl(context, builder, sig, args):
707
+ _check_arity_and_homogeneity(sig, args, 1)
708
+ return cmathimpl.exp_impl(context, builder, sig, args)
709
+
710
+
711
+ ########################################################################
712
+ # NumPy exp2
713
+
714
+
715
+ def np_real_exp2_impl(context, builder, sig, args):
716
+ _check_arity_and_homogeneity(sig, args, 1)
717
+
718
+ ll_ty = args[0].type
719
+ fnty = llvmlite.ir.FunctionType(
720
+ ll_ty,
721
+ [
722
+ ll_ty,
723
+ ],
724
+ )
725
+ fn = cgutils.insert_pure_function(builder.module, fnty, name="llvm.exp2")
726
+ return builder.call(fn, [args[0]])
727
+
728
+
729
+ def np_complex_exp2_impl(context, builder, sig, args):
730
+ _check_arity_and_homogeneity(sig, args, 1)
731
+ ty = sig.args[0]
732
+ float_ty = ty.underlying_float
733
+ in1 = context.make_complex(builder, ty, value=args[0])
734
+ tmp = context.make_complex(builder, ty)
735
+ loge2 = context.get_constant(float_ty, _NPY_LOGE2)
736
+ tmp.real = builder.fmul(loge2, in1.real)
737
+ tmp.imag = builder.fmul(loge2, in1.imag)
738
+ return np_complex_exp_impl(context, builder, sig, [tmp._getvalue()])
739
+
740
+
741
+ ########################################################################
742
+ # NumPy log
743
+
744
+
745
+ def np_real_log_impl(context, builder, sig, args):
746
+ _check_arity_and_homogeneity(sig, args, 1)
747
+ return mathimpl.log_impl(context, builder, sig, args)
748
+
749
+
750
+ def np_complex_log_impl(context, builder, sig, args):
751
+ _check_arity_and_homogeneity(sig, args, 1)
752
+ return cmathimpl.log_impl(context, builder, sig, args)
753
+
754
+
755
+ ########################################################################
756
+ # NumPy log2
757
+
758
+
759
+ def np_real_log2_impl(context, builder, sig, args):
760
+ _check_arity_and_homogeneity(sig, args, 1)
761
+
762
+ ll_ty = args[0].type
763
+ fnty = llvmlite.ir.FunctionType(
764
+ ll_ty,
765
+ [
766
+ ll_ty,
767
+ ],
768
+ )
769
+ fn = cgutils.insert_pure_function(builder.module, fnty, name="llvm.log2")
770
+ return builder.call(fn, [args[0]])
771
+
772
+
773
+ def np_complex_log2_impl(context, builder, sig, args):
774
+ _check_arity_and_homogeneity(sig, args, 1)
775
+
776
+ ty = sig.args[0]
777
+ float_ty = ty.underlying_float
778
+ tmp = np_complex_log_impl(context, builder, sig, args)
779
+ tmp = context.make_complex(builder, ty, value=tmp)
780
+ log2e = context.get_constant(float_ty, _NPY_LOG2E)
781
+ tmp.real = builder.fmul(log2e, tmp.real)
782
+ tmp.imag = builder.fmul(log2e, tmp.imag)
783
+ return tmp._getvalue()
784
+
785
+
786
+ ########################################################################
787
+ # NumPy log10
788
+
789
+
790
+ def np_real_log10_impl(context, builder, sig, args):
791
+ _check_arity_and_homogeneity(sig, args, 1)
792
+ return mathimpl.log10_impl(context, builder, sig, args)
793
+
794
+
795
+ def np_complex_log10_impl(context, builder, sig, args):
796
+ _check_arity_and_homogeneity(sig, args, 1)
797
+
798
+ ty = sig.args[0]
799
+ float_ty = ty.underlying_float
800
+ tmp = np_complex_log_impl(context, builder, sig, args)
801
+ tmp = context.make_complex(builder, ty, value=tmp)
802
+ log10e = context.get_constant(float_ty, _NPY_LOG10E)
803
+ tmp.real = builder.fmul(log10e, tmp.real)
804
+ tmp.imag = builder.fmul(log10e, tmp.imag)
805
+ return tmp._getvalue()
806
+
807
+
808
+ ########################################################################
809
+ # NumPy expm1
810
+
811
+
812
+ def np_real_expm1_impl(context, builder, sig, args):
813
+ _check_arity_and_homogeneity(sig, args, 1)
814
+ return mathimpl.expm1_impl(context, builder, sig, args)
815
+
816
+
817
+ def np_complex_expm1_impl(context, builder, sig, args):
818
+ # this is based on nc_expm1 in funcs.inc.src
819
+ _check_arity_and_homogeneity(sig, args, 1)
820
+
821
+ ty = sig.args[0]
822
+ float_ty = ty.underlying_float
823
+ float_unary_sig = typing.signature(*[float_ty] * 2)
824
+
825
+ MINUS_ONE = context.get_constant(float_ty, -1.0)
826
+ in1 = context.make_complex(builder, ty, value=args[0])
827
+ a = np_real_exp_impl(context, builder, float_unary_sig, [in1.real])
828
+ out = context.make_complex(builder, ty)
829
+ cos_imag = np_real_cos_impl(context, builder, float_unary_sig, [in1.imag])
830
+ sin_imag = np_real_sin_impl(context, builder, float_unary_sig, [in1.imag])
831
+ tmp = builder.fmul(a, cos_imag)
832
+ out.imag = builder.fmul(a, sin_imag)
833
+ out.real = builder.fadd(tmp, MINUS_ONE)
834
+
835
+ return out._getvalue()
836
+
837
+
838
+ ########################################################################
839
+ # NumPy log1p
840
+
841
+
842
+ def np_real_log1p_impl(context, builder, sig, args):
843
+ _check_arity_and_homogeneity(sig, args, 1)
844
+ return mathimpl.log1p_impl(context, builder, sig, args)
845
+
846
+
847
+ def np_complex_log1p_impl(context, builder, sig, args):
848
+ # base on NumPy's nc_log1p in funcs.inc.src
849
+ _check_arity_and_homogeneity(sig, args, 1)
850
+
851
+ ty = sig.args[0]
852
+ float_ty = ty.underlying_float
853
+ float_unary_sig = typing.signature(*[float_ty] * 2)
854
+ float_binary_sig = typing.signature(*[float_ty] * 3)
855
+
856
+ ONE = context.get_constant(float_ty, 1.0)
857
+ in1 = context.make_complex(builder, ty, value=args[0])
858
+ out = context.make_complex(builder, ty)
859
+ real_plus_one = builder.fadd(in1.real, ONE)
860
+ l = np_real_hypot_impl(
861
+ context, builder, float_binary_sig, [real_plus_one, in1.imag]
862
+ )
863
+ out.imag = np_real_atan2_impl(
864
+ context, builder, float_binary_sig, [in1.imag, real_plus_one]
865
+ )
866
+ out.real = np_real_log_impl(context, builder, float_unary_sig, [l])
867
+
868
+ return out._getvalue()
869
+
870
+
871
+ ########################################################################
872
+ # NumPy sqrt
873
+
874
+
875
+ def np_real_sqrt_impl(context, builder, sig, args):
876
+ _check_arity_and_homogeneity(sig, args, 1)
877
+ return mathimpl.sqrt_impl(context, builder, sig, args)
878
+
879
+
880
+ def np_complex_sqrt_impl(context, builder, sig, args):
881
+ _check_arity_and_homogeneity(sig, args, 1)
882
+ return cmathimpl.sqrt_impl(context, builder, sig, args)
883
+
884
+
885
+ ########################################################################
886
+ # NumPy square
887
+
888
+
889
+ def np_int_square_impl(context, builder, sig, args):
890
+ _check_arity_and_homogeneity(sig, args, 1)
891
+ return builder.mul(args[0], args[0])
892
+
893
+
894
+ def np_real_square_impl(context, builder, sig, args):
895
+ _check_arity_and_homogeneity(sig, args, 1)
896
+ return builder.fmul(args[0], args[0])
897
+
898
+
899
+ def np_complex_square_impl(context, builder, sig, args):
900
+ _check_arity_and_homogeneity(sig, args, 1)
901
+ binary_sig = typing.signature(*[sig.return_type] * 3)
902
+ return numbers.complex_mul_impl(
903
+ context, builder, binary_sig, [args[0], args[0]]
904
+ )
905
+
906
+
907
+ ########################################################################
908
+ # NumPy cbrt
909
+
910
+
911
+ def np_real_cbrt_impl(context, builder, sig, args):
912
+ _check_arity_and_homogeneity(sig, args, 1)
913
+
914
+ # We enable fastmath here to force np.power(x, 1/3) to generate a
915
+ # call to libm cbrt function
916
+ @register_jitable(fastmath=True)
917
+ def cbrt(x):
918
+ if x < 0:
919
+ return -np.power(-x, 1.0 / 3.0)
920
+ else:
921
+ return np.power(x, 1.0 / 3.0)
922
+
923
+ def _cbrt(x):
924
+ if np.isnan(x):
925
+ return np.nan
926
+ return cbrt(x)
927
+
928
+ return context.compile_internal(builder, _cbrt, sig, args)
929
+
930
+
931
+ ########################################################################
932
+ # NumPy reciprocal
933
+
934
+
935
+ def np_int_reciprocal_impl(context, builder, sig, args):
936
+ # based on the implementation in loops.c.src
937
+ # integer versions for reciprocal are performed via promotion
938
+ # using double, and then converted back to the type
939
+ _check_arity_and_homogeneity(sig, args, 1)
940
+ ty = sig.return_type
941
+
942
+ in_as_float = context.cast(builder, args[0], ty, types.float64)
943
+ ONE = context.get_constant(types.float64, 1)
944
+ result_as_float = builder.fdiv(ONE, in_as_float)
945
+ return context.cast(builder, result_as_float, types.float64, ty)
946
+
947
+
948
+ def np_real_reciprocal_impl(context, builder, sig, args):
949
+ _check_arity_and_homogeneity(sig, args, 1)
950
+ ONE = context.get_constant(sig.return_type, 1.0)
951
+ return builder.fdiv(ONE, args[0])
952
+
953
+
954
+ def np_complex_reciprocal_impl(context, builder, sig, args):
955
+ # based on the implementation in loops.c.src
956
+ # Basically the same Smith method used for division, but with
957
+ # the numerator substituted by 1.0
958
+ _check_arity_and_homogeneity(sig, args, 1)
959
+
960
+ ty = sig.args[0]
961
+ float_ty = ty.underlying_float
962
+
963
+ ZERO = context.get_constant(float_ty, 0.0)
964
+ ONE = context.get_constant(float_ty, 1.0)
965
+ in1 = context.make_complex(builder, ty, value=args[0])
966
+ out = context.make_complex(builder, ty)
967
+ in1r = in1.real
968
+ in1i = in1.imag
969
+ in1r_abs = _fabs(context, builder, in1r)
970
+ in1i_abs = _fabs(context, builder, in1i)
971
+ in1i_abs_le_in1r_abs = builder.fcmp_ordered("<=", in1i_abs, in1r_abs)
972
+
973
+ with builder.if_else(in1i_abs_le_in1r_abs) as (then, otherwise):
974
+ with then:
975
+ r = builder.fdiv(in1i, in1r)
976
+ tmp0 = builder.fmul(in1i, r)
977
+ d = builder.fadd(in1r, tmp0)
978
+ inv_d = builder.fdiv(ONE, d)
979
+ minus_r = builder.fsub(ZERO, r)
980
+ out.real = inv_d
981
+ out.imag = builder.fmul(minus_r, inv_d)
982
+ with otherwise:
983
+ r = builder.fdiv(in1r, in1i)
984
+ tmp0 = builder.fmul(in1r, r)
985
+ d = builder.fadd(tmp0, in1i)
986
+ inv_d = builder.fdiv(ONE, d)
987
+ out.real = builder.fmul(r, inv_d)
988
+ out.imag = builder.fsub(ZERO, inv_d)
989
+
990
+ return out._getvalue()
991
+
992
+
993
+ ########################################################################
994
+ # NumPy sin
995
+
996
+
997
+ def np_real_sin_impl(context, builder, sig, args):
998
+ _check_arity_and_homogeneity(sig, args, 1)
999
+ return mathimpl.sin_impl(context, builder, sig, args)
1000
+
1001
+
1002
+ def np_complex_sin_impl(context, builder, sig, args):
1003
+ _check_arity_and_homogeneity(sig, args, 1)
1004
+ return cmathimpl.sin_impl(context, builder, sig, args)
1005
+
1006
+
1007
+ ########################################################################
1008
+ # NumPy cos
1009
+
1010
+
1011
+ def np_real_cos_impl(context, builder, sig, args):
1012
+ _check_arity_and_homogeneity(sig, args, 1)
1013
+ return mathimpl.cos_impl(context, builder, sig, args)
1014
+
1015
+
1016
+ def np_complex_cos_impl(context, builder, sig, args):
1017
+ _check_arity_and_homogeneity(sig, args, 1)
1018
+ return cmathimpl.cos_impl(context, builder, sig, args)
1019
+
1020
+
1021
+ ########################################################################
1022
+ # NumPy tan
1023
+
1024
+
1025
+ def np_real_tan_impl(context, builder, sig, args):
1026
+ _check_arity_and_homogeneity(sig, args, 1)
1027
+ return mathimpl.tan_impl(context, builder, sig, args)
1028
+
1029
+
1030
+ ########################################################################
1031
+ # NumPy asin
1032
+
1033
+
1034
+ def np_real_asin_impl(context, builder, sig, args):
1035
+ _check_arity_and_homogeneity(sig, args, 1)
1036
+ return mathimpl.asin_impl(context, builder, sig, args)
1037
+
1038
+
1039
+ ########################################################################
1040
+ # NumPy acos
1041
+
1042
+
1043
+ def np_real_acos_impl(context, builder, sig, args):
1044
+ _check_arity_and_homogeneity(sig, args, 1)
1045
+ return mathimpl.acos_impl(context, builder, sig, args)
1046
+
1047
+
1048
+ ########################################################################
1049
+ # NumPy atan
1050
+
1051
+
1052
+ def np_real_atan_impl(context, builder, sig, args):
1053
+ _check_arity_and_homogeneity(sig, args, 1)
1054
+ return mathimpl.atan_impl(context, builder, sig, args)
1055
+
1056
+
1057
+ ########################################################################
1058
+ # NumPy atan2
1059
+
1060
+
1061
+ def np_real_atan2_impl(context, builder, sig, args):
1062
+ _check_arity_and_homogeneity(sig, args, 2)
1063
+ return mathimpl.atan2_float_impl(context, builder, sig, args)
1064
+
1065
+
1066
+ ########################################################################
1067
+ # NumPy hypot
1068
+
1069
+
1070
+ def np_real_hypot_impl(context, builder, sig, args):
1071
+ _check_arity_and_homogeneity(sig, args, 2)
1072
+ return mathimpl.hypot_float_impl(context, builder, sig, args)
1073
+
1074
+
1075
+ ########################################################################
1076
+ # NumPy sinh
1077
+
1078
+
1079
+ def np_real_sinh_impl(context, builder, sig, args):
1080
+ _check_arity_and_homogeneity(sig, args, 1)
1081
+ return mathimpl.sinh_impl(context, builder, sig, args)
1082
+
1083
+
1084
+ def np_complex_sinh_impl(context, builder, sig, args):
1085
+ # npymath does not provide a complex sinh. The code in funcs.inc.src
1086
+ # is translated here...
1087
+ _check_arity_and_homogeneity(sig, args, 1)
1088
+
1089
+ ty = sig.args[0]
1090
+ fty = ty.underlying_float
1091
+ fsig1 = typing.signature(*[fty] * 2)
1092
+ x = context.make_complex(builder, ty, args[0])
1093
+ out = context.make_complex(builder, ty)
1094
+ xr = x.real
1095
+ xi = x.imag
1096
+
1097
+ sxi = np_real_sin_impl(context, builder, fsig1, [xi])
1098
+ shxr = np_real_sinh_impl(context, builder, fsig1, [xr])
1099
+ cxi = np_real_cos_impl(context, builder, fsig1, [xi])
1100
+ chxr = np_real_cosh_impl(context, builder, fsig1, [xr])
1101
+
1102
+ out.real = builder.fmul(cxi, shxr)
1103
+ out.imag = builder.fmul(sxi, chxr)
1104
+
1105
+ return out._getvalue()
1106
+
1107
+
1108
+ ########################################################################
1109
+ # NumPy cosh
1110
+
1111
+
1112
+ def np_real_cosh_impl(context, builder, sig, args):
1113
+ _check_arity_and_homogeneity(sig, args, 1)
1114
+ return mathimpl.cosh_impl(context, builder, sig, args)
1115
+
1116
+
1117
+ def np_complex_cosh_impl(context, builder, sig, args):
1118
+ # npymath does not provide a complex cosh. The code in funcs.inc.src
1119
+ # is translated here...
1120
+ _check_arity_and_homogeneity(sig, args, 1)
1121
+
1122
+ ty = sig.args[0]
1123
+ fty = ty.underlying_float
1124
+ fsig1 = typing.signature(*[fty] * 2)
1125
+ x = context.make_complex(builder, ty, args[0])
1126
+ out = context.make_complex(builder, ty)
1127
+ xr = x.real
1128
+ xi = x.imag
1129
+
1130
+ cxi = np_real_cos_impl(context, builder, fsig1, [xi])
1131
+ chxr = np_real_cosh_impl(context, builder, fsig1, [xr])
1132
+ sxi = np_real_sin_impl(context, builder, fsig1, [xi])
1133
+ shxr = np_real_sinh_impl(context, builder, fsig1, [xr])
1134
+
1135
+ out.real = builder.fmul(cxi, chxr)
1136
+ out.imag = builder.fmul(sxi, shxr)
1137
+
1138
+ return out._getvalue()
1139
+
1140
+
1141
+ ########################################################################
1142
+ # NumPy tanh
1143
+
1144
+
1145
+ def np_real_tanh_impl(context, builder, sig, args):
1146
+ _check_arity_and_homogeneity(sig, args, 1)
1147
+ return mathimpl.tanh_impl(context, builder, sig, args)
1148
+
1149
+
1150
+ def np_complex_tanh_impl(context, builder, sig, args):
1151
+ # npymath does not provide complex tan functions. The code
1152
+ # in funcs.inc.src for tanh is translated here...
1153
+ _check_arity_and_homogeneity(sig, args, 1)
1154
+
1155
+ ty = sig.args[0]
1156
+ fty = ty.underlying_float
1157
+ fsig1 = typing.signature(*[fty] * 2)
1158
+ ONE = context.get_constant(fty, 1.0)
1159
+ x = context.make_complex(builder, ty, args[0])
1160
+ out = context.make_complex(builder, ty)
1161
+
1162
+ xr = x.real
1163
+ xi = x.imag
1164
+ si = np_real_sin_impl(context, builder, fsig1, [xi])
1165
+ ci = np_real_cos_impl(context, builder, fsig1, [xi])
1166
+ shr = np_real_sinh_impl(context, builder, fsig1, [xr])
1167
+ chr_ = np_real_cosh_impl(context, builder, fsig1, [xr])
1168
+ rs = builder.fmul(ci, shr)
1169
+ is_ = builder.fmul(si, chr_)
1170
+ rc = builder.fmul(ci, chr_)
1171
+ ic = builder.fmul(si, shr) # note: opposite sign from code in funcs.inc.src
1172
+ sqr_rc = builder.fmul(rc, rc)
1173
+ sqr_ic = builder.fmul(ic, ic)
1174
+ d = builder.fadd(sqr_rc, sqr_ic)
1175
+ inv_d = builder.fdiv(ONE, d)
1176
+ rs_rc = builder.fmul(rs, rc)
1177
+ is_ic = builder.fmul(is_, ic)
1178
+ is_rc = builder.fmul(is_, rc)
1179
+ rs_ic = builder.fmul(rs, ic)
1180
+ numr = builder.fadd(rs_rc, is_ic)
1181
+ numi = builder.fsub(is_rc, rs_ic)
1182
+ out.real = builder.fmul(numr, inv_d)
1183
+ out.imag = builder.fmul(numi, inv_d)
1184
+
1185
+ return out._getvalue()
1186
+
1187
+
1188
+ ########################################################################
1189
+ # NumPy asinh
1190
+
1191
+
1192
+ def np_real_asinh_impl(context, builder, sig, args):
1193
+ _check_arity_and_homogeneity(sig, args, 1)
1194
+ return mathimpl.asinh_impl(context, builder, sig, args)
1195
+
1196
+
1197
+ ########################################################################
1198
+ # NumPy acosh
1199
+
1200
+
1201
+ def np_real_acosh_impl(context, builder, sig, args):
1202
+ _check_arity_and_homogeneity(sig, args, 1)
1203
+ return mathimpl.acosh_impl(context, builder, sig, args)
1204
+
1205
+
1206
+ def np_complex_acosh_impl(context, builder, sig, args):
1207
+ # npymath does not provide a complex acosh. The code in funcs.inc.src
1208
+ # is translated here...
1209
+ # log(x + sqrt(x+1) * sqrt(x-1))
1210
+ _check_arity_and_homogeneity(sig, args, 1)
1211
+
1212
+ ty = sig.args[0]
1213
+ csig2 = typing.signature(*[ty] * 3)
1214
+
1215
+ ONE = context.get_constant_generic(builder, ty, 1.0 + 0.0j)
1216
+ x = args[0]
1217
+
1218
+ x_plus_one = numbers.complex_add_impl(context, builder, csig2, [x, ONE])
1219
+ x_minus_one = numbers.complex_sub_impl(context, builder, csig2, [x, ONE])
1220
+ sqrt_x_plus_one = np_complex_sqrt_impl(context, builder, sig, [x_plus_one])
1221
+ sqrt_x_minus_one = np_complex_sqrt_impl(
1222
+ context, builder, sig, [x_minus_one]
1223
+ )
1224
+ prod_sqrt = numbers.complex_mul_impl(
1225
+ context, builder, csig2, [sqrt_x_plus_one, sqrt_x_minus_one]
1226
+ )
1227
+ log_arg = numbers.complex_add_impl(context, builder, csig2, [x, prod_sqrt])
1228
+
1229
+ return np_complex_log_impl(context, builder, sig, [log_arg])
1230
+
1231
+
1232
+ ########################################################################
1233
+ # NumPy atanh
1234
+
1235
+
1236
+ def np_real_atanh_impl(context, builder, sig, args):
1237
+ _check_arity_and_homogeneity(sig, args, 1)
1238
+ return mathimpl.atanh_impl(context, builder, sig, args)
1239
+
1240
+
1241
+ ########################################################################
1242
+ # NumPy floor
1243
+
1244
+
1245
+ def np_real_floor_impl(context, builder, sig, args):
1246
+ _check_arity_and_homogeneity(sig, args, 1)
1247
+
1248
+ return mathimpl.call_fp_intrinsic(builder, "llvm.floor", args)
1249
+
1250
+
1251
+ ########################################################################
1252
+ # NumPy ceil
1253
+
1254
+
1255
+ def np_real_ceil_impl(context, builder, sig, args):
1256
+ _check_arity_and_homogeneity(sig, args, 1)
1257
+
1258
+ return mathimpl.call_fp_intrinsic(builder, "llvm.ceil", args)
1259
+
1260
+
1261
+ ########################################################################
1262
+ # NumPy trunc
1263
+
1264
+
1265
+ def np_real_trunc_impl(context, builder, sig, args):
1266
+ _check_arity_and_homogeneity(sig, args, 1)
1267
+
1268
+ return mathimpl.call_fp_intrinsic(builder, "llvm.trunc", args)
1269
+
1270
+
1271
+ ########################################################################
1272
+ # NumPy fabs
1273
+
1274
+
1275
+ def np_real_fabs_impl(context, builder, sig, args):
1276
+ _check_arity_and_homogeneity(sig, args, 1)
1277
+
1278
+ return mathimpl.call_fp_intrinsic(builder, "llvm.fabs", args)
1279
+
1280
+
1281
+ ########################################################################
1282
+ # NumPy style predicates
1283
+
1284
+
1285
+ # For real and integer types rely on numbers... but complex ordering in
1286
+ # NumPy is lexicographic (while Python does not provide ordering).
1287
+ def np_complex_ge_impl(context, builder, sig, args):
1288
+ # equivalent to macro CGE in NumPy's loops.c.src
1289
+ # ((xr > yr && !npy_isnan(xi) && !npy_isnan(yi)) || (xr == yr && xi >= yi))
1290
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1291
+
1292
+ ty = sig.args[0]
1293
+ in1, in2 = [context.make_complex(builder, ty, value=arg) for arg in args]
1294
+ xr = in1.real
1295
+ xi = in1.imag
1296
+ yr = in2.real
1297
+ yi = in2.imag
1298
+
1299
+ xr_gt_yr = builder.fcmp_ordered(">", xr, yr)
1300
+ no_nan_xi_yi = builder.fcmp_ordered("ord", xi, yi)
1301
+ xr_eq_yr = builder.fcmp_ordered("==", xr, yr)
1302
+ xi_ge_yi = builder.fcmp_ordered(">=", xi, yi)
1303
+ first_term = builder.and_(xr_gt_yr, no_nan_xi_yi)
1304
+ second_term = builder.and_(xr_eq_yr, xi_ge_yi)
1305
+ return builder.or_(first_term, second_term)
1306
+
1307
+
1308
+ def np_complex_le_impl(context, builder, sig, args):
1309
+ # equivalent to macro CLE in NumPy's loops.c.src
1310
+ # ((xr < yr && !npy_isnan(xi) && !npy_isnan(yi)) || (xr == yr && xi <= yi))
1311
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1312
+
1313
+ ty = sig.args[0]
1314
+ in1, in2 = [context.make_complex(builder, ty, value=arg) for arg in args]
1315
+ xr = in1.real
1316
+ xi = in1.imag
1317
+ yr = in2.real
1318
+ yi = in2.imag
1319
+
1320
+ xr_lt_yr = builder.fcmp_ordered("<", xr, yr)
1321
+ no_nan_xi_yi = builder.fcmp_ordered("ord", xi, yi)
1322
+ xr_eq_yr = builder.fcmp_ordered("==", xr, yr)
1323
+ xi_le_yi = builder.fcmp_ordered("<=", xi, yi)
1324
+ first_term = builder.and_(xr_lt_yr, no_nan_xi_yi)
1325
+ second_term = builder.and_(xr_eq_yr, xi_le_yi)
1326
+ return builder.or_(first_term, second_term)
1327
+
1328
+
1329
+ def np_complex_gt_impl(context, builder, sig, args):
1330
+ # equivalent to macro CGT in NumPy's loops.c.src
1331
+ # ((xr > yr && !npy_isnan(xi) && !npy_isnan(yi)) || (xr == yr && xi > yi))
1332
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1333
+
1334
+ ty = sig.args[0]
1335
+ in1, in2 = [context.make_complex(builder, ty, value=arg) for arg in args]
1336
+ xr = in1.real
1337
+ xi = in1.imag
1338
+ yr = in2.real
1339
+ yi = in2.imag
1340
+
1341
+ xr_gt_yr = builder.fcmp_ordered(">", xr, yr)
1342
+ no_nan_xi_yi = builder.fcmp_ordered("ord", xi, yi)
1343
+ xr_eq_yr = builder.fcmp_ordered("==", xr, yr)
1344
+ xi_gt_yi = builder.fcmp_ordered(">", xi, yi)
1345
+ first_term = builder.and_(xr_gt_yr, no_nan_xi_yi)
1346
+ second_term = builder.and_(xr_eq_yr, xi_gt_yi)
1347
+ return builder.or_(first_term, second_term)
1348
+
1349
+
1350
+ def np_complex_lt_impl(context, builder, sig, args):
1351
+ # equivalent to macro CLT in NumPy's loops.c.src
1352
+ # ((xr < yr && !npy_isnan(xi) && !npy_isnan(yi)) || (xr == yr && xi < yi))
1353
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1354
+
1355
+ ty = sig.args[0]
1356
+ in1, in2 = [context.make_complex(builder, ty, value=arg) for arg in args]
1357
+ xr = in1.real
1358
+ xi = in1.imag
1359
+ yr = in2.real
1360
+ yi = in2.imag
1361
+
1362
+ xr_lt_yr = builder.fcmp_ordered("<", xr, yr)
1363
+ no_nan_xi_yi = builder.fcmp_ordered("ord", xi, yi)
1364
+ xr_eq_yr = builder.fcmp_ordered("==", xr, yr)
1365
+ xi_lt_yi = builder.fcmp_ordered("<", xi, yi)
1366
+ first_term = builder.and_(xr_lt_yr, no_nan_xi_yi)
1367
+ second_term = builder.and_(xr_eq_yr, xi_lt_yi)
1368
+ return builder.or_(first_term, second_term)
1369
+
1370
+
1371
+ def np_complex_eq_impl(context, builder, sig, args):
1372
+ # equivalent to macro CEQ in NumPy's loops.c.src
1373
+ # (xr == yr && xi == yi)
1374
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1375
+
1376
+ ty = sig.args[0]
1377
+ in1, in2 = [context.make_complex(builder, ty, value=arg) for arg in args]
1378
+ xr = in1.real
1379
+ xi = in1.imag
1380
+ yr = in2.real
1381
+ yi = in2.imag
1382
+
1383
+ xr_eq_yr = builder.fcmp_ordered("==", xr, yr)
1384
+ xi_eq_yi = builder.fcmp_ordered("==", xi, yi)
1385
+ return builder.and_(xr_eq_yr, xi_eq_yi)
1386
+
1387
+
1388
+ def np_complex_ne_impl(context, builder, sig, args):
1389
+ # equivalent to macro CNE in NumPy's loops.c.src
1390
+ # (xr != yr || xi != yi)
1391
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1392
+
1393
+ ty = sig.args[0]
1394
+ in1, in2 = [context.make_complex(builder, ty, value=arg) for arg in args]
1395
+ xr = in1.real
1396
+ xi = in1.imag
1397
+ yr = in2.real
1398
+ yi = in2.imag
1399
+
1400
+ xr_ne_yr = builder.fcmp_unordered("!=", xr, yr)
1401
+ xi_ne_yi = builder.fcmp_unordered("!=", xi, yi)
1402
+ return builder.or_(xr_ne_yr, xi_ne_yi)
1403
+
1404
+
1405
+ ########################################################################
1406
+ # NumPy logical algebra
1407
+
1408
+ # these are made generic for all types for now, assuming that
1409
+ # cgutils.is_true works in the underlying types.
1410
+
1411
+
1412
+ def _complex_is_true(context, builder, ty, val):
1413
+ complex_val = context.make_complex(builder, ty, value=val)
1414
+ re_true = cgutils.is_true(builder, complex_val.real)
1415
+ im_true = cgutils.is_true(builder, complex_val.imag)
1416
+ return builder.or_(re_true, im_true)
1417
+
1418
+
1419
+ def np_logical_and_impl(context, builder, sig, args):
1420
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1421
+ a = cgutils.is_true(builder, args[0])
1422
+ b = cgutils.is_true(builder, args[1])
1423
+ return builder.and_(a, b)
1424
+
1425
+
1426
+ def np_complex_logical_and_impl(context, builder, sig, args):
1427
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1428
+ a = _complex_is_true(context, builder, sig.args[0], args[0])
1429
+ b = _complex_is_true(context, builder, sig.args[1], args[1])
1430
+ return builder.and_(a, b)
1431
+
1432
+
1433
+ def np_logical_or_impl(context, builder, sig, args):
1434
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1435
+ a = cgutils.is_true(builder, args[0])
1436
+ b = cgutils.is_true(builder, args[1])
1437
+ return builder.or_(a, b)
1438
+
1439
+
1440
+ def np_complex_logical_or_impl(context, builder, sig, args):
1441
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1442
+ a = _complex_is_true(context, builder, sig.args[0], args[0])
1443
+ b = _complex_is_true(context, builder, sig.args[1], args[1])
1444
+ return builder.or_(a, b)
1445
+
1446
+
1447
+ def np_logical_xor_impl(context, builder, sig, args):
1448
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1449
+ a = cgutils.is_true(builder, args[0])
1450
+ b = cgutils.is_true(builder, args[1])
1451
+ return builder.xor(a, b)
1452
+
1453
+
1454
+ def np_complex_logical_xor_impl(context, builder, sig, args):
1455
+ _check_arity_and_homogeneity(sig, args, 2, return_type=types.boolean)
1456
+ a = _complex_is_true(context, builder, sig.args[0], args[0])
1457
+ b = _complex_is_true(context, builder, sig.args[1], args[1])
1458
+ return builder.xor(a, b)
1459
+
1460
+
1461
+ def np_logical_not_impl(context, builder, sig, args):
1462
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1463
+ return cgutils.is_false(builder, args[0])
1464
+
1465
+
1466
+ def np_complex_logical_not_impl(context, builder, sig, args):
1467
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1468
+ a = _complex_is_true(context, builder, sig.args[0], args[0])
1469
+ return builder.not_(a)
1470
+
1471
+
1472
+ ########################################################################
1473
+ # NumPy style max/min
1474
+ #
1475
+ # There are 2 different sets of functions to perform max and min in
1476
+ # NumPy: maximum/minimum and fmax/fmin.
1477
+ # Both differ in the way NaNs are handled, so the actual differences
1478
+ # come in action only on float/complex numbers. The functions used for
1479
+ # integers is shared. For booleans maximum is equivalent to or, and
1480
+ # minimum is equivalent to and. Datetime support will go elsewhere.
1481
+
1482
+
1483
+ def np_int_smax_impl(context, builder, sig, args):
1484
+ _check_arity_and_homogeneity(sig, args, 2)
1485
+ arg1, arg2 = args
1486
+ arg1_sge_arg2 = builder.icmp_signed(">=", arg1, arg2)
1487
+ return builder.select(arg1_sge_arg2, arg1, arg2)
1488
+
1489
+
1490
+ def np_int_umax_impl(context, builder, sig, args):
1491
+ _check_arity_and_homogeneity(sig, args, 2)
1492
+ arg1, arg2 = args
1493
+ arg1_uge_arg2 = builder.icmp_unsigned(">=", arg1, arg2)
1494
+ return builder.select(arg1_uge_arg2, arg1, arg2)
1495
+
1496
+
1497
+ def np_real_maximum_impl(context, builder, sig, args):
1498
+ # maximum prefers nan (tries to return a nan).
1499
+ _check_arity_and_homogeneity(sig, args, 2)
1500
+
1501
+ arg1, arg2 = args
1502
+ arg1_nan = builder.fcmp_unordered("uno", arg1, arg1)
1503
+ any_nan = builder.fcmp_unordered("uno", arg1, arg2)
1504
+ nan_result = builder.select(arg1_nan, arg1, arg2)
1505
+
1506
+ arg1_ge_arg2 = builder.fcmp_ordered(">=", arg1, arg2)
1507
+ non_nan_result = builder.select(arg1_ge_arg2, arg1, arg2)
1508
+
1509
+ return builder.select(any_nan, nan_result, non_nan_result)
1510
+
1511
+
1512
+ def np_real_fmax_impl(context, builder, sig, args):
1513
+ # fmax prefers non-nan (tries to return a non-nan).
1514
+ _check_arity_and_homogeneity(sig, args, 2)
1515
+
1516
+ arg1, arg2 = args
1517
+ arg2_nan = builder.fcmp_unordered("uno", arg2, arg2)
1518
+ any_nan = builder.fcmp_unordered("uno", arg1, arg2)
1519
+ nan_result = builder.select(arg2_nan, arg1, arg2)
1520
+
1521
+ arg1_ge_arg2 = builder.fcmp_ordered(">=", arg1, arg2)
1522
+ non_nan_result = builder.select(arg1_ge_arg2, arg1, arg2)
1523
+
1524
+ return builder.select(any_nan, nan_result, non_nan_result)
1525
+
1526
+
1527
+ def np_complex_maximum_impl(context, builder, sig, args):
1528
+ # maximum prefers nan (tries to return a nan).
1529
+ # There is an extra caveat with complex numbers, as there is more
1530
+ # than one type of nan. NumPy's docs state that the nan in the
1531
+ # first argument is returned when both arguments are nans.
1532
+ # If only one nan is found, that nan is returned.
1533
+ _check_arity_and_homogeneity(sig, args, 2)
1534
+ ty = sig.args[0]
1535
+ bc_sig = typing.signature(types.boolean, ty)
1536
+ bcc_sig = typing.signature(types.boolean, *[ty] * 2)
1537
+ arg1, arg2 = args
1538
+ arg1_nan = np_complex_isnan_impl(context, builder, bc_sig, [arg1])
1539
+ arg2_nan = np_complex_isnan_impl(context, builder, bc_sig, [arg2])
1540
+ any_nan = builder.or_(arg1_nan, arg2_nan)
1541
+ nan_result = builder.select(arg1_nan, arg1, arg2)
1542
+
1543
+ arg1_ge_arg2 = np_complex_ge_impl(context, builder, bcc_sig, args)
1544
+ non_nan_result = builder.select(arg1_ge_arg2, arg1, arg2)
1545
+
1546
+ return builder.select(any_nan, nan_result, non_nan_result)
1547
+
1548
+
1549
+ def np_complex_fmax_impl(context, builder, sig, args):
1550
+ # fmax prefers non-nan (tries to return a non-nan).
1551
+ # There is an extra caveat with complex numbers, as there is more
1552
+ # than one type of nan. NumPy's docs state that the nan in the
1553
+ # first argument is returned when both arguments are nans.
1554
+ _check_arity_and_homogeneity(sig, args, 2)
1555
+ ty = sig.args[0]
1556
+ bc_sig = typing.signature(types.boolean, ty)
1557
+ bcc_sig = typing.signature(types.boolean, *[ty] * 2)
1558
+ arg1, arg2 = args
1559
+ arg1_nan = np_complex_isnan_impl(context, builder, bc_sig, [arg1])
1560
+ arg2_nan = np_complex_isnan_impl(context, builder, bc_sig, [arg2])
1561
+ any_nan = builder.or_(arg1_nan, arg2_nan)
1562
+ nan_result = builder.select(arg2_nan, arg1, arg2)
1563
+
1564
+ arg1_ge_arg2 = np_complex_ge_impl(context, builder, bcc_sig, args)
1565
+ non_nan_result = builder.select(arg1_ge_arg2, arg1, arg2)
1566
+
1567
+ return builder.select(any_nan, nan_result, non_nan_result)
1568
+
1569
+
1570
+ def np_int_smin_impl(context, builder, sig, args):
1571
+ _check_arity_and_homogeneity(sig, args, 2)
1572
+ arg1, arg2 = args
1573
+ arg1_sle_arg2 = builder.icmp_signed("<=", arg1, arg2)
1574
+ return builder.select(arg1_sle_arg2, arg1, arg2)
1575
+
1576
+
1577
+ def np_int_umin_impl(context, builder, sig, args):
1578
+ _check_arity_and_homogeneity(sig, args, 2)
1579
+ arg1, arg2 = args
1580
+ arg1_ule_arg2 = builder.icmp_unsigned("<=", arg1, arg2)
1581
+ return builder.select(arg1_ule_arg2, arg1, arg2)
1582
+
1583
+
1584
+ def np_real_minimum_impl(context, builder, sig, args):
1585
+ # minimum prefers nan (tries to return a nan).
1586
+ _check_arity_and_homogeneity(sig, args, 2)
1587
+
1588
+ arg1, arg2 = args
1589
+ arg1_nan = builder.fcmp_unordered("uno", arg1, arg1)
1590
+ any_nan = builder.fcmp_unordered("uno", arg1, arg2)
1591
+ nan_result = builder.select(arg1_nan, arg1, arg2)
1592
+
1593
+ arg1_le_arg2 = builder.fcmp_ordered("<=", arg1, arg2)
1594
+ non_nan_result = builder.select(arg1_le_arg2, arg1, arg2)
1595
+
1596
+ return builder.select(any_nan, nan_result, non_nan_result)
1597
+
1598
+
1599
+ def np_real_fmin_impl(context, builder, sig, args):
1600
+ # fmin prefers non-nan (tries to return a non-nan).
1601
+ _check_arity_and_homogeneity(sig, args, 2)
1602
+
1603
+ arg1, arg2 = args
1604
+ arg1_nan = builder.fcmp_unordered("uno", arg1, arg1)
1605
+ any_nan = builder.fcmp_unordered("uno", arg1, arg2)
1606
+ nan_result = builder.select(arg1_nan, arg2, arg1)
1607
+
1608
+ arg1_le_arg2 = builder.fcmp_ordered("<=", arg1, arg2)
1609
+ non_nan_result = builder.select(arg1_le_arg2, arg1, arg2)
1610
+
1611
+ return builder.select(any_nan, nan_result, non_nan_result)
1612
+
1613
+
1614
+ def np_complex_minimum_impl(context, builder, sig, args):
1615
+ # minimum prefers nan (tries to return a nan).
1616
+ # There is an extra caveat with complex numbers, as there is more
1617
+ # than one type of nan. NumPy's docs state that the nan in the
1618
+ # first argument is returned when both arguments are nans.
1619
+ # If only one nan is found, that nan is returned.
1620
+ _check_arity_and_homogeneity(sig, args, 2)
1621
+ ty = sig.args[0]
1622
+ bc_sig = typing.signature(types.boolean, ty)
1623
+ bcc_sig = typing.signature(types.boolean, *[ty] * 2)
1624
+ arg1, arg2 = args
1625
+ arg1_nan = np_complex_isnan_impl(context, builder, bc_sig, [arg1])
1626
+ arg2_nan = np_complex_isnan_impl(context, builder, bc_sig, [arg2])
1627
+ any_nan = builder.or_(arg1_nan, arg2_nan)
1628
+ nan_result = builder.select(arg1_nan, arg1, arg2)
1629
+
1630
+ arg1_le_arg2 = np_complex_le_impl(context, builder, bcc_sig, args)
1631
+ non_nan_result = builder.select(arg1_le_arg2, arg1, arg2)
1632
+
1633
+ return builder.select(any_nan, nan_result, non_nan_result)
1634
+
1635
+
1636
+ def np_complex_fmin_impl(context, builder, sig, args):
1637
+ # fmin prefers non-nan (tries to return a non-nan).
1638
+ # There is an extra caveat with complex numbers, as there is more
1639
+ # than one type of nan. NumPy's docs state that the nan in the
1640
+ # first argument is returned when both arguments are nans.
1641
+ _check_arity_and_homogeneity(sig, args, 2)
1642
+ ty = sig.args[0]
1643
+ bc_sig = typing.signature(types.boolean, ty)
1644
+ bcc_sig = typing.signature(types.boolean, *[ty] * 2)
1645
+ arg1, arg2 = args
1646
+ arg1_nan = np_complex_isnan_impl(context, builder, bc_sig, [arg1])
1647
+ arg2_nan = np_complex_isnan_impl(context, builder, bc_sig, [arg2])
1648
+ any_nan = builder.or_(arg1_nan, arg2_nan)
1649
+ nan_result = builder.select(arg2_nan, arg1, arg2)
1650
+
1651
+ arg1_le_arg2 = np_complex_le_impl(context, builder, bcc_sig, args)
1652
+ non_nan_result = builder.select(arg1_le_arg2, arg1, arg2)
1653
+
1654
+ return builder.select(any_nan, nan_result, non_nan_result)
1655
+
1656
+
1657
+ ########################################################################
1658
+ # NumPy floating point misc
1659
+
1660
+
1661
+ def np_int_isnan_impl(context, builder, sig, args):
1662
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1663
+ return cgutils.false_bit
1664
+
1665
+
1666
+ def np_real_isnan_impl(context, builder, sig, args):
1667
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1668
+ return mathimpl.is_nan(builder, args[0])
1669
+
1670
+
1671
+ def np_complex_isnan_impl(context, builder, sig, args):
1672
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1673
+
1674
+ (x,) = args
1675
+ (ty,) = sig.args
1676
+ complex_val = context.make_complex(builder, ty, value=x)
1677
+ return cmathimpl.is_nan(builder, complex_val)
1678
+
1679
+
1680
+ def np_int_isfinite_impl(context, builder, sig, args):
1681
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1682
+ return cgutils.true_bit
1683
+
1684
+
1685
+ def np_datetime_isfinite_impl(context, builder, sig, args):
1686
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1687
+ return builder.icmp_unsigned("!=", args[0], npdatetime.NAT)
1688
+
1689
+
1690
+ def np_datetime_isnat_impl(context, builder, sig, args):
1691
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1692
+ return builder.icmp_signed("==", args[0], npdatetime.NAT)
1693
+
1694
+
1695
+ def np_real_isfinite_impl(context, builder, sig, args):
1696
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1697
+ return mathimpl.is_finite(builder, args[0])
1698
+
1699
+
1700
+ def np_complex_isfinite_impl(context, builder, sig, args):
1701
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1702
+ (x,) = args
1703
+ (ty,) = sig.args
1704
+ complex_val = context.make_complex(builder, ty, value=x)
1705
+ return cmathimpl.is_finite(builder, complex_val)
1706
+
1707
+
1708
+ def np_int_isinf_impl(context, builder, sig, args):
1709
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1710
+ return cgutils.false_bit
1711
+
1712
+
1713
+ def np_real_isinf_impl(context, builder, sig, args):
1714
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1715
+ return mathimpl.is_inf(builder, args[0])
1716
+
1717
+
1718
+ def np_complex_isinf_impl(context, builder, sig, args):
1719
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1720
+ (x,) = args
1721
+ (ty,) = sig.args
1722
+ complex_val = context.make_complex(builder, ty, value=x)
1723
+ return cmathimpl.is_inf(builder, complex_val)
1724
+
1725
+
1726
+ def np_real_signbit_impl(context, builder, sig, args):
1727
+ _check_arity_and_homogeneity(sig, args, 1, return_type=types.boolean)
1728
+ # there's no signbit intrinsic in LLVM, so just bitcast as int, mask the
1729
+ # signbit and cmp against 0.
1730
+ masks = {
1731
+ types.float16: context.get_constant(types.uint16, 0x8000),
1732
+ types.float32: context.get_constant(types.uint32, 0x80000000),
1733
+ types.float64: context.get_constant(types.uint64, 0x8000000000000000),
1734
+ }
1735
+ arg_ty = sig.args[0]
1736
+ arg_int_ty = getattr(types, f"uint{arg_ty.bitwidth}")
1737
+ arg_ll_int_ty = context.get_value_type(arg_int_ty)
1738
+ int_res = builder.and_(
1739
+ builder.bitcast(args[0], arg_ll_int_ty), masks[arg_ty]
1740
+ )
1741
+ bool_res = builder.icmp_unsigned("!=", int_res, int_res.type(0))
1742
+ return bool_res
1743
+
1744
+
1745
+ def np_real_copysign_impl(context, builder, sig, args):
1746
+ _check_arity_and_homogeneity(sig, args, 2)
1747
+
1748
+ return mathimpl.copysign_float_impl(context, builder, sig, args)
1749
+
1750
+
1751
+ def np_real_nextafter_impl(context, builder, sig, args):
1752
+ _check_arity_and_homogeneity(sig, args, 2)
1753
+
1754
+ dispatch_table = {
1755
+ types.float32: "numba_nextafterf",
1756
+ types.float64: "numba_nextafter",
1757
+ }
1758
+
1759
+ return _dispatch_func_by_name_type(
1760
+ context, builder, sig, args, dispatch_table, "nextafter"
1761
+ )
1762
+
1763
+
1764
+ def np_real_spacing_impl(context, builder, sig, args):
1765
+ # This is different to how NumPy does it, NumPy has a specialisation of
1766
+ # nextafter called _next, which is used. See:
1767
+ # https://github.com/numpy/numpy/blob/12c2b7dd62fc0c14b81c8892ed5f4f59cc94d09c/numpy/core/src/npymath/ieee754.c.src#L32-L38
1768
+ # Numba elects to use `nextafter` for a similar behaviour to save
1769
+ # translating this very involved function. Further, the NumPy comments note
1770
+ # that there is a lot of redundancy present between the two.
1771
+ _check_arity_and_homogeneity(sig, args, 1)
1772
+
1773
+ dispatch_table = {
1774
+ types.float32: "numba_nextafterf",
1775
+ types.float64: "numba_nextafter",
1776
+ }
1777
+
1778
+ [ty] = sig.args
1779
+ inner_sig = typing.signature(sig.return_type, ty, ty)
1780
+ ll_ty = args[0].type
1781
+ ll_inf = ll_ty(np.inf)
1782
+ fnty = llvmlite.ir.FunctionType(ll_ty, [ll_ty, ll_ty])
1783
+ fn = cgutils.insert_pure_function(
1784
+ builder.module, fnty, name="llvm.copysign"
1785
+ )
1786
+ ll_sinf = builder.call(fn, [ll_inf, args[0]])
1787
+ inner_args = args + [
1788
+ ll_sinf,
1789
+ ]
1790
+ nextafter = _dispatch_func_by_name_type(
1791
+ context, builder, inner_sig, inner_args, dispatch_table, "nextafter"
1792
+ )
1793
+ return builder.fsub(nextafter, args[0])
1794
+
1795
+
1796
+ def np_real_ldexp_impl(context, builder, sig, args):
1797
+ # this one is slightly different to other ufuncs.
1798
+ # arguments are not homogeneous and second arg may come as
1799
+ # an 'i' or an 'l'.
1800
+
1801
+ # the function expects the second argument to be have a C int type
1802
+ x1, x2 = args
1803
+ ty1, ty2 = sig.args
1804
+ # note that types.intc should be equivalent to int_ that is
1805
+ # 'NumPy's default int')
1806
+ x2 = context.cast(builder, x2, ty2, types.intc)
1807
+ f_fi_sig = typing.signature(ty1, ty1, types.intc)
1808
+ return mathimpl.ldexp_impl(context, builder, f_fi_sig, (x1, x2))