interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / gpu / nvidia / om / components / cm_math_erfc.om

160 lines · 3758 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Positive continued fraction outside [0,2), double-double erf series inside.
3fn cm_erfc_small x f64 out edit ulong 2 to void
4p = new ulong 2 0
5q = new ulong 2 0
6r = new ulong 2 0
7put p 0 bitcast ulong x
8put out 0 bitcast ulong x
9put out 1 0
10cm_mul neg x f64 0.0 x f64 0.0 r
11for i ulong 64
12cm_dd_mul p r
13cm_dd_div_scalar p f64 add i 1
14cm_div bitcast f64 get p 0 bitcast f64 get p 1 f64 add mul i 2 3 f64 0.0 q
15cm_dd_add out q
16end
17cm_mul bitcast f64 get out 0 bitcast f64 get out 1 bitcast f64 ulong 4603256987541740397 bitcast f64 ulong 4350950281585880448 out
18cm_dd_scale out f64 -2.0
19cm_add f64 1.0 f64 0.0 bitcast f64 get out 0 bitcast f64 get out 1 out
20end
21fn cm_erfc_cf x f64 out edit ulong 2 to void
22q = new ulong 2 0
23put out 0 bitcast ulong x
24put out 1 0
25for j ulong 512
26i = sub 512 j
27cm_div mul f64 i f64 0.5 f64 0.0 bitcast f64 get out 0 bitcast f64 get out 1 q
28cm_add x f64 0.0 bitcast f64 get q 0 bitcast f64 get q 1 out
29end
30cm_div bitcast f64 ulong 4603256987541740397 bitcast f64 ulong 4350950281585880448 bitcast f64 get out 0 bitcast f64 get out 1 out
31end
32fn cm_erfc_reflect a edit ulong 2 to void
33cm_add f64 2.0 f64 0.0 neg bitcast f64 get a 0 neg bitcast f64 get a 1 a
34end
35fn cm_erfc bits ulong to ulong
36x = bitcast f64 and bits 9223372036854775807
37if ne x x
38ret bitcast ulong add x x
39end
40negative = ne and bits 9223372036854775808 0
41if ge x f64 28.0
42if negative
43ret 4611686018427387904
44end
45ret 0
46end
47if and negative ge x f64 6.0
48ret 4611686018427387904
49end
50out = new ulong 2 0
51if lt x f64 2.0
52cm_erfc_small x out
53if negative
54cm_erfc_reflect out
55end
56ret bitcast ulong add bitcast f64 get out 0 bitcast f64 get out 1
57end
58factor = new ulong 2 0
59cm_erfc_cf x factor
60cm_mul neg x f64 0.0 x f64 0.0 out
61h = bitcast f64 get out 0
62l = bitcast f64 get out 1
63k = cm_exp_reduce h l false out
64cm_dd_mul out factor
65if negative
66scale = bitcast f64 shl ulong add k 1023 52
67cm_dd_scale out scale
68cm_erfc_reflect out
69ret bitcast ulong add bitcast f64 get out 0 bitcast f64 get out 1
70end
71if lt k -1075
72ret 0
73end
74ret cm_scale_bits out k
75end
76fn cm_sqrt_core x f64 to f64
77bits = bitcast ulong x
78sig = or and bits 4503599627370495 4503599627370496
79shift = add 52 sub and shr bits 52 2047 1023
80root = ulong 0
81rest = ulong 0
82for i ulong 53
83top = sub 105 mul i 2
84bottom = sub top 1
85pair = ulong 0
86if ge top shift
87pair = shl and shr sig sub top shift 1 1
88end
89if ge bottom shift
90pair = or pair and shr sig sub bottom shift 1
91end
92rest = or shl rest 2 pair
93trial = or shl root 2 1
94root = shl root 1
95if ge rest trial
96rest = sub rest trial
97root = add root 1
98end
99end
100if gt rest root
101root = add root 1
102end
103ret mul f64 root bitcast f64 ulong 4372995238176751616
104end
105fn cm_sqrt bits ulong to ulong
106x = bitcast f64 bits
107if ne x x
108ret bitcast ulong add x x
109end
110if eq and bits 9223372036854775807 0
111ret bits
112end
113if lt x f64 0.0
114ret 9221120237041090560
115end
116if eq bits 9218868437227405312
117ret bits
118end
119adj = long 0
120if eq and shr bits 52 2047 0
121x = mul x bitcast f64 ulong 4850376798678024192
122bits = bitcast ulong x
123adj = -54
124end
125e = add sub long and shr bits 52 2047 1023 adj
126m = bitcast f64 or and bits 4503599627370495 4607182418800017408
127if ne and e 1 0
128m = mul m f64 2.0
129e = sub e 1
130end
131scale = bitcast f64 shl ulong add div e 2 1023 52
132ret bitcast ulong mul cm_sqrt_core m scale
133end
134fn cm_math mode ulong bits ulong to ulong
135if eq mode 0
136ret bitcast ulong cm_log bitcast f64 bits false
137end
138if eq mode 1
139ret bitcast ulong cm_log bitcast f64 bits true
140end
141if eq mode 2
142ret cm_ceil bits
143end
144if eq mode 3
145ret cm_exp bits false
146end
147if eq mode 4
148ret cm_exp bits true
149end
150if eq mode 5
151ret cm_sqrt bits
152end
153if eq mode 7
154ret and bits 9223372036854775807
155end
156if eq mode 6
157ret cm_erfc bits
158end
159ret 9221120237041090560
160end