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,1461 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: BSD-2-Clause
3
+
4
+ import math
5
+ import numbers
6
+
7
+ import numpy as np
8
+
9
+ from llvmlite import ir
10
+ from llvmlite.ir import Constant
11
+
12
+ from numba.cuda.core.imputils import impl_ret_untracked
13
+ from numba.cuda import typing, types, cgutils
14
+ from numba.cuda.core import errors
15
+ from numba.cuda.cpython.unsafe.numbers import viewer
16
+
17
+
18
+ def _int_arith_flags(rettype):
19
+ """
20
+ Return the modifier flags for integer arithmetic.
21
+ """
22
+ if rettype.signed:
23
+ # Ignore the effects of signed overflow. This is important for
24
+ # optimization of some indexing operations. For example
25
+ # array[i+1] could see `i+1` trigger a signed overflow and
26
+ # give a negative number. With Python's indexing, a negative
27
+ # index is treated differently: its resolution has a runtime cost.
28
+ # Telling LLVM to ignore signed overflows allows it to optimize
29
+ # away the check for a negative `i+1` if it knows `i` is positive.
30
+ return ["nsw"]
31
+ else:
32
+ return []
33
+
34
+
35
+ def int_add_impl(context, builder, sig, args):
36
+ [va, vb] = args
37
+ [ta, tb] = sig.args
38
+ a = context.cast(builder, va, ta, sig.return_type)
39
+ b = context.cast(builder, vb, tb, sig.return_type)
40
+ res = builder.add(a, b, flags=_int_arith_flags(sig.return_type))
41
+ return impl_ret_untracked(context, builder, sig.return_type, res)
42
+
43
+
44
+ def int_sub_impl(context, builder, sig, args):
45
+ [va, vb] = args
46
+ [ta, tb] = sig.args
47
+ a = context.cast(builder, va, ta, sig.return_type)
48
+ b = context.cast(builder, vb, tb, sig.return_type)
49
+ res = builder.sub(a, b, flags=_int_arith_flags(sig.return_type))
50
+ return impl_ret_untracked(context, builder, sig.return_type, res)
51
+
52
+
53
+ def int_mul_impl(context, builder, sig, args):
54
+ [va, vb] = args
55
+ [ta, tb] = sig.args
56
+ a = context.cast(builder, va, ta, sig.return_type)
57
+ b = context.cast(builder, vb, tb, sig.return_type)
58
+ res = builder.mul(a, b, flags=_int_arith_flags(sig.return_type))
59
+ return impl_ret_untracked(context, builder, sig.return_type, res)
60
+
61
+
62
+ def int_divmod_signed(context, builder, ty, x, y):
63
+ """
64
+ Reference Objects/intobject.c
65
+ xdivy = x / y;
66
+ xmody = (long)(x - (unsigned long)xdivy * y);
67
+ /* If the signs of x and y differ, and the remainder is non-0,
68
+ * C89 doesn't define whether xdivy is now the floor or the
69
+ * ceiling of the infinitely precise quotient. We want the floor,
70
+ * and we have it iff the remainder's sign matches y's.
71
+ */
72
+ if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
73
+ xmody += y;
74
+ --xdivy;
75
+ assert(xmody && ((y ^ xmody) >= 0));
76
+ }
77
+ *p_xdivy = xdivy;
78
+ *p_xmody = xmody;
79
+ """
80
+ assert x.type == y.type
81
+
82
+ ZERO = y.type(0)
83
+ ONE = y.type(1)
84
+
85
+ # NOTE: On x86 at least, dividing the lowest representable integer
86
+ # (e.g. 0x80000000 for int32) by -1 causes a SIFGPE (division overflow),
87
+ # causing the process to crash.
88
+ # We return 0, 0 instead (more or less like Numpy).
89
+
90
+ resdiv = cgutils.alloca_once_value(builder, ZERO)
91
+ resmod = cgutils.alloca_once_value(builder, ZERO)
92
+
93
+ is_overflow = builder.and_(
94
+ builder.icmp_signed("==", x, x.type(ty.minval)),
95
+ builder.icmp_signed("==", y, y.type(-1)),
96
+ )
97
+
98
+ with builder.if_then(builder.not_(is_overflow), likely=True):
99
+ # Note LLVM will optimize this to a single divmod instruction,
100
+ # if available on the target CPU (e.g. x86).
101
+ xdivy = builder.sdiv(x, y)
102
+ xmody = builder.srem(x, y)
103
+
104
+ y_xor_xmody_ltz = builder.icmp_signed("<", builder.xor(y, xmody), ZERO)
105
+ xmody_istrue = builder.icmp_signed("!=", xmody, ZERO)
106
+ cond = builder.and_(xmody_istrue, y_xor_xmody_ltz)
107
+
108
+ with builder.if_else(cond) as (if_different_signs, if_same_signs):
109
+ with if_same_signs:
110
+ builder.store(xdivy, resdiv)
111
+ builder.store(xmody, resmod)
112
+
113
+ with if_different_signs:
114
+ builder.store(builder.sub(xdivy, ONE), resdiv)
115
+ builder.store(builder.add(xmody, y), resmod)
116
+
117
+ return builder.load(resdiv), builder.load(resmod)
118
+
119
+
120
+ def int_divmod(context, builder, ty, x, y):
121
+ """
122
+ Integer divmod(x, y). The caller must ensure that y != 0.
123
+ """
124
+ if ty.signed:
125
+ return int_divmod_signed(context, builder, ty, x, y)
126
+ else:
127
+ return builder.udiv(x, y), builder.urem(x, y)
128
+
129
+
130
+ def _int_divmod_impl(context, builder, sig, args, zerodiv_message):
131
+ va, vb = args
132
+ ta, tb = sig.args
133
+
134
+ ty = sig.return_type
135
+ if isinstance(ty, types.UniTuple):
136
+ ty = ty.dtype
137
+ a = context.cast(builder, va, ta, ty)
138
+ b = context.cast(builder, vb, tb, ty)
139
+ quot = cgutils.alloca_once(builder, a.type, name="quot")
140
+ rem = cgutils.alloca_once(builder, a.type, name="rem")
141
+
142
+ with builder.if_else(cgutils.is_scalar_zero(builder, b), likely=False) as (
143
+ if_zero,
144
+ if_non_zero,
145
+ ):
146
+ with if_zero:
147
+ if not context.error_model.fp_zero_division(
148
+ builder, (zerodiv_message,)
149
+ ):
150
+ # No exception raised => return 0
151
+ # XXX We should also set the FPU exception status, but
152
+ # there's no easy way to do that from LLVM.
153
+ builder.store(b, quot)
154
+ builder.store(b, rem)
155
+ with if_non_zero:
156
+ q, r = int_divmod(context, builder, ty, a, b)
157
+ builder.store(q, quot)
158
+ builder.store(r, rem)
159
+
160
+ return quot, rem
161
+
162
+
163
+ # @lower_builtin(divmod, types.Integer, types.Integer)
164
+ def int_divmod_impl(context, builder, sig, args):
165
+ quot, rem = _int_divmod_impl(
166
+ context, builder, sig, args, "integer divmod by zero"
167
+ )
168
+
169
+ return cgutils.pack_array(builder, (builder.load(quot), builder.load(rem)))
170
+
171
+
172
+ # @lower_builtin(operator.floordiv, types.Integer, types.Integer)
173
+ # @lower_builtin(operator.ifloordiv, types.Integer, types.Integer)
174
+ def int_floordiv_impl(context, builder, sig, args):
175
+ quot, rem = _int_divmod_impl(
176
+ context, builder, sig, args, "integer division by zero"
177
+ )
178
+ return builder.load(quot)
179
+
180
+
181
+ # @lower_builtin(operator.truediv, types.Integer, types.Integer)
182
+ # @lower_builtin(operator.itruediv, types.Integer, types.Integer)
183
+ def int_truediv_impl(context, builder, sig, args):
184
+ [va, vb] = args
185
+ [ta, tb] = sig.args
186
+ a = context.cast(builder, va, ta, sig.return_type)
187
+ b = context.cast(builder, vb, tb, sig.return_type)
188
+ with cgutils.if_zero(builder, b):
189
+ context.error_model.fp_zero_division(builder, ("division by zero",))
190
+ res = builder.fdiv(a, b)
191
+ return impl_ret_untracked(context, builder, sig.return_type, res)
192
+
193
+
194
+ # @lower_builtin(operator.mod, types.Integer, types.Integer)
195
+ # @lower_builtin(operator.imod, types.Integer, types.Integer)
196
+ def int_rem_impl(context, builder, sig, args):
197
+ quot, rem = _int_divmod_impl(
198
+ context, builder, sig, args, "integer modulo by zero"
199
+ )
200
+ return builder.load(rem)
201
+
202
+
203
+ def _get_power_zerodiv_return(context, return_type):
204
+ if (
205
+ isinstance(return_type, types.Integer)
206
+ and not context.error_model.raise_on_fp_zero_division
207
+ ):
208
+ # If not raising, return 0x8000... when computing 0 ** <negative number>
209
+ return -1 << (return_type.bitwidth - 1)
210
+ else:
211
+ return False
212
+
213
+
214
+ def int_power_impl(context, builder, sig, args):
215
+ """
216
+ a ^ b, where a is an integer or real, and b an integer
217
+ """
218
+ is_integer = isinstance(sig.args[0], types.Integer)
219
+ tp = sig.return_type
220
+ zerodiv_return = _get_power_zerodiv_return(context, tp)
221
+
222
+ def int_power(a, b):
223
+ # Ensure computations are done with a large enough width
224
+ r = tp(1)
225
+ a = tp(a)
226
+ if b < 0:
227
+ invert = True
228
+ exp = -b
229
+ if exp < 0:
230
+ raise OverflowError
231
+ if is_integer:
232
+ if a == 0:
233
+ if zerodiv_return:
234
+ return zerodiv_return
235
+ else:
236
+ raise ZeroDivisionError(
237
+ "0 cannot be raised to a negative power"
238
+ )
239
+ if a != 1 and a != -1:
240
+ return 0
241
+ else:
242
+ invert = False
243
+ exp = b
244
+ if exp > 0x10000:
245
+ # Optimization cutoff: fallback on the generic algorithm
246
+ return math.pow(a, float(b))
247
+ while exp != 0:
248
+ if exp & 1:
249
+ r *= a
250
+ exp >>= 1
251
+ a *= a
252
+
253
+ return 1.0 / r if invert else r
254
+
255
+ res = context.compile_internal(builder, int_power, sig, args)
256
+ return impl_ret_untracked(context, builder, sig.return_type, res)
257
+
258
+
259
+ # @lower_builtin(operator.pow, types.Integer, types.IntegerLiteral)
260
+ # @lower_builtin(operator.ipow, types.Integer, types.IntegerLiteral)
261
+ # @lower_builtin(operator.pow, types.Float, types.IntegerLiteral)
262
+ # @lower_builtin(operator.ipow, types.Float, types.IntegerLiteral)
263
+ def static_power_impl(context, builder, sig, args):
264
+ """
265
+ a ^ b, where a is an integer or real, and b a constant integer
266
+ """
267
+ exp = sig.args[1].value
268
+ if not isinstance(exp, numbers.Integral):
269
+ raise NotImplementedError
270
+ if abs(exp) > 0x10000:
271
+ # Optimization cutoff: fallback on the generic algorithm above
272
+ raise NotImplementedError
273
+ invert = exp < 0
274
+ exp = abs(exp)
275
+
276
+ tp = sig.return_type
277
+ is_integer = isinstance(tp, types.Integer)
278
+ zerodiv_return = _get_power_zerodiv_return(context, tp)
279
+
280
+ val = context.cast(builder, args[0], sig.args[0], tp)
281
+ lty = val.type
282
+
283
+ def mul(a, b):
284
+ if is_integer:
285
+ return builder.mul(a, b)
286
+ else:
287
+ return builder.fmul(a, b)
288
+
289
+ # Unroll the exponentiation loop
290
+ res = lty(1)
291
+ while exp != 0:
292
+ if exp & 1:
293
+ res = mul(res, val)
294
+ exp >>= 1
295
+ val = mul(val, val)
296
+
297
+ if invert:
298
+ # If the exponent was negative, fix the result by inverting it
299
+ if is_integer:
300
+ # Integer inversion
301
+ def invert_impl(a):
302
+ if a == 0:
303
+ if zerodiv_return:
304
+ return zerodiv_return
305
+ else:
306
+ raise ZeroDivisionError(
307
+ "0 cannot be raised to a negative power"
308
+ )
309
+ if a != 1 and a != -1:
310
+ return 0
311
+ else:
312
+ return a
313
+
314
+ else:
315
+ # Real inversion
316
+ def invert_impl(a):
317
+ return 1.0 / a
318
+
319
+ res = context.compile_internal(
320
+ builder, invert_impl, typing.signature(tp, tp), (res,)
321
+ )
322
+
323
+ return res
324
+
325
+
326
+ def int_slt_impl(context, builder, sig, args):
327
+ res = builder.icmp_signed("<", *args)
328
+ return impl_ret_untracked(context, builder, sig.return_type, res)
329
+
330
+
331
+ def int_sle_impl(context, builder, sig, args):
332
+ res = builder.icmp_signed("<=", *args)
333
+ return impl_ret_untracked(context, builder, sig.return_type, res)
334
+
335
+
336
+ def int_sgt_impl(context, builder, sig, args):
337
+ res = builder.icmp_signed(">", *args)
338
+ return impl_ret_untracked(context, builder, sig.return_type, res)
339
+
340
+
341
+ def int_sge_impl(context, builder, sig, args):
342
+ res = builder.icmp_signed(">=", *args)
343
+ return impl_ret_untracked(context, builder, sig.return_type, res)
344
+
345
+
346
+ def int_ult_impl(context, builder, sig, args):
347
+ res = builder.icmp_unsigned("<", *args)
348
+ return impl_ret_untracked(context, builder, sig.return_type, res)
349
+
350
+
351
+ def int_ule_impl(context, builder, sig, args):
352
+ res = builder.icmp_unsigned("<=", *args)
353
+ return impl_ret_untracked(context, builder, sig.return_type, res)
354
+
355
+
356
+ def int_ugt_impl(context, builder, sig, args):
357
+ res = builder.icmp_unsigned(">", *args)
358
+ return impl_ret_untracked(context, builder, sig.return_type, res)
359
+
360
+
361
+ def int_uge_impl(context, builder, sig, args):
362
+ res = builder.icmp_unsigned(">=", *args)
363
+ return impl_ret_untracked(context, builder, sig.return_type, res)
364
+
365
+
366
+ def int_eq_impl(context, builder, sig, args):
367
+ res = builder.icmp_unsigned("==", *args)
368
+ return impl_ret_untracked(context, builder, sig.return_type, res)
369
+
370
+
371
+ def int_ne_impl(context, builder, sig, args):
372
+ res = builder.icmp_unsigned("!=", *args)
373
+ return impl_ret_untracked(context, builder, sig.return_type, res)
374
+
375
+
376
+ def int_signed_unsigned_cmp(op):
377
+ def impl(context, builder, sig, args):
378
+ (left, right) = args
379
+ # This code is translated from the NumPy source.
380
+ # What we're going to do is divide the range of a signed value at zero.
381
+ # If the signed value is less than zero, then we can treat zero as the
382
+ # unsigned value since the unsigned value is necessarily zero or larger
383
+ # and any signed comparison between a negative value and zero/infinity
384
+ # will yield the same result. If the signed value is greater than or
385
+ # equal to zero, then we can safely cast it to an unsigned value and do
386
+ # the expected unsigned-unsigned comparison operation.
387
+ # Original: https://github.com/numpy/numpy/pull/23713
388
+ cmp_zero = builder.icmp_signed("<", left, Constant(left.type, 0))
389
+ lt_zero = builder.icmp_signed(op, left, Constant(left.type, 0))
390
+ ge_zero = builder.icmp_unsigned(op, left, right)
391
+ res = builder.select(cmp_zero, lt_zero, ge_zero)
392
+ return impl_ret_untracked(context, builder, sig.return_type, res)
393
+
394
+ return impl
395
+
396
+
397
+ def int_unsigned_signed_cmp(op):
398
+ def impl(context, builder, sig, args):
399
+ (left, right) = args
400
+ # See the function `int_signed_unsigned_cmp` for implementation notes.
401
+ cmp_zero = builder.icmp_signed("<", right, Constant(right.type, 0))
402
+ lt_zero = builder.icmp_signed(op, Constant(right.type, 0), right)
403
+ ge_zero = builder.icmp_unsigned(op, left, right)
404
+ res = builder.select(cmp_zero, lt_zero, ge_zero)
405
+ return impl_ret_untracked(context, builder, sig.return_type, res)
406
+
407
+ return impl
408
+
409
+
410
+ def int_abs_impl(context, builder, sig, args):
411
+ [x] = args
412
+ ZERO = Constant(x.type, None)
413
+ ltz = builder.icmp_signed("<", x, ZERO)
414
+ negated = builder.neg(x)
415
+ res = builder.select(ltz, negated, x)
416
+ return impl_ret_untracked(context, builder, sig.return_type, res)
417
+
418
+
419
+ def identity_impl(context, builder, sig, args):
420
+ [x] = args
421
+ return impl_ret_untracked(context, builder, sig.return_type, x)
422
+
423
+
424
+ def uint_abs_impl(context, builder, sig, args):
425
+ [x] = args
426
+ return impl_ret_untracked(context, builder, sig.return_type, x)
427
+
428
+
429
+ def int_shl_impl(context, builder, sig, args):
430
+ [valty, amtty] = sig.args
431
+ [val, amt] = args
432
+ val = context.cast(builder, val, valty, sig.return_type)
433
+ amt = context.cast(builder, amt, amtty, sig.return_type)
434
+ res = builder.shl(val, amt)
435
+ return impl_ret_untracked(context, builder, sig.return_type, res)
436
+
437
+
438
+ def int_shr_impl(context, builder, sig, args):
439
+ [valty, amtty] = sig.args
440
+ [val, amt] = args
441
+ val = context.cast(builder, val, valty, sig.return_type)
442
+ amt = context.cast(builder, amt, amtty, sig.return_type)
443
+ if sig.return_type.signed:
444
+ res = builder.ashr(val, amt)
445
+ else:
446
+ res = builder.lshr(val, amt)
447
+ return impl_ret_untracked(context, builder, sig.return_type, res)
448
+
449
+
450
+ def int_and_impl(context, builder, sig, args):
451
+ [at, bt] = sig.args
452
+ [av, bv] = args
453
+ cav = context.cast(builder, av, at, sig.return_type)
454
+ cbc = context.cast(builder, bv, bt, sig.return_type)
455
+ res = builder.and_(cav, cbc)
456
+ return impl_ret_untracked(context, builder, sig.return_type, res)
457
+
458
+
459
+ def int_or_impl(context, builder, sig, args):
460
+ [at, bt] = sig.args
461
+ [av, bv] = args
462
+ cav = context.cast(builder, av, at, sig.return_type)
463
+ cbc = context.cast(builder, bv, bt, sig.return_type)
464
+ res = builder.or_(cav, cbc)
465
+ return impl_ret_untracked(context, builder, sig.return_type, res)
466
+
467
+
468
+ def int_xor_impl(context, builder, sig, args):
469
+ [at, bt] = sig.args
470
+ [av, bv] = args
471
+ cav = context.cast(builder, av, at, sig.return_type)
472
+ cbc = context.cast(builder, bv, bt, sig.return_type)
473
+ res = builder.xor(cav, cbc)
474
+ return impl_ret_untracked(context, builder, sig.return_type, res)
475
+
476
+
477
+ def int_negate_impl(context, builder, sig, args):
478
+ [typ] = sig.args
479
+ [val] = args
480
+ # Negate before upcasting, for unsigned numbers
481
+ res = builder.neg(val)
482
+ res = context.cast(builder, res, typ, sig.return_type)
483
+ return impl_ret_untracked(context, builder, sig.return_type, res)
484
+
485
+
486
+ def int_positive_impl(context, builder, sig, args):
487
+ [typ] = sig.args
488
+ [val] = args
489
+ res = context.cast(builder, val, typ, sig.return_type)
490
+ return impl_ret_untracked(context, builder, sig.return_type, res)
491
+
492
+
493
+ def int_invert_impl(context, builder, sig, args):
494
+ [typ] = sig.args
495
+ [val] = args
496
+ # Invert before upcasting, for unsigned numbers
497
+ res = builder.xor(val, Constant(val.type, int("1" * val.type.width, 2)))
498
+ res = context.cast(builder, res, typ, sig.return_type)
499
+ return impl_ret_untracked(context, builder, sig.return_type, res)
500
+
501
+
502
+ def int_sign_impl(context, builder, sig, args):
503
+ """
504
+ np.sign(int)
505
+ """
506
+ [x] = args
507
+ POS = Constant(x.type, 1)
508
+ NEG = Constant(x.type, -1)
509
+ ZERO = Constant(x.type, 0)
510
+
511
+ cmp_zero = builder.icmp_unsigned("==", x, ZERO)
512
+ cmp_pos = builder.icmp_signed(">", x, ZERO)
513
+
514
+ presult = cgutils.alloca_once(builder, x.type)
515
+
516
+ bb_zero = builder.append_basic_block(".zero")
517
+ bb_postest = builder.append_basic_block(".postest")
518
+ bb_pos = builder.append_basic_block(".pos")
519
+ bb_neg = builder.append_basic_block(".neg")
520
+ bb_exit = builder.append_basic_block(".exit")
521
+
522
+ builder.cbranch(cmp_zero, bb_zero, bb_postest)
523
+
524
+ with builder.goto_block(bb_zero):
525
+ builder.store(ZERO, presult)
526
+ builder.branch(bb_exit)
527
+
528
+ with builder.goto_block(bb_postest):
529
+ builder.cbranch(cmp_pos, bb_pos, bb_neg)
530
+
531
+ with builder.goto_block(bb_pos):
532
+ builder.store(POS, presult)
533
+ builder.branch(bb_exit)
534
+
535
+ with builder.goto_block(bb_neg):
536
+ builder.store(NEG, presult)
537
+ builder.branch(bb_exit)
538
+
539
+ builder.position_at_end(bb_exit)
540
+ res = builder.load(presult)
541
+ return impl_ret_untracked(context, builder, sig.return_type, res)
542
+
543
+
544
+ def bool_negate_impl(context, builder, sig, args):
545
+ [typ] = sig.args
546
+ [val] = args
547
+ res = context.cast(builder, val, typ, sig.return_type)
548
+ res = builder.neg(res)
549
+ return impl_ret_untracked(context, builder, sig.return_type, res)
550
+
551
+
552
+ def bool_unary_positive_impl(context, builder, sig, args):
553
+ [typ] = sig.args
554
+ [val] = args
555
+ res = context.cast(builder, val, typ, sig.return_type)
556
+ return impl_ret_untracked(context, builder, sig.return_type, res)
557
+
558
+
559
+ # lower_builtin(operator.eq, types.boolean, types.boolean)(int_eq_impl)
560
+ # lower_builtin(operator.ne, types.boolean, types.boolean)(int_ne_impl)
561
+ # lower_builtin(operator.lt, types.boolean, types.boolean)(int_ult_impl)
562
+ # lower_builtin(operator.le, types.boolean, types.boolean)(int_ule_impl)
563
+ # lower_builtin(operator.gt, types.boolean, types.boolean)(int_ugt_impl)
564
+ # lower_builtin(operator.ge, types.boolean, types.boolean)(int_uge_impl)
565
+ # lower_builtin(operator.neg, types.boolean)(bool_negate_impl)
566
+ # lower_builtin(operator.pos, types.boolean)(bool_unary_positive_impl)
567
+
568
+
569
+ # def _implement_integer_operators():
570
+ # ty = types.Integer
571
+
572
+ # lower_builtin(operator.add, ty, ty)(int_add_impl)
573
+ # lower_builtin(operator.iadd, ty, ty)(int_add_impl)
574
+ # lower_builtin(operator.sub, ty, ty)(int_sub_impl)
575
+ # lower_builtin(operator.isub, ty, ty)(int_sub_impl)
576
+ # lower_builtin(operator.mul, ty, ty)(int_mul_impl)
577
+ # lower_builtin(operator.imul, ty, ty)(int_mul_impl)
578
+ # lower_builtin(operator.eq, ty, ty)(int_eq_impl)
579
+ # lower_builtin(operator.ne, ty, ty)(int_ne_impl)
580
+
581
+ # lower_builtin(operator.lshift, ty, ty)(int_shl_impl)
582
+ # lower_builtin(operator.ilshift, ty, ty)(int_shl_impl)
583
+ # lower_builtin(operator.rshift, ty, ty)(int_shr_impl)
584
+ # lower_builtin(operator.irshift, ty, ty)(int_shr_impl)
585
+
586
+ # lower_builtin(operator.neg, ty)(int_negate_impl)
587
+ # lower_builtin(operator.pos, ty)(int_positive_impl)
588
+
589
+ # lower_builtin(operator.pow, ty, ty)(int_power_impl)
590
+ # lower_builtin(operator.ipow, ty, ty)(int_power_impl)
591
+ # lower_builtin(pow, ty, ty)(int_power_impl)
592
+
593
+ # for ty in types.unsigned_domain:
594
+ # lower_builtin(operator.lt, ty, ty)(int_ult_impl)
595
+ # lower_builtin(operator.le, ty, ty)(int_ule_impl)
596
+ # lower_builtin(operator.gt, ty, ty)(int_ugt_impl)
597
+ # lower_builtin(operator.ge, ty, ty)(int_uge_impl)
598
+ # lower_builtin(operator.pow, types.Float, ty)(int_power_impl)
599
+ # lower_builtin(operator.ipow, types.Float, ty)(int_power_impl)
600
+ # lower_builtin(pow, types.Float, ty)(int_power_impl)
601
+ # lower_builtin(abs, ty)(uint_abs_impl)
602
+
603
+ # lower_builtin(operator.lt, types.IntegerLiteral, types.IntegerLiteral)(int_slt_impl)
604
+ # lower_builtin(operator.gt, types.IntegerLiteral, types.IntegerLiteral)(int_slt_impl)
605
+ # lower_builtin(operator.le, types.IntegerLiteral, types.IntegerLiteral)(int_slt_impl)
606
+ # lower_builtin(operator.ge, types.IntegerLiteral, types.IntegerLiteral)(int_slt_impl)
607
+ # for ty in types.signed_domain:
608
+ # lower_builtin(operator.lt, ty, ty)(int_slt_impl)
609
+ # lower_builtin(operator.le, ty, ty)(int_sle_impl)
610
+ # lower_builtin(operator.gt, ty, ty)(int_sgt_impl)
611
+ # lower_builtin(operator.ge, ty, ty)(int_sge_impl)
612
+ # lower_builtin(operator.pow, types.Float, ty)(int_power_impl)
613
+ # lower_builtin(operator.ipow, types.Float, ty)(int_power_impl)
614
+ # lower_builtin(pow, types.Float, ty)(int_power_impl)
615
+ # lower_builtin(abs, ty)(int_abs_impl)
616
+
617
+ # def _implement_bitwise_operators():
618
+ # for ty in (types.Boolean, types.Integer):
619
+ # lower_builtin(operator.and_, ty, ty)(int_and_impl)
620
+ # lower_builtin(operator.iand, ty, ty)(int_and_impl)
621
+ # lower_builtin(operator.or_, ty, ty)(int_or_impl)
622
+ # lower_builtin(operator.ior, ty, ty)(int_or_impl)
623
+ # lower_builtin(operator.xor, ty, ty)(int_xor_impl)
624
+ # lower_builtin(operator.ixor, ty, ty)(int_xor_impl)
625
+
626
+ # lower_builtin(operator.invert, ty)(int_invert_impl)
627
+
628
+ # _implement_integer_operators()
629
+
630
+ # _implement_bitwise_operators()
631
+
632
+
633
+ def real_add_impl(context, builder, sig, args):
634
+ res = builder.fadd(*args)
635
+ return impl_ret_untracked(context, builder, sig.return_type, res)
636
+
637
+
638
+ def real_sub_impl(context, builder, sig, args):
639
+ res = builder.fsub(*args)
640
+ return impl_ret_untracked(context, builder, sig.return_type, res)
641
+
642
+
643
+ def real_mul_impl(context, builder, sig, args):
644
+ res = builder.fmul(*args)
645
+ return impl_ret_untracked(context, builder, sig.return_type, res)
646
+
647
+
648
+ def real_div_impl(context, builder, sig, args):
649
+ with cgutils.if_zero(builder, args[1]):
650
+ context.error_model.fp_zero_division(builder, ("division by zero",))
651
+ res = builder.fdiv(*args)
652
+ return impl_ret_untracked(context, builder, sig.return_type, res)
653
+
654
+
655
+ def real_divmod(context, builder, x, y):
656
+ assert x.type == y.type
657
+ floatty = x.type
658
+
659
+ module = builder.module
660
+ fname = context.mangler(".numba.python.rem", [x.type])
661
+ fnty = ir.FunctionType(floatty, (floatty, floatty, ir.PointerType(floatty)))
662
+ fn = cgutils.get_or_insert_function(module, fnty, fname)
663
+
664
+ if fn.is_declaration:
665
+ fn.linkage = "linkonce_odr"
666
+ fnbuilder = ir.IRBuilder(fn.append_basic_block("entry"))
667
+ fx, fy, pmod = fn.args
668
+ div, mod = real_divmod_func_body(context, fnbuilder, fx, fy)
669
+ fnbuilder.store(mod, pmod)
670
+ fnbuilder.ret(div)
671
+
672
+ pmod = cgutils.alloca_once(builder, floatty)
673
+ quotient = builder.call(fn, (x, y, pmod))
674
+ return quotient, builder.load(pmod)
675
+
676
+
677
+ def real_divmod_func_body(context, builder, vx, wx):
678
+ # Reference Objects/floatobject.c
679
+ #
680
+ # float_divmod(PyObject *v, PyObject *w)
681
+ # {
682
+ # double vx, wx;
683
+ # double div, mod, floordiv;
684
+ # CONVERT_TO_DOUBLE(v, vx);
685
+ # CONVERT_TO_DOUBLE(w, wx);
686
+ # mod = fmod(vx, wx);
687
+ # /* fmod is typically exact, so vx-mod is *mathematically* an
688
+ # exact multiple of wx. But this is fp arithmetic, and fp
689
+ # vx - mod is an approximation; the result is that div may
690
+ # not be an exact integral value after the division, although
691
+ # it will always be very close to one.
692
+ # */
693
+ # div = (vx - mod) / wx;
694
+ # if (mod) {
695
+ # /* ensure the remainder has the same sign as the denominator */
696
+ # if ((wx < 0) != (mod < 0)) {
697
+ # mod += wx;
698
+ # div -= 1.0;
699
+ # }
700
+ # }
701
+ # else {
702
+ # /* the remainder is zero, and in the presence of signed zeroes
703
+ # fmod returns different results across platforms; ensure
704
+ # it has the same sign as the denominator; we'd like to do
705
+ # "mod = wx * 0.0", but that may get optimized away */
706
+ # mod *= mod; /* hide "mod = +0" from optimizer */
707
+ # if (wx < 0.0)
708
+ # mod = -mod;
709
+ # }
710
+ # /* snap quotient to nearest integral value */
711
+ # if (div) {
712
+ # floordiv = floor(div);
713
+ # if (div - floordiv > 0.5)
714
+ # floordiv += 1.0;
715
+ # }
716
+ # else {
717
+ # /* div is zero - get the same sign as the true quotient */
718
+ # div *= div; /* hide "div = +0" from optimizers */
719
+ # floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
720
+ # }
721
+ # return Py_BuildValue("(dd)", floordiv, mod);
722
+ # }
723
+ pmod = cgutils.alloca_once(builder, vx.type)
724
+ pdiv = cgutils.alloca_once(builder, vx.type)
725
+ pfloordiv = cgutils.alloca_once(builder, vx.type)
726
+
727
+ mod = builder.frem(vx, wx)
728
+ div = builder.fdiv(builder.fsub(vx, mod), wx)
729
+
730
+ builder.store(mod, pmod)
731
+ builder.store(div, pdiv)
732
+
733
+ # Note the use of negative zero for proper negating with `ZERO - x`
734
+ ZERO = vx.type(0.0)
735
+ NZERO = vx.type(-0.0)
736
+ ONE = vx.type(1.0)
737
+ mod_istrue = builder.fcmp_unordered("!=", mod, ZERO)
738
+ wx_ltz = builder.fcmp_ordered("<", wx, ZERO)
739
+ mod_ltz = builder.fcmp_ordered("<", mod, ZERO)
740
+
741
+ with builder.if_else(mod_istrue, likely=True) as (
742
+ if_nonzero_mod,
743
+ if_zero_mod,
744
+ ):
745
+ with if_nonzero_mod:
746
+ # `mod` is non-zero or NaN
747
+ # Ensure the remainder has the same sign as the denominator
748
+ wx_ltz_ne_mod_ltz = builder.icmp_unsigned("!=", wx_ltz, mod_ltz)
749
+
750
+ with builder.if_then(wx_ltz_ne_mod_ltz):
751
+ builder.store(builder.fsub(div, ONE), pdiv)
752
+ builder.store(builder.fadd(mod, wx), pmod)
753
+
754
+ with if_zero_mod:
755
+ # `mod` is zero, select the proper sign depending on
756
+ # the denominator's sign
757
+ mod = builder.select(wx_ltz, NZERO, ZERO)
758
+ builder.store(mod, pmod)
759
+
760
+ del mod, div
761
+
762
+ div = builder.load(pdiv)
763
+ div_istrue = builder.fcmp_ordered("!=", div, ZERO)
764
+
765
+ with builder.if_then(div_istrue):
766
+ realtypemap = {"float": types.float32, "double": types.float64}
767
+ realtype = realtypemap[str(wx.type)]
768
+ floorfn = context.get_function(
769
+ math.floor, typing.signature(realtype, realtype)
770
+ )
771
+ floordiv = floorfn(builder, [div])
772
+ floordivdiff = builder.fsub(div, floordiv)
773
+ floordivincr = builder.fadd(floordiv, ONE)
774
+ HALF = Constant(wx.type, 0.5)
775
+ pred = builder.fcmp_ordered(">", floordivdiff, HALF)
776
+ floordiv = builder.select(pred, floordivincr, floordiv)
777
+ builder.store(floordiv, pfloordiv)
778
+
779
+ with cgutils.ifnot(builder, div_istrue):
780
+ div = builder.fmul(div, div)
781
+ builder.store(div, pdiv)
782
+ floordiv = builder.fdiv(builder.fmul(div, vx), wx)
783
+ builder.store(floordiv, pfloordiv)
784
+
785
+ return builder.load(pfloordiv), builder.load(pmod)
786
+
787
+
788
+ # @lower_builtin(divmod, types.Float, types.Float)
789
+ def real_divmod_impl(context, builder, sig, args, loc=None):
790
+ x, y = args
791
+ quot = cgutils.alloca_once(builder, x.type, name="quot")
792
+ rem = cgutils.alloca_once(builder, x.type, name="rem")
793
+
794
+ with builder.if_else(cgutils.is_scalar_zero(builder, y), likely=False) as (
795
+ if_zero,
796
+ if_non_zero,
797
+ ):
798
+ with if_zero:
799
+ if not context.error_model.fp_zero_division(
800
+ builder, ("modulo by zero",), loc
801
+ ):
802
+ # No exception raised => compute the nan result,
803
+ # and set the FP exception word for Numpy warnings.
804
+ q = builder.fdiv(x, y)
805
+ r = builder.frem(x, y)
806
+ builder.store(q, quot)
807
+ builder.store(r, rem)
808
+ with if_non_zero:
809
+ q, r = real_divmod(context, builder, x, y)
810
+ builder.store(q, quot)
811
+ builder.store(r, rem)
812
+
813
+ return cgutils.pack_array(builder, (builder.load(quot), builder.load(rem)))
814
+
815
+
816
+ def real_mod_impl(context, builder, sig, args, loc=None):
817
+ x, y = args
818
+ res = cgutils.alloca_once(builder, x.type)
819
+ with builder.if_else(cgutils.is_scalar_zero(builder, y), likely=False) as (
820
+ if_zero,
821
+ if_non_zero,
822
+ ):
823
+ with if_zero:
824
+ if not context.error_model.fp_zero_division(
825
+ builder, ("modulo by zero",), loc
826
+ ):
827
+ # No exception raised => compute the nan result,
828
+ # and set the FP exception word for Numpy warnings.
829
+ rem = builder.frem(x, y)
830
+ builder.store(rem, res)
831
+ with if_non_zero:
832
+ _, rem = real_divmod(context, builder, x, y)
833
+ builder.store(rem, res)
834
+ return impl_ret_untracked(
835
+ context, builder, sig.return_type, builder.load(res)
836
+ )
837
+
838
+
839
+ def real_floordiv_impl(context, builder, sig, args, loc=None):
840
+ x, y = args
841
+ res = cgutils.alloca_once(builder, x.type)
842
+ with builder.if_else(cgutils.is_scalar_zero(builder, y), likely=False) as (
843
+ if_zero,
844
+ if_non_zero,
845
+ ):
846
+ with if_zero:
847
+ if not context.error_model.fp_zero_division(
848
+ builder, ("division by zero",), loc
849
+ ):
850
+ # No exception raised => compute the +/-inf or nan result,
851
+ # and set the FP exception word for Numpy warnings.
852
+ quot = builder.fdiv(x, y)
853
+ builder.store(quot, res)
854
+ with if_non_zero:
855
+ quot, _ = real_divmod(context, builder, x, y)
856
+ builder.store(quot, res)
857
+ return impl_ret_untracked(
858
+ context, builder, sig.return_type, builder.load(res)
859
+ )
860
+
861
+
862
+ def real_power_impl(context, builder, sig, args):
863
+ x, y = args
864
+ module = builder.module
865
+ if context.implement_powi_as_math_call:
866
+ imp = context.get_function(math.pow, sig)
867
+ res = imp(builder, args)
868
+ else:
869
+ fn = module.declare_intrinsic("llvm.pow", [y.type])
870
+ res = builder.call(fn, (x, y))
871
+ return impl_ret_untracked(context, builder, sig.return_type, res)
872
+
873
+
874
+ def real_lt_impl(context, builder, sig, args):
875
+ res = builder.fcmp_ordered("<", *args)
876
+ return impl_ret_untracked(context, builder, sig.return_type, res)
877
+
878
+
879
+ def real_le_impl(context, builder, sig, args):
880
+ res = builder.fcmp_ordered("<=", *args)
881
+ return impl_ret_untracked(context, builder, sig.return_type, res)
882
+
883
+
884
+ def real_gt_impl(context, builder, sig, args):
885
+ res = builder.fcmp_ordered(">", *args)
886
+ return impl_ret_untracked(context, builder, sig.return_type, res)
887
+
888
+
889
+ def real_ge_impl(context, builder, sig, args):
890
+ res = builder.fcmp_ordered(">=", *args)
891
+ return impl_ret_untracked(context, builder, sig.return_type, res)
892
+
893
+
894
+ def real_eq_impl(context, builder, sig, args):
895
+ res = builder.fcmp_ordered("==", *args)
896
+ return impl_ret_untracked(context, builder, sig.return_type, res)
897
+
898
+
899
+ def real_ne_impl(context, builder, sig, args):
900
+ res = builder.fcmp_unordered("!=", *args)
901
+ return impl_ret_untracked(context, builder, sig.return_type, res)
902
+
903
+
904
+ def real_abs_impl(context, builder, sig, args):
905
+ [ty] = sig.args
906
+ sig = typing.signature(ty, ty)
907
+ impl = context.get_function(math.fabs, sig)
908
+ return impl(builder, args)
909
+
910
+
911
+ def real_negate_impl(context, builder, sig, args):
912
+ from numba.cuda.cpython import mathimpl
913
+
914
+ res = mathimpl.negate_real(builder, args[0])
915
+ return impl_ret_untracked(context, builder, sig.return_type, res)
916
+
917
+
918
+ def real_positive_impl(context, builder, sig, args):
919
+ [typ] = sig.args
920
+ [val] = args
921
+ res = context.cast(builder, val, typ, sig.return_type)
922
+ return impl_ret_untracked(context, builder, sig.return_type, res)
923
+
924
+
925
+ def real_sign_impl(context, builder, sig, args):
926
+ """
927
+ np.sign(float)
928
+ """
929
+ [x] = args
930
+ POS = Constant(x.type, 1)
931
+ NEG = Constant(x.type, -1)
932
+ ZERO = Constant(x.type, 0)
933
+
934
+ presult = cgutils.alloca_once(builder, x.type)
935
+
936
+ is_pos = builder.fcmp_ordered(">", x, ZERO)
937
+ is_neg = builder.fcmp_ordered("<", x, ZERO)
938
+
939
+ with builder.if_else(is_pos) as (gt_zero, not_gt_zero):
940
+ with gt_zero:
941
+ builder.store(POS, presult)
942
+ with not_gt_zero:
943
+ with builder.if_else(is_neg) as (lt_zero, not_lt_zero):
944
+ with lt_zero:
945
+ builder.store(NEG, presult)
946
+ with not_lt_zero:
947
+ # For both NaN and 0, the result of sign() is simply
948
+ # the input value.
949
+ builder.store(x, presult)
950
+
951
+ res = builder.load(presult)
952
+ return impl_ret_untracked(context, builder, sig.return_type, res)
953
+
954
+
955
+ # ty = types.Float
956
+
957
+ # lower_builtin(operator.add, ty, ty)(real_add_impl)
958
+ # lower_builtin(operator.iadd, ty, ty)(real_add_impl)
959
+ # lower_builtin(operator.sub, ty, ty)(real_sub_impl)
960
+ # lower_builtin(operator.isub, ty, ty)(real_sub_impl)
961
+ # lower_builtin(operator.mul, ty, ty)(real_mul_impl)
962
+ # lower_builtin(operator.imul, ty, ty)(real_mul_impl)
963
+ # lower_builtin(operator.floordiv, ty, ty)(real_floordiv_impl)
964
+ # lower_builtin(operator.ifloordiv, ty, ty)(real_floordiv_impl)
965
+ # lower_builtin(operator.truediv, ty, ty)(real_div_impl)
966
+ # lower_builtin(operator.itruediv, ty, ty)(real_div_impl)
967
+ # lower_builtin(operator.mod, ty, ty)(real_mod_impl)
968
+ # lower_builtin(operator.imod, ty, ty)(real_mod_impl)
969
+ # lower_builtin(operator.pow, ty, ty)(real_power_impl)
970
+ # lower_builtin(operator.ipow, ty, ty)(real_power_impl)
971
+ # lower_builtin(pow, ty, ty)(real_power_impl)
972
+
973
+ # lower_builtin(operator.eq, ty, ty)(real_eq_impl)
974
+ # lower_builtin(operator.ne, ty, ty)(real_ne_impl)
975
+ # lower_builtin(operator.lt, ty, ty)(real_lt_impl)
976
+ # lower_builtin(operator.le, ty, ty)(real_le_impl)
977
+ # lower_builtin(operator.gt, ty, ty)(real_gt_impl)
978
+ # lower_builtin(operator.ge, ty, ty)(real_ge_impl)
979
+
980
+ # lower_builtin(abs, ty)(real_abs_impl)
981
+
982
+ # lower_builtin(operator.neg, ty)(real_negate_impl)
983
+ # lower_builtin(operator.pos, ty)(real_positive_impl)
984
+
985
+ # del ty
986
+
987
+
988
+ # @lower_getattr(types.Complex, "real")
989
+ def complex_real_impl(context, builder, typ, value):
990
+ cplx = context.make_complex(builder, typ, value=value)
991
+ res = cplx.real
992
+ return impl_ret_untracked(context, builder, typ, res)
993
+
994
+
995
+ # @lower_getattr(types.Complex, "imag")
996
+ def complex_imag_impl(context, builder, typ, value):
997
+ cplx = context.make_complex(builder, typ, value=value)
998
+ res = cplx.imag
999
+ return impl_ret_untracked(context, builder, typ, res)
1000
+
1001
+
1002
+ # @lower_builtin("complex.conjugate", types.Complex)
1003
+ def complex_conjugate_impl(context, builder, sig, args):
1004
+ from numba.cuda.cpython import mathimpl
1005
+
1006
+ z = context.make_complex(builder, sig.args[0], args[0])
1007
+ z.imag = mathimpl.negate_real(builder, z.imag)
1008
+ res = z._getvalue()
1009
+ return impl_ret_untracked(context, builder, sig.return_type, res)
1010
+
1011
+
1012
+ def real_real_impl(context, builder, typ, value):
1013
+ return impl_ret_untracked(context, builder, typ, value)
1014
+
1015
+
1016
+ def real_imag_impl(context, builder, typ, value):
1017
+ res = cgutils.get_null_value(value.type)
1018
+ return impl_ret_untracked(context, builder, typ, res)
1019
+
1020
+
1021
+ def real_conjugate_impl(context, builder, sig, args):
1022
+ return impl_ret_untracked(context, builder, sig.return_type, args[0])
1023
+
1024
+
1025
+ # for cls in (types.Float, types.Integer):
1026
+ # lower_getattr(cls, "real")(real_real_impl)
1027
+ # lower_getattr(cls, "imag")(real_imag_impl)
1028
+ # lower_builtin("complex.conjugate", cls)(real_conjugate_impl)
1029
+
1030
+
1031
+ # @lower_builtin(operator.pow, types.Complex, types.Complex)
1032
+ # @lower_builtin(operator.ipow, types.Complex, types.Complex)
1033
+ # @lower_builtin(pow, types.Complex, types.Complex)
1034
+ def complex_power_impl(context, builder, sig, args):
1035
+ [ca, cb] = args
1036
+ ty = sig.args[0]
1037
+ fty = ty.underlying_float
1038
+ a = context.make_helper(builder, ty, value=ca)
1039
+ b = context.make_helper(builder, ty, value=cb)
1040
+ c = context.make_helper(builder, ty)
1041
+ module = builder.module
1042
+ pa = a._getpointer()
1043
+ pb = b._getpointer()
1044
+ pc = c._getpointer()
1045
+
1046
+ # Optimize for square because cpow loses a lot of precision
1047
+ TWO = context.get_constant(fty, 2)
1048
+ ZERO = context.get_constant(fty, 0)
1049
+
1050
+ b_real_is_two = builder.fcmp_ordered("==", b.real, TWO)
1051
+ b_imag_is_zero = builder.fcmp_ordered("==", b.imag, ZERO)
1052
+ b_is_two = builder.and_(b_real_is_two, b_imag_is_zero)
1053
+
1054
+ with builder.if_else(b_is_two) as (then, otherwise):
1055
+ with then:
1056
+ # Lower as multiplication
1057
+ res = complex_mul_impl(context, builder, sig, (ca, ca))
1058
+ cres = context.make_helper(builder, ty, value=res)
1059
+ c.real = cres.real
1060
+ c.imag = cres.imag
1061
+
1062
+ with otherwise:
1063
+ # Lower with call to external function
1064
+ func_name = {
1065
+ types.complex64: "numba_cpowf",
1066
+ types.complex128: "numba_cpow",
1067
+ }[ty]
1068
+ fnty = ir.FunctionType(ir.VoidType(), [pa.type] * 3)
1069
+ cpow = cgutils.get_or_insert_function(module, fnty, func_name)
1070
+ builder.call(cpow, (pa, pb, pc))
1071
+
1072
+ res = builder.load(pc)
1073
+ return impl_ret_untracked(context, builder, sig.return_type, res)
1074
+
1075
+
1076
+ def complex_add_impl(context, builder, sig, args):
1077
+ [cx, cy] = args
1078
+ ty = sig.args[0]
1079
+ x = context.make_complex(builder, ty, value=cx)
1080
+ y = context.make_complex(builder, ty, value=cy)
1081
+ z = context.make_complex(builder, ty)
1082
+ a = x.real
1083
+ b = x.imag
1084
+ c = y.real
1085
+ d = y.imag
1086
+ z.real = builder.fadd(a, c)
1087
+ z.imag = builder.fadd(b, d)
1088
+ res = z._getvalue()
1089
+ return impl_ret_untracked(context, builder, sig.return_type, res)
1090
+
1091
+
1092
+ def complex_sub_impl(context, builder, sig, args):
1093
+ [cx, cy] = args
1094
+ ty = sig.args[0]
1095
+ x = context.make_complex(builder, ty, value=cx)
1096
+ y = context.make_complex(builder, ty, value=cy)
1097
+ z = context.make_complex(builder, ty)
1098
+ a = x.real
1099
+ b = x.imag
1100
+ c = y.real
1101
+ d = y.imag
1102
+ z.real = builder.fsub(a, c)
1103
+ z.imag = builder.fsub(b, d)
1104
+ res = z._getvalue()
1105
+ return impl_ret_untracked(context, builder, sig.return_type, res)
1106
+
1107
+
1108
+ def complex_mul_impl(context, builder, sig, args):
1109
+ """
1110
+ (a+bi)(c+di)=(ac-bd)+i(ad+bc)
1111
+ """
1112
+ [cx, cy] = args
1113
+ ty = sig.args[0]
1114
+ x = context.make_complex(builder, ty, value=cx)
1115
+ y = context.make_complex(builder, ty, value=cy)
1116
+ z = context.make_complex(builder, ty)
1117
+ a = x.real
1118
+ b = x.imag
1119
+ c = y.real
1120
+ d = y.imag
1121
+ ac = builder.fmul(a, c)
1122
+ bd = builder.fmul(b, d)
1123
+ ad = builder.fmul(a, d)
1124
+ bc = builder.fmul(b, c)
1125
+ z.real = builder.fsub(ac, bd)
1126
+ z.imag = builder.fadd(ad, bc)
1127
+ res = z._getvalue()
1128
+ return impl_ret_untracked(context, builder, sig.return_type, res)
1129
+
1130
+
1131
+ NAN = float("nan")
1132
+
1133
+
1134
+ def complex_div_impl(context, builder, sig, args):
1135
+ def complex_div(a, b):
1136
+ # This is CPython's algorithm (in _Py_c_quot()).
1137
+ areal = a.real
1138
+ aimag = a.imag
1139
+ breal = b.real
1140
+ bimag = b.imag
1141
+ if not breal and not bimag:
1142
+ raise ZeroDivisionError("complex division by zero")
1143
+ if abs(breal) >= abs(bimag):
1144
+ # Divide tops and bottom by b.real
1145
+ if not breal:
1146
+ return complex(NAN, NAN)
1147
+ ratio = bimag / breal
1148
+ denom = breal + bimag * ratio
1149
+ return complex(
1150
+ (areal + aimag * ratio) / denom, (aimag - areal * ratio) / denom
1151
+ )
1152
+ else:
1153
+ # Divide tops and bottom by b.imag
1154
+ if not bimag:
1155
+ return complex(NAN, NAN)
1156
+ ratio = breal / bimag
1157
+ denom = breal * ratio + bimag
1158
+ return complex(
1159
+ (a.real * ratio + a.imag) / denom,
1160
+ (a.imag * ratio - a.real) / denom,
1161
+ )
1162
+
1163
+ res = context.compile_internal(builder, complex_div, sig, args)
1164
+ return impl_ret_untracked(context, builder, sig.return_type, res)
1165
+
1166
+
1167
+ def complex_negate_impl(context, builder, sig, args):
1168
+ from numba.cuda.cpython import mathimpl
1169
+
1170
+ [typ] = sig.args
1171
+ [val] = args
1172
+ cmplx = context.make_complex(builder, typ, value=val)
1173
+ res = context.make_complex(builder, typ)
1174
+ res.real = mathimpl.negate_real(builder, cmplx.real)
1175
+ res.imag = mathimpl.negate_real(builder, cmplx.imag)
1176
+ res = res._getvalue()
1177
+ return impl_ret_untracked(context, builder, sig.return_type, res)
1178
+
1179
+
1180
+ def complex_positive_impl(context, builder, sig, args):
1181
+ [val] = args
1182
+ return impl_ret_untracked(context, builder, sig.return_type, val)
1183
+
1184
+
1185
+ def complex_eq_impl(context, builder, sig, args):
1186
+ [cx, cy] = args
1187
+ typ = sig.args[0]
1188
+ x = context.make_complex(builder, typ, value=cx)
1189
+ y = context.make_complex(builder, typ, value=cy)
1190
+
1191
+ reals_are_eq = builder.fcmp_ordered("==", x.real, y.real)
1192
+ imags_are_eq = builder.fcmp_ordered("==", x.imag, y.imag)
1193
+ res = builder.and_(reals_are_eq, imags_are_eq)
1194
+ return impl_ret_untracked(context, builder, sig.return_type, res)
1195
+
1196
+
1197
+ def complex_ne_impl(context, builder, sig, args):
1198
+ [cx, cy] = args
1199
+ typ = sig.args[0]
1200
+ x = context.make_complex(builder, typ, value=cx)
1201
+ y = context.make_complex(builder, typ, value=cy)
1202
+
1203
+ reals_are_ne = builder.fcmp_unordered("!=", x.real, y.real)
1204
+ imags_are_ne = builder.fcmp_unordered("!=", x.imag, y.imag)
1205
+ res = builder.or_(reals_are_ne, imags_are_ne)
1206
+ return impl_ret_untracked(context, builder, sig.return_type, res)
1207
+
1208
+
1209
+ def complex_abs_impl(context, builder, sig, args):
1210
+ """
1211
+ abs(z) := hypot(z.real, z.imag)
1212
+ """
1213
+
1214
+ def complex_abs(z):
1215
+ return math.hypot(z.real, z.imag)
1216
+
1217
+ res = context.compile_internal(builder, complex_abs, sig, args)
1218
+ return impl_ret_untracked(context, builder, sig.return_type, res)
1219
+
1220
+
1221
+ # ty = types.Complex
1222
+
1223
+ # lower_builtin(operator.add, ty, ty)(complex_add_impl)
1224
+ # lower_builtin(operator.iadd, ty, ty)(complex_add_impl)
1225
+ # lower_builtin(operator.sub, ty, ty)(complex_sub_impl)
1226
+ # lower_builtin(operator.isub, ty, ty)(complex_sub_impl)
1227
+ # lower_builtin(operator.mul, ty, ty)(complex_mul_impl)
1228
+ # lower_builtin(operator.imul, ty, ty)(complex_mul_impl)
1229
+ # lower_builtin(operator.truediv, ty, ty)(complex_div_impl)
1230
+ # lower_builtin(operator.itruediv, ty, ty)(complex_div_impl)
1231
+ # lower_builtin(operator.neg, ty)(complex_negate_impl)
1232
+ # lower_builtin(operator.pos, ty)(complex_positive_impl)
1233
+ # # Complex modulo is deprecated in python3
1234
+
1235
+ # lower_builtin(operator.eq, ty, ty)(complex_eq_impl)
1236
+ # lower_builtin(operator.ne, ty, ty)(complex_ne_impl)
1237
+
1238
+ # lower_builtin(abs, ty)(complex_abs_impl)
1239
+
1240
+ # del ty
1241
+
1242
+
1243
+ # @lower_builtin("number.item", types.Boolean)
1244
+ # @lower_builtin("number.item", types.Number)
1245
+ def number_item_impl(context, builder, sig, args):
1246
+ """
1247
+ The no-op .item() method on booleans and numbers.
1248
+ """
1249
+ return args[0]
1250
+
1251
+
1252
+ # ------------------------------------------------------------------------------
1253
+
1254
+
1255
+ def number_not_impl(context, builder, sig, args):
1256
+ [typ] = sig.args
1257
+ [val] = args
1258
+ istrue = context.cast(builder, val, typ, sig.return_type)
1259
+ res = builder.not_(istrue)
1260
+ return impl_ret_untracked(context, builder, sig.return_type, res)
1261
+
1262
+
1263
+ # @lower_builtin(bool, types.Boolean)
1264
+ def bool_as_bool(context, builder, sig, args):
1265
+ [val] = args
1266
+ return val
1267
+
1268
+
1269
+ # @lower_builtin(bool, types.Integer)
1270
+ def int_as_bool(context, builder, sig, args):
1271
+ [val] = args
1272
+ return builder.icmp_unsigned("!=", val, Constant(val.type, 0))
1273
+
1274
+
1275
+ # @lower_builtin(bool, types.Float)
1276
+ def float_as_bool(context, builder, sig, args):
1277
+ [val] = args
1278
+ return builder.fcmp_unordered("!=", val, Constant(val.type, 0.0))
1279
+
1280
+
1281
+ # @lower_builtin(bool, types.Complex)
1282
+ def complex_as_bool(context, builder, sig, args):
1283
+ [typ] = sig.args
1284
+ [val] = args
1285
+ cmplx = context.make_complex(builder, typ, val)
1286
+ real, imag = cmplx.real, cmplx.imag
1287
+ zero = Constant(real.type, 0.0)
1288
+ real_istrue = builder.fcmp_unordered("!=", real, zero)
1289
+ imag_istrue = builder.fcmp_unordered("!=", imag, zero)
1290
+ return builder.or_(real_istrue, imag_istrue)
1291
+
1292
+
1293
+ # for ty in (types.Integer, types.Float, types.Complex):
1294
+ # lower_builtin(operator.not_, ty)(number_not_impl)
1295
+
1296
+ # lower_builtin(operator.not_, types.boolean)(number_not_impl)
1297
+
1298
+
1299
+ # ------------------------------------------------------------------------------
1300
+ # Hashing numbers, see hashing.py
1301
+
1302
+ # -------------------------------------------------------------------------------
1303
+ # Implicit casts between numerics
1304
+
1305
+
1306
+ # @lower_cast(types.IntegerLiteral, types.Integer)
1307
+ # @lower_cast(types.IntegerLiteral, types.Float)
1308
+ # @lower_cast(types.IntegerLiteral, types.Complex)
1309
+ def literal_int_to_number(context, builder, fromty, toty, val):
1310
+ lit = context.get_constant_generic(
1311
+ builder,
1312
+ fromty.literal_type,
1313
+ fromty.literal_value,
1314
+ )
1315
+ return context.cast(builder, lit, fromty.literal_type, toty)
1316
+
1317
+
1318
+ # @lower_cast(types.Integer, types.Integer)
1319
+ def integer_to_integer(context, builder, fromty, toty, val):
1320
+ if toty.bitwidth == fromty.bitwidth:
1321
+ # Just a change of signedness
1322
+ return val
1323
+ elif toty.bitwidth < fromty.bitwidth:
1324
+ # Downcast
1325
+ return builder.trunc(val, context.get_value_type(toty))
1326
+ elif fromty.signed:
1327
+ # Signed upcast
1328
+ return builder.sext(val, context.get_value_type(toty))
1329
+ else:
1330
+ # Unsigned upcast
1331
+ return builder.zext(val, context.get_value_type(toty))
1332
+
1333
+
1334
+ # @lower_cast(types.Integer, types.voidptr)
1335
+ def integer_to_voidptr(context, builder, fromty, toty, val):
1336
+ return builder.inttoptr(val, context.get_value_type(toty))
1337
+
1338
+
1339
+ # @lower_cast(types.Float, types.Float)
1340
+ def float_to_float(context, builder, fromty, toty, val):
1341
+ lty = context.get_value_type(toty)
1342
+ if fromty.bitwidth < toty.bitwidth:
1343
+ return builder.fpext(val, lty)
1344
+ else:
1345
+ return builder.fptrunc(val, lty)
1346
+
1347
+
1348
+ # @lower_cast(types.Integer, types.Float)
1349
+ def integer_to_float(context, builder, fromty, toty, val):
1350
+ lty = context.get_value_type(toty)
1351
+ if fromty.signed:
1352
+ return builder.sitofp(val, lty)
1353
+ else:
1354
+ return builder.uitofp(val, lty)
1355
+
1356
+
1357
+ # @lower_cast(types.Float, types.Integer)
1358
+ def float_to_integer(context, builder, fromty, toty, val):
1359
+ lty = context.get_value_type(toty)
1360
+ if toty.signed:
1361
+ return builder.fptosi(val, lty)
1362
+ else:
1363
+ return builder.fptoui(val, lty)
1364
+
1365
+
1366
+ # @lower_cast(types.Float, types.Complex)
1367
+ # @lower_cast(types.Integer, types.Complex)
1368
+ def non_complex_to_complex(context, builder, fromty, toty, val):
1369
+ real = context.cast(builder, val, fromty, toty.underlying_float)
1370
+ imag = context.get_constant(toty.underlying_float, 0)
1371
+
1372
+ cmplx = context.make_complex(builder, toty)
1373
+ cmplx.real = real
1374
+ cmplx.imag = imag
1375
+ return cmplx._getvalue()
1376
+
1377
+
1378
+ # @lower_cast(types.Complex, types.Complex)
1379
+ def complex_to_complex(context, builder, fromty, toty, val):
1380
+ srcty = fromty.underlying_float
1381
+ dstty = toty.underlying_float
1382
+
1383
+ src = context.make_complex(builder, fromty, value=val)
1384
+ dst = context.make_complex(builder, toty)
1385
+ dst.real = context.cast(builder, src.real, srcty, dstty)
1386
+ dst.imag = context.cast(builder, src.imag, srcty, dstty)
1387
+ return dst._getvalue()
1388
+
1389
+
1390
+ # @lower_cast(types.Any, types.Boolean)
1391
+ def any_to_boolean(context, builder, fromty, toty, val):
1392
+ return context.is_true(builder, fromty, val)
1393
+
1394
+
1395
+ # @lower_cast(types.Boolean, types.Number)
1396
+ def boolean_to_any(context, builder, fromty, toty, val):
1397
+ # Casting from boolean to anything first casts to int32
1398
+ asint = builder.zext(val, ir.IntType(32))
1399
+ return context.cast(builder, asint, types.int32, toty)
1400
+
1401
+
1402
+ # @lower_cast(types.IntegerLiteral, types.Boolean)
1403
+ # @lower_cast(types.BooleanLiteral, types.Boolean)
1404
+ def literal_int_to_boolean(context, builder, fromty, toty, val):
1405
+ lit = context.get_constant_generic(
1406
+ builder,
1407
+ fromty.literal_type,
1408
+ fromty.literal_value,
1409
+ )
1410
+ return context.is_true(builder, fromty.literal_type, lit)
1411
+
1412
+
1413
+ # -------------------------------------------------------------------------------
1414
+ # Constants
1415
+
1416
+
1417
+ # @lower_constant(types.Complex)
1418
+ def constant_complex(context, builder, ty, pyval):
1419
+ fty = ty.underlying_float
1420
+ real = context.get_constant_generic(builder, fty, pyval.real)
1421
+ imag = context.get_constant_generic(builder, fty, pyval.imag)
1422
+ return Constant.literal_struct((real, imag))
1423
+
1424
+
1425
+ # @lower_constant(types.Integer)
1426
+ # @lower_constant(types.Float)
1427
+ # @lower_constant(types.Boolean)
1428
+ def constant_integer(context, builder, ty, pyval):
1429
+ # See https://github.com/numba/numba/issues/6979
1430
+ # llvmlite ir.IntType specialises the formatting of the constant for a
1431
+ # cpython bool. A NumPy np.bool_ is not a cpython bool so force it to be one
1432
+ # so that the constant renders correctly!
1433
+ if isinstance(pyval, np.bool_):
1434
+ pyval = bool(pyval)
1435
+ lty = context.get_value_type(ty)
1436
+ return lty(pyval)
1437
+
1438
+
1439
+ # -------------------------------------------------------------------------------
1440
+ # View
1441
+
1442
+
1443
+ def scalar_view(scalar, viewty):
1444
+ """Typing for the np scalar 'view' method."""
1445
+ if isinstance(scalar, (types.Float, types.Integer)) and isinstance(
1446
+ viewty, types.abstract.DTypeSpec
1447
+ ):
1448
+ if scalar.bitwidth != viewty.dtype.bitwidth:
1449
+ raise errors.TypingError(
1450
+ "Changing the dtype of a 0d array is only supported if the "
1451
+ "itemsize is unchanged"
1452
+ )
1453
+
1454
+ def impl(scalar, viewty):
1455
+ return viewer(scalar, viewty)
1456
+
1457
+ return impl
1458
+
1459
+
1460
+ # overload_method(types.Float, 'view')(scalar_view)
1461
+ # overload_method(types.Integer, 'view')(scalar_view)