-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathnpyfuncs.py
1707 lines (1287 loc) · 61.6 KB
/
npyfuncs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Codegen for functions used as kernels in NumPy functions
Typically, the kernels of several ufuncs that can't map directly to
Python builtins
"""
import math
import llvmlite.ir
import numpy as np
from numba.core.extending import overload
from numba.core.imputils import impl_ret_untracked
from numba.core import typing, types, errors, lowering, cgutils, config
from numba.core.extending import register_jitable
from numba.np import npdatetime
from numba.np.math import cmathimpl, mathimpl, numbers
from numba.np.numpy_support import numpy_version
# some NumPy constants. Note that we could generate some of them using
# the math library, but having the values copied from npy_math seems to
# yield more accurate results
_NPY_LOG2E = 1.442695040888963407359924681001892137 # math.log(math.e, 2)
_NPY_LOG10E = 0.434294481903251827651128918916605082 # math.log(math.e, 10)
_NPY_LOGE2 = 0.693147180559945309417232121458176568 # math.log(2)
def _check_arity_and_homogeneity(sig, args, arity, return_type = None):
"""checks that the following are true:
- args and sig.args have arg_count elements
- all input types are homogeneous
- return type is 'return_type' if provided, otherwise it must be
homogeneous with the input types.
"""
assert len(args) == arity
assert len(sig.args) == arity
ty = sig.args[0]
if return_type is None:
return_type = ty
# must have homogeneous args
if not( all(arg==ty for arg in sig.args) and sig.return_type == return_type):
import inspect
fname = inspect.currentframe().f_back.f_code.co_name
msg = '{0} called with invalid types: {1}'.format(fname, sig)
assert False, msg
if config.USE_LEGACY_TYPE_SYSTEM:
cast_arg_ty = types.float64
else:
cast_arg_ty = types.np_float64
def _call_func_by_name_with_cast(context, builder, sig, args,
func_name, ty=cast_arg_ty):
# it is quite common in NumPy to have loops implemented as a call
# to the double version of the function, wrapped in casts. This
# helper function facilitates that.
mod = builder.module
lty = context.get_argument_type(ty)
fnty = llvmlite.ir.FunctionType(lty, [lty]*len(sig.args))
fn = cgutils.insert_pure_function(mod, fnty, name=func_name)
cast_args = [context.cast(builder, arg, argty, ty)
for arg, argty in zip(args, sig.args) ]
result = builder.call(fn, cast_args)
return context.cast(builder, result, types.float64, sig.return_type)
def _dispatch_func_by_name_type(context, builder, sig, args, table, user_name):
# for most cases the functions are homogeneous on all their types.
# this code dispatches on the first argument type as it is the most useful
# for our uses (all cases but ldexp are homogeneous in all types, and
# dispatching on the first argument type works of ldexp as well)
#
# assumes that the function pointed by func_name has the type
# signature sig (but needs translation to llvm types).
ty = sig.args[0]
try:
func_name = table[ty]
except KeyError as e:
msg = "No {0} function for real type {1}".format(user_name, str(e))
raise errors.LoweringError(msg)
mod = builder.module
if ty in types.complex_domain:
# In numba struct types are always passed by pointer. So the call has to
# be transformed from "result = func(ops...)" to "func(&result, ops...).
# note that the result value pointer as first argument is the convention
# used by numba.
# First, prepare the return value
out = context.make_complex(builder, ty)
ptrargs = [cgutils.alloca_once_value(builder, arg)
for arg in args]
call_args = [out._getpointer()] + ptrargs
# get_value_as_argument for struct types like complex allocate stack space
# and initialize with the value, the return value is the pointer to that
# allocated space (ie: pointer to a copy of the value in the stack).
# get_argument_type returns a pointer to the struct type in consonance.
call_argtys = [ty] + list(sig.args)
call_argltys = [context.get_value_type(ty).as_pointer()
for ty in call_argtys]
fnty = llvmlite.ir.FunctionType(llvmlite.ir.VoidType(), call_argltys)
# Note: the function isn't pure here (it writes to its pointer args)
fn = cgutils.get_or_insert_function(mod, fnty, func_name)
builder.call(fn, call_args)
retval = builder.load(call_args[0])
else:
argtypes = [context.get_argument_type(aty) for aty in sig.args]
restype = context.get_argument_type(sig.return_type)
fnty = llvmlite.ir.FunctionType(restype, argtypes)
fn = cgutils.insert_pure_function(mod, fnty, name=func_name)
retval = context.call_external_function(builder, fn, sig.args, args)
return retval
########################################################################
# Division kernels inspired by NumPy loops.c.src code
#
# The builtins are not applicable as they rely on a test for zero in the
# denominator. If it is zero the appropriate exception is raised.
# In NumPy, a division by zero does not raise an exception, but instead
# generated a known value. Note that a division by zero in any of the
# operations of a vector may raise an exception or issue a warning
# depending on the np.seterr configuration. This is not supported
# right now (and in any case, it won't be handled by these functions
# either)
def np_int_sdiv_impl(context, builder, sig, args):
# based on the actual code in NumPy loops.c.src for signed integer types
_check_arity_and_homogeneity(sig, args, 2)
num, den = args
ty = sig.args[0] # any arg type will do, homogeneous
ZERO = context.get_constant(ty, 0)
MINUS_ONE = context.get_constant(ty, -1)
MIN_INT = context.get_constant(ty, 1 << (den.type.width-1))
den_is_zero = builder.icmp_unsigned('==', ZERO, den)
den_is_minus_one = builder.icmp_unsigned('==', MINUS_ONE, den)
num_is_min_int = builder.icmp_unsigned('==', MIN_INT, num)
could_cause_sigfpe = builder.and_(den_is_minus_one, num_is_min_int)
force_zero = builder.or_(den_is_zero, could_cause_sigfpe)
with builder.if_else(force_zero, likely=False) as (then, otherwise):
with then:
bb_then = builder.basic_block
with otherwise:
bb_otherwise = builder.basic_block
div = builder.sdiv(num, den)
mod = builder.srem(num, den)
num_gt_zero = builder.icmp_signed('>', num, ZERO)
den_gt_zero = builder.icmp_signed('>', den, ZERO)
not_same_sign = builder.xor(num_gt_zero, den_gt_zero)
mod_not_zero = builder.icmp_unsigned('!=', mod, ZERO)
needs_fixing = builder.and_(not_same_sign, mod_not_zero)
fix_value = builder.select(needs_fixing, MINUS_ONE, ZERO)
result_otherwise = builder.add(div, fix_value)
result = builder.phi(ZERO.type)
result.add_incoming(ZERO, bb_then)
result.add_incoming(result_otherwise, bb_otherwise)
return result
def np_int_srem_impl(context, builder, sig, args):
# based on the actual code in NumPy loops.c.src for signed integers
_check_arity_and_homogeneity(sig, args, 2)
num, den = args
ty = sig.args[0] # any arg type will do, homogeneous
ZERO = context.get_constant(ty, 0)
den_not_zero = builder.icmp_unsigned('!=', ZERO, den)
bb_no_if = builder.basic_block
with cgutils.if_unlikely(builder, den_not_zero):
bb_if = builder.basic_block
mod = builder.srem(num,den)
num_gt_zero = builder.icmp_signed('>', num, ZERO)
den_gt_zero = builder.icmp_signed('>', den, ZERO)
not_same_sign = builder.xor(num_gt_zero, den_gt_zero)
mod_not_zero = builder.icmp_unsigned('!=', mod, ZERO)
needs_fixing = builder.and_(not_same_sign, mod_not_zero)
fix_value = builder.select(needs_fixing, den, ZERO)
final_mod = builder.add(fix_value, mod)
result = builder.phi(ZERO.type)
result.add_incoming(ZERO, bb_no_if)
result.add_incoming(final_mod, bb_if)
return result
def np_int_sdivrem_impl(context, builder, sig, args):
div = np_int_sdiv_impl(context, builder, sig.return_type[0](*sig.args), args)
rem = np_int_srem_impl(context, builder, sig.return_type[1](*sig.args), args)
return context.make_tuple(builder, sig.return_type, [div, rem])
def np_int_udiv_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 2)
num, den = args
ty = sig.args[0] # any arg type will do, homogeneous
ZERO = context.get_constant(ty, 0)
div_by_zero = builder.icmp_unsigned('==', ZERO, den)
with builder.if_else(div_by_zero, likely=False) as (then, otherwise):
with then:
# division by zero
bb_then = builder.basic_block
with otherwise:
# divide!
div = builder.udiv(num, den)
bb_otherwise = builder.basic_block
result = builder.phi(ZERO.type)
result.add_incoming(ZERO, bb_then)
result.add_incoming(div, bb_otherwise)
return result
def np_int_urem_impl(context, builder, sig, args):
# based on the actual code in NumPy loops.c.src for signed integers
_check_arity_and_homogeneity(sig, args, 2)
num, den = args
ty = sig.args[0] # any arg type will do, homogeneous
ZERO = context.get_constant(ty, 0)
den_not_zero = builder.icmp_unsigned('!=', ZERO, den)
bb_no_if = builder.basic_block
with cgutils.if_unlikely(builder, den_not_zero):
bb_if = builder.basic_block
mod = builder.urem(num,den)
result = builder.phi(ZERO.type)
result.add_incoming(ZERO, bb_no_if)
result.add_incoming(mod, bb_if)
return result
def np_int_udivrem_impl(context, builder, sig, args):
div = np_int_udiv_impl(context, builder, sig.return_type[0](*sig.args), args)
rem = np_int_urem_impl(context, builder, sig.return_type[1](*sig.args), args)
return context.make_tuple(builder, sig.return_type, [div, rem])
# implementation of int_fmod is in fact the same as the unsigned remainder,
# that is: srem with a special case returning 0 when the denominator is 0.
np_int_fmod_impl = np_int_urem_impl
def np_real_div_impl(context, builder, sig, args):
# in NumPy real div has the same semantics as an fdiv for generating
# NANs, INF and NINF
_check_arity_and_homogeneity(sig, args, 2)
return builder.fdiv(*args)
def np_real_mod_impl(context, builder, sig, args):
# note: this maps to NumPy remainder, which has the same semantics as Python
# based on code in loops.c.src
_check_arity_and_homogeneity(sig, args, 2)
in1, in2 = args
ty = sig.args[0]
ZERO = context.get_constant(ty, 0.0)
res = builder.frem(in1, in2)
res_ne_zero = builder.fcmp_ordered('!=', res, ZERO)
den_lt_zero = builder.fcmp_ordered('<', in2, ZERO)
res_lt_zero = builder.fcmp_ordered('<', res, ZERO)
needs_fixing = builder.and_(res_ne_zero,
builder.xor(den_lt_zero, res_lt_zero))
fix_value = builder.select(needs_fixing, in2, ZERO)
return builder.fadd(res, fix_value)
def np_real_fmod_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 2)
return builder.frem(*args)
def _fabs(context, builder, arg):
ZERO = llvmlite.ir.Constant(arg.type, 0.0)
arg_negated = builder.fsub(ZERO, arg)
arg_is_negative = builder.fcmp_ordered('<', arg, ZERO)
return builder.select(arg_is_negative, arg_negated, arg)
def np_complex_div_impl(context, builder, sig, args):
# Extracted from numpy/core/src/umath/loops.c.src,
# inspired by complex_div_impl
# variables named coherent with loops.c.src
# This is implemented using the approach described in
# R.L. Smith. Algorithm 116: Complex division.
# Communications of the ACM, 5(8):435, 1962
in1, in2 = [context.make_complex(builder, sig.args[0], value=arg)
for arg in args]
in1r = in1.real # numerator.real
in1i = in1.imag # numerator.imag
in2r = in2.real # denominator.real
in2i = in2.imag # denominator.imag
ftype = in1r.type
assert all([i.type==ftype for i in [in1r, in1i, in2r, in2i]]), "mismatched types"
out = context.make_helper(builder, sig.return_type)
ZERO = llvmlite.ir.Constant(ftype, 0.0)
ONE = llvmlite.ir.Constant(ftype, 1.0)
# if abs(denominator.real) >= abs(denominator.imag)
in2r_abs = _fabs(context, builder, in2r)
in2i_abs = _fabs(context, builder, in2i)
in2r_abs_ge_in2i_abs = builder.fcmp_ordered('>=', in2r_abs, in2i_abs)
with builder.if_else(in2r_abs_ge_in2i_abs) as (then, otherwise):
with then:
# if abs(denominator.real) == 0 and abs(denominator.imag) == 0
in2r_is_zero = builder.fcmp_ordered('==', in2r_abs, ZERO)
in2i_is_zero = builder.fcmp_ordered('==', in2i_abs, ZERO)
in2_is_zero = builder.and_(in2r_is_zero, in2i_is_zero)
with builder.if_else(in2_is_zero) as (inn_then, inn_otherwise):
with inn_then:
# division by 0.
# fdiv generates the appropriate NAN/INF/NINF
out.real = builder.fdiv(in1r, in2r_abs)
out.imag = builder.fdiv(in1i, in2i_abs)
with inn_otherwise:
# general case for:
# abs(denominator.real) > abs(denominator.imag)
rat = builder.fdiv(in2i, in2r)
# scl = 1.0/(in2r + in2i*rat)
tmp1 = builder.fmul(in2i, rat)
tmp2 = builder.fadd(in2r, tmp1)
scl = builder.fdiv(ONE, tmp2)
# out.real = (in1r + in1i*rat)*scl
# out.imag = (in1i - in1r*rat)*scl
tmp3 = builder.fmul(in1i, rat)
tmp4 = builder.fmul(in1r, rat)
tmp5 = builder.fadd(in1r, tmp3)
tmp6 = builder.fsub(in1i, tmp4)
out.real = builder.fmul(tmp5, scl)
out.imag = builder.fmul(tmp6, scl)
with otherwise:
# general case for:
# abs(denominator.imag) > abs(denominator.real)
rat = builder.fdiv(in2r, in2i)
# scl = 1.0/(in2i + in2r*rat)
tmp1 = builder.fmul(in2r, rat)
tmp2 = builder.fadd(in2i, tmp1)
scl = builder.fdiv(ONE, tmp2)
# out.real = (in1r*rat + in1i)*scl
# out.imag = (in1i*rat - in1r)*scl
tmp3 = builder.fmul(in1r, rat)
tmp4 = builder.fmul(in1i, rat)
tmp5 = builder.fadd(tmp3, in1i)
tmp6 = builder.fsub(tmp4, in1r)
out.real = builder.fmul(tmp5, scl)
out.imag = builder.fmul(tmp6, scl)
return out._getvalue()
########################################################################
# NumPy logaddexp
def _npy_logaddexp(x1, x2):
pass
def _generate_logaddexp(fnoverload, const, log1pfn, expfn):
# Code generation for logaddexp and logaddexp2 is based on:
# https://github.com/numpy/numpy/blob/12c2b7dd62fc0c14b81c8892ed5f4f59cc94d09c/numpy/core/src/npymath/npy_math_internal.h.src#L467-L507
@overload(fnoverload, target='generic')
def ol_npy_logaddexp(x1, x2):
if x1 != x2:
return
shift = x1(const)
def impl(x1, x2):
x, y = x1, x2
if (x == y):
# Handles infinities of the same sign without warnings
return x + shift
else:
tmp = x - y
if (tmp > 0):
return x + log1pfn(expfn(-tmp))
elif (tmp <= 0):
return y + log1pfn(expfn(tmp))
else:
# NaN
return tmp
return impl
def _npy_logaddexp(x1, x2):
pass
_generate_logaddexp(_npy_logaddexp, _NPY_LOGE2, np.log1p, np.exp)
def np_real_logaddexp_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 2)
fnty = context.typing_context.resolve_value_type(_npy_logaddexp)
sig = fnty.get_call_type(context.typing_context, (*sig.args,), {})
impl = context.get_function(fnty, sig)
return impl(builder, args)
########################################################################
# NumPy logaddexp2
def _npy_logaddexp2(x1, x2):
pass
def npy_log2_1p(x):
pass
# The following npy_log2_1p function is a translation of:
# https://github.com/numpy/numpy/blob/12c2b7dd62fc0c14b81c8892ed5f4f59cc94d09c/numpy/core/src/npymath/npy_math_internal.h.src#L457-L460
@overload(npy_log2_1p, target='generic')
def ol_npy_log2_1p(x):
LOG2E = x(_NPY_LOG2E)
def impl(x):
return LOG2E * np.log1p(x)
return impl
_generate_logaddexp(_npy_logaddexp2, 1.0, npy_log2_1p, np.exp2)
def np_real_logaddexp2_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 2)
fnty = context.typing_context.resolve_value_type(_npy_logaddexp2)
sig = fnty.get_call_type(context.typing_context, (*sig.args,), {})
impl = context.get_function(fnty, sig)
return impl(builder, args)
########################################################################
# true div kernels
def np_int_truediv_impl(context, builder, sig, args):
# in NumPy we don't check for 0 denominator... fdiv handles div by
# 0 in the way NumPy expects..
# integer truediv always yields double
num, den = args
lltype = num.type
assert all(i.type==lltype for i in args), "must have homogeneous types"
numty, denty = sig.args
num = context.cast(builder, num, numty, types.float64)
den = context.cast(builder, den, denty, types.float64)
return builder.fdiv(num,den)
########################################################################
# floor div kernels
def np_real_floor_div_impl(context, builder, sig, args):
res = np_real_div_impl(context, builder, sig, args)
s = typing.signature(sig.return_type, sig.return_type)
return np_real_floor_impl(context, builder, s, (res,))
def np_real_divmod_impl(context, builder, sig, args):
div = np_real_floor_div_impl(context, builder, sig.return_type[0](*sig.args), args)
rem = np_real_mod_impl(context, builder, sig.return_type[1](*sig.args), args)
return context.make_tuple(builder, sig.return_type, [div, rem])
def np_complex_floor_div_impl(context, builder, sig, args):
# this is based on the complex floor divide in Numpy's loops.c.src
# This is basically a full complex division with a complex floor
# applied.
# The complex floor seems to be defined as the real floor applied
# with the real part and zero in the imaginary part. Fully developed
# so it avoids computing anything related to the imaginary result.
float_kind = sig.args[0].underlying_float
floor_sig = typing.signature(float_kind, float_kind)
in1, in2 = [context.make_complex(builder, sig.args[0], value=arg)
for arg in args]
in1r = in1.real
in1i = in1.imag
in2r = in2.real
in2i = in2.imag
ftype = in1r.type
assert all([i.type==ftype for i in [in1r, in1i, in2r, in2i]]), "mismatched types"
ZERO = llvmlite.ir.Constant(ftype, 0.0)
out = context.make_helper(builder, sig.return_type)
out.imag = ZERO
in2r_abs = _fabs(context, builder, in2r)
in2i_abs = _fabs(context, builder, in2i)
in2r_abs_ge_in2i_abs = builder.fcmp_ordered('>=', in2r_abs, in2i_abs)
with builder.if_else(in2r_abs_ge_in2i_abs) as (then, otherwise):
with then:
rat = builder.fdiv(in2i, in2r)
# out.real = floor((in1r+in1i*rat)/(in2r + in2i*rat))
tmp1 = builder.fmul(in1i, rat)
tmp2 = builder.fmul(in2i, rat)
tmp3 = builder.fadd(in1r, tmp1)
tmp4 = builder.fadd(in2r, tmp2)
tmp5 = builder.fdiv(tmp3, tmp4)
out.real = np_real_floor_impl(context, builder, floor_sig, (tmp5,))
with otherwise:
rat = builder.fdiv(in2r, in2i)
# out.real = floor((in1i + in1r*rat)/(in2i + in2r*rat))
tmp1 = builder.fmul(in1r, rat)
tmp2 = builder.fmul(in2r, rat)
tmp3 = builder.fadd(in1i, tmp1)
tmp4 = builder.fadd(in2i, tmp2)
tmp5 = builder.fdiv(tmp3, tmp4)
out.real = np_real_floor_impl(context, builder, floor_sig, (tmp5,))
return out._getvalue()
########################################################################
# numpy power funcs
def np_complex_power_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 2)
return numbers.complex_power_impl(context, builder, sig, args)
########################################################################
# numpy float power funcs
def real_float_power_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 2)
return numbers.real_power_impl(context, builder, sig, args)
def np_complex_float_power_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 2)
return numbers.complex_power_impl(context, builder, sig, args)
########################################################################
# numpy greatest common denominator
def np_gcd_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 2)
return mathimpl.gcd_impl(context, builder, sig, args)
########################################################################
# numpy lowest common multiple
def np_lcm_impl(context, builder, sig, args):
xty, yty = sig.args
assert xty == yty == sig.return_type
x, y = args
def lcm(a, b):
"""
Like gcd, heavily cribbed from Julia.
"""
return 0 if a == 0 else abs(a * (b // np.gcd(b, a)))
res = context.compile_internal(builder, lcm, sig, args)
return impl_ret_untracked(context, builder, sig.return_type, res)
########################################################################
# Numpy style complex sign
def np_complex_sign_impl(context, builder, sig, args):
# equivalent to complex sign in NumPy's sign
# but implemented via selects, balancing the 4 cases.
_check_arity_and_homogeneity(sig, args, 1)
if numpy_version >= (2, 0):
# NumPy >= 2.0.0
def complex_sign(z):
abs = math.hypot(z.real, z.imag)
if abs == 0:
return 0 + 0j
else:
return z / abs
res = context.compile_internal(builder, complex_sign, sig, args)
return impl_ret_untracked(context, builder, sig.return_type, res)
else:
op = args[0]
ty = sig.args[0]
result = context.make_complex(builder, ty)
float_ty = ty.underlying_float
ZERO = context.get_constant(float_ty, 0.0)
ONE = context.get_constant(float_ty, 1.0)
MINUS_ONE = context.get_constant(float_ty, -1.0)
NAN = context.get_constant(float_ty, float('nan'))
result.real = ZERO
result.imag = ZERO
cmp_sig = typing.signature(types.boolean, *[ty] * 2)
cmp_args = [op, result._getvalue()]
arg1_ge_arg2 = np_complex_ge_impl(context, builder, cmp_sig, cmp_args)
arg1_eq_arg2 = np_complex_eq_impl(context, builder, cmp_sig, cmp_args)
arg1_lt_arg2 = np_complex_lt_impl(context, builder, cmp_sig, cmp_args)
real_when_ge = builder.select(arg1_eq_arg2, ZERO, ONE)
real_when_nge = builder.select(arg1_lt_arg2, MINUS_ONE, NAN)
result.real = builder.select(arg1_ge_arg2, real_when_ge, real_when_nge)
return result._getvalue()
########################################################################
# Numpy rint
def np_real_rint_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.call_fp_intrinsic(builder, 'llvm.rint', args)
def np_complex_rint_impl(context, builder, sig, args):
# based on code in NumPy's funcs.inc.src
# rint of a complex number defined as rint of its real and imag
# parts
_check_arity_and_homogeneity(sig, args, 1)
ty = sig.args[0]
float_ty = ty.underlying_float
in1 = context.make_complex(builder, ty, value=args[0])
out = context.make_complex(builder, ty)
inner_sig = typing.signature(*[float_ty]*2)
out.real = np_real_rint_impl(context, builder, inner_sig, [in1.real])
out.imag = np_real_rint_impl(context, builder, inner_sig, [in1.imag])
return out._getvalue()
########################################################################
# NumPy exp
def np_real_exp_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.exp_impl(context, builder, sig, args)
def np_complex_exp_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return cmathimpl.exp_impl(context, builder, sig, args)
########################################################################
# NumPy exp2
def np_real_exp2_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
ll_ty = args[0].type
fnty = llvmlite.ir.FunctionType(ll_ty, [ll_ty,])
fn = cgutils.insert_pure_function(builder.module, fnty,
name='llvm.exp2')
return builder.call(fn, [args[0]])
def np_complex_exp2_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
ty = sig.args[0]
float_ty = ty.underlying_float
in1 = context.make_complex(builder, ty, value=args[0])
tmp = context.make_complex(builder, ty)
loge2 = context.get_constant(float_ty, _NPY_LOGE2)
tmp.real = builder.fmul(loge2, in1.real)
tmp.imag = builder.fmul(loge2, in1.imag)
return np_complex_exp_impl(context, builder, sig, [tmp._getvalue()])
########################################################################
# NumPy log
def np_real_log_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.log_impl(context, builder, sig, args)
def np_complex_log_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return cmathimpl.log_impl(context, builder, sig, args)
########################################################################
# NumPy log2
def np_real_log2_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
ll_ty = args[0].type
fnty = llvmlite.ir.FunctionType(ll_ty, [ll_ty,])
fn = cgutils.insert_pure_function(builder.module, fnty,
name='llvm.log2')
return builder.call(fn, [args[0]])
def np_complex_log2_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
ty = sig.args[0]
float_ty = ty.underlying_float
tmp = np_complex_log_impl(context, builder, sig, args)
tmp = context.make_complex(builder, ty, value=tmp)
log2e = context.get_constant(float_ty, _NPY_LOG2E)
tmp.real = builder.fmul(log2e, tmp.real)
tmp.imag = builder.fmul(log2e, tmp.imag)
return tmp._getvalue()
########################################################################
# NumPy log10
def np_real_log10_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.log10_impl(context, builder, sig, args)
def np_complex_log10_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
ty = sig.args[0]
float_ty = ty.underlying_float
tmp = np_complex_log_impl(context, builder, sig, args)
tmp = context.make_complex(builder, ty, value=tmp)
log10e = context.get_constant(float_ty, _NPY_LOG10E)
tmp.real = builder.fmul(log10e, tmp.real)
tmp.imag = builder.fmul(log10e, tmp.imag)
return tmp._getvalue()
########################################################################
# NumPy expm1
def np_real_expm1_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.expm1_impl(context, builder, sig, args)
def np_complex_expm1_impl(context, builder, sig, args):
# this is based on nc_expm1 in funcs.inc.src
_check_arity_and_homogeneity(sig, args, 1)
ty = sig.args[0]
float_ty = ty.underlying_float
float_unary_sig = typing.signature(*[float_ty]*2)
MINUS_ONE = context.get_constant(float_ty, -1.0)
in1 = context.make_complex(builder, ty, value=args[0])
a = np_real_exp_impl(context, builder, float_unary_sig, [in1.real])
out = context.make_complex(builder, ty)
cos_imag = np_real_cos_impl(context, builder, float_unary_sig, [in1.imag])
sin_imag = np_real_sin_impl(context, builder, float_unary_sig, [in1.imag])
tmp = builder.fmul(a, cos_imag)
out.imag = builder.fmul(a, sin_imag)
out.real = builder.fadd(tmp, MINUS_ONE)
return out._getvalue()
########################################################################
# NumPy log1p
def np_real_log1p_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.log1p_impl(context, builder, sig, args)
def np_complex_log1p_impl(context, builder, sig, args):
# base on NumPy's nc_log1p in funcs.inc.src
_check_arity_and_homogeneity(sig, args, 1)
ty = sig.args[0]
float_ty = ty.underlying_float
float_unary_sig = typing.signature(*[float_ty]*2)
float_binary_sig = typing.signature(*[float_ty]*3)
ONE = context.get_constant(float_ty, 1.0)
in1 = context.make_complex(builder, ty, value=args[0])
out = context.make_complex(builder, ty)
real_plus_one = builder.fadd(in1.real, ONE)
l = np_real_hypot_impl(context, builder, float_binary_sig,
[real_plus_one, in1.imag])
out.imag = np_real_atan2_impl(context, builder, float_binary_sig,
[in1.imag, real_plus_one])
out.real = np_real_log_impl(context, builder, float_unary_sig, [l])
return out._getvalue()
########################################################################
# NumPy sqrt
def np_real_sqrt_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.sqrt_impl(context, builder, sig, args)
def np_complex_sqrt_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return cmathimpl.sqrt_impl(context, builder, sig, args)
########################################################################
# NumPy square
def np_int_square_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return builder.mul(args[0], args[0])
def np_real_square_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return builder.fmul(args[0], args[0])
def np_complex_square_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
binary_sig = typing.signature(*[sig.return_type]*3)
return numbers.complex_mul_impl(context, builder, binary_sig,
[args[0], args[0]])
########################################################################
# NumPy cbrt
def np_real_cbrt_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
# We enable fastmath here to force np.power(x, 1/3) to generate a
# call to libm cbrt function
@register_jitable(fastmath=True)
def cbrt(x):
if x < 0:
return -np.power(-x, 1.0 / 3.0)
else:
return np.power(x, 1.0 / 3.0)
def _cbrt(x):
if np.isnan(x):
return np.nan
return cbrt(x)
return context.compile_internal(builder, _cbrt, sig, args)
########################################################################
# NumPy reciprocal
def np_int_reciprocal_impl(context, builder, sig, args):
# based on the implementation in loops.c.src
# integer versions for reciprocal are performed via promotion
# using double, and then converted back to the type
_check_arity_and_homogeneity(sig, args, 1)
ty = sig.return_type
binary_sig = typing.signature(*[ty]*3)
in_as_float = context.cast(builder, args[0], ty, types.float64)
ONE = context.get_constant(types.float64, 1)
result_as_float = builder.fdiv(ONE, in_as_float)
return context.cast(builder, result_as_float, types.float64, ty)
def np_real_reciprocal_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
ONE = context.get_constant(sig.return_type, 1.0)
return builder.fdiv(ONE, args[0])
def np_complex_reciprocal_impl(context, builder, sig, args):
# based on the implementation in loops.c.src
# Basically the same Smith method used for division, but with
# the numerator substituted by 1.0
_check_arity_and_homogeneity(sig, args, 1)
ty = sig.args[0]
float_ty = ty.underlying_float
ZERO = context.get_constant(float_ty, 0.0)
ONE = context.get_constant(float_ty, 1.0)
in1 = context.make_complex(builder, ty, value=args[0])
out = context.make_complex(builder, ty)
in1r = in1.real
in1i = in1.imag
in1r_abs = _fabs(context, builder, in1r)
in1i_abs = _fabs(context, builder, in1i)
in1i_abs_le_in1r_abs = builder.fcmp_ordered('<=', in1i_abs, in1r_abs)
with builder.if_else(in1i_abs_le_in1r_abs) as (then, otherwise):
with then:
r = builder.fdiv(in1i, in1r)
tmp0 = builder.fmul(in1i, r)
d = builder.fadd(in1r, tmp0)
inv_d = builder.fdiv(ONE, d)
minus_r = builder.fsub(ZERO, r)
out.real = inv_d
out.imag = builder.fmul(minus_r, inv_d)
with otherwise:
r = builder.fdiv(in1r, in1i)
tmp0 = builder.fmul(in1r, r)
d = builder.fadd(tmp0, in1i)
inv_d = builder.fdiv(ONE, d)
out.real = builder.fmul(r, inv_d)
out.imag = builder.fsub(ZERO, inv_d)
return out._getvalue()
########################################################################
# NumPy sin
def np_real_sin_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.sin_impl(context, builder, sig, args)
def np_complex_sin_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return cmathimpl.sin_impl(context, builder, sig, args)
########################################################################
# NumPy cos
def np_real_cos_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.cos_impl(context, builder, sig, args)
def np_complex_cos_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return cmathimpl.cos_impl(context, builder, sig, args)
########################################################################
# NumPy tan
def np_real_tan_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.tan_impl(context, builder, sig, args)
########################################################################
# NumPy asin
def np_real_asin_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.asin_impl(context, builder, sig, args)
########################################################################
# NumPy acos
def np_real_acos_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.acos_impl(context, builder, sig, args)
########################################################################
# NumPy atan
def np_real_atan_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.atan_impl(context, builder, sig, args)
########################################################################
# NumPy atan2
def np_real_atan2_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 2)
return mathimpl.atan2_float_impl(context, builder, sig, args)
########################################################################
# NumPy hypot
def np_real_hypot_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 2)
return mathimpl.hypot_float_impl(context, builder, sig, args)
########################################################################
# NumPy sinh
def np_real_sinh_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
return mathimpl.sinh_impl(context, builder, sig, args)