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 / api / attn_math.om

181 lines · 4403 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3note Preserve src/omega_gpu_hostmath.c operation order and its filed fdlibm constants.
4fn av_pow2 k long to f64
5if gt k 1023
6ret bitcast f64 ulong 9218868437227405312
7end
8if lt k -1074
9ret bitcast f64 ulong 0
10end
11if ge k -1022
12ret bitcast f64 shl ulong add k 1023 52
13end
14ret mul bitcast f64 shl ulong 1 52 bitcast f64 shl ulong add k 2045 52
15end
16fn av_log x f64 to f64
17if or not gt x bitcast f64 ulong 0 gt x bitcast f64 ulong 9218868437227405311
18ret bitcast f64 ulong 9221120237041090560
19end
20e = long 0
21if lt x bitcast f64 ulong 4503599627370496
22x = mul x bitcast f64 ulong 4850376798678024192
23e = long -54
24end
25u = bitcast ulong x
26e = add e sub long and shr u 52 2047 1023
27m = bitcast f64 or and u 4503599627370495 4607182418800017408
28if gt m bitcast f64 ulong 4609047870845172685
29m = mul m bitcast f64 ulong 4602678819172646912
30e = add e 1
31end
32t = div sub m bitcast f64 ulong 4607182418800017408 add m bitcast f64 ulong 4607182418800017408
33t2 = mul t t
34s = bitcast f64 ulong 0
35k = long 25
36while ge k 1
37s = add mul s t2 div bitcast f64 ulong 4607182418800017408 f64 k
38k = sub k 2
39end
40x = mul f64 e bitcast f64 ulong 4461442080421002358
41s = mul mul bitcast f64 ulong 4611686018427387904 t s
42s = add s x
43x = mul f64 e bitcast f64 ulong 4604418534311723008
44ret add x s
45end
46fn av_exp y f64 to f64
47if ne y y
48ret y
49end
50if gt y bitcast f64 ulong 4649454506724284170
51ret bitcast f64 ulong 9218868437227405312
52end
53if lt y bitcast f64 ulong 13873138101193906586
54ret bitcast f64 ulong 0
55end
56fk = mul y bitcast f64 ulong 4609176140021203710
57round = bitcast f64 ulong 13826050856027422720
58if ge fk bitcast f64 ulong 0
59round = bitcast f64 ulong 4602678819172646912
60end
61k = long add fk round
62kd = f64 k
63r = sub sub y mul kd bitcast f64 ulong 4604418534311723008 mul kd bitcast f64 ulong 4461442080421002358
64p = bitcast f64 ulong 4607182418800017408
65n = long 17
66while ge n 1
67p = add bitcast f64 ulong 4607182418800017408 div mul r p f64 n
68n = sub n 1
69end
70if lt k -1020
71ret mul mul p av_pow2 long -1020 av_pow2 add k 1020
72end
73ret mul p av_pow2 k
74end
75fn av_pow base f64 e f64 to f64
76if or eq base bitcast f64 ulong 4607182418800017408 eq e bitcast f64 ulong 0
77ret bitcast f64 ulong 4607182418800017408
78end
79ret av_exp mul e av_log base
80end
81fn av_sinc a f64 dest edit ulong 2 to void
82fnn = mul a bitcast f64 ulong 4603909380684499075
83round = bitcast f64 ulong 13826050856027422720
84if ge fnn bitcast f64 ulong 0
85round = bitcast f64 ulong 4602678819172646912
86end
87n = long add fnn round
88nd = f64 n
89r = sub a mul nd bitcast f64 ulong 4609753056924401664
90r = sub r mul nd bitcast f64 ulong 4454258360616747008
91r = sub r mul nd bitcast f64 ulong 4297306550709518336
92r = sub r mul nd bitcast f64 ulong 4142048980368378305
93w = mul r r
94ps = bitcast f64 ulong 4607182418800017408
95k = long 15
96while ge k 3
97nd = f64 mul k sub k 1
98ps = mul w ps
99ps = div ps nd
100ps = sub bitcast f64 ulong 4607182418800017408 ps
101k = sub k 2
102end
103pc = bitcast f64 ulong 4607182418800017408
104k = long 16
105while ge k 2
106nd = f64 mul k sub k 1
107pc = mul w pc
108pc = div pc nd
109pc = sub bitcast f64 ulong 4607182418800017408 pc
110k = sub k 2
111end
112sr = mul r ps
113q = and ulong n 3
114if eq q 0
115put dest 0 bitcast ulong sr
116put dest 1 bitcast ulong pc
117else
118if eq q 1
119put dest 0 bitcast ulong pc
120put dest 1 bitcast ulong neg sr
121else
122if eq q 2
123put dest 0 bitcast ulong neg sr
124put dest 1 bitcast ulong neg pc
125else
126put dest 0 bitcast ulong neg pc
127put dest 1 bitcast ulong sr
128end
129end
130end
131end
132fn av_rrow d edit f32 * q view ulong 16 off ulong pos ulong to void
133sc = new ulong 2 0
134half = div get q 3 2
135for i ulong half
136idx = add off i
137if lt i div get q 2 2
138inv = f64 mul i 2
139inv = neg inv
140inv = div inv f64 get q 2
141inv = av_pow bitcast f64 get q 1 inv
142inv = mul f64 pos inv
143av_sinc inv sc
144put d idx f32 bitcast f64 get sc 1
145idx = add idx half
146put d idx f32 bitcast f64 get sc 0
147else
148put d idx f32 1.0
149idx = add idx half
150put d idx f32 0.0
151end
152end
153end
154fn vc_rope d edit f32 * q view ulong 16 to ulong
155for j ulong get q 7
156row = add get q 6 j
157off = mul row get q 3
158pos = add get q 0 row
159av_rrow d q off pos
160end
161ret 0
162end
163fn av_attnrope d edit f32 * q view ulong 16 to ulong
164z = new ulong 16 0
165for j ulong 16
166put z j get q j
167end
168for bb ulong add div get q 4 1000000 1
169put z 6 mul bb 1000000
170cnt = sub get q 4 get z 6
171if gt cnt 1000000
172cnt = ulong 1000000
173end
174put z 7 cnt
175rc = vc_rope d z
176if ne rc 0
177ret rc
178end
179end
180ret 0
181end