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 / core / sovereign-core / om / qwen-native / blocks / math.om

99 lines · 1999 bytes Raw Blame History
1note Scalar operators from Core om/flint/ops.om, names scoped for composition.
2fn qb_exp x f64 to f64
3if lt x -708.0f64
4ret 0.0f64
5end
6if gt x 709.0f64
7ret div 1.0f64 0.0f64
8end
9kf = mul x 1.442695040888963407f64
10if lt kf 0.0f64
11kf = sub kf 0.5f64
12else
13kf = add kf 0.5f64
14end
15k = long kf
16fk = f64 k
17r = sub sub x mul fk 0.6931471803691238165f64 mul fk 1.908214929270587816e-10f64
18p = 1.0f64
19for j ulong 13
20p = add 1.0f64 mul div r f64 sub 13 j p
21end
22s = bitcast f64 shl ulong add k 1023 52
23ret mul p s
24end
25
26fn qb_sig x f64 to f64
27ret div 1.0f64 add 1.0f64 qb_exp neg x
28end
29
30note sin (c false) or cos (c true) of x in [0, 2^20): k = round(x 2 / pi), reduction by
31note pio2_hi (33 bits) and pio2_lo, Taylor through degree 21 on |r| <= pi / 4
32fn qb_sc x f64 c bool to f64
33k = ulong add mul x 0.6366197723675813431f64 0.5f64
34fk = f64 k
35r = sub sub x mul fk 1.570796326734125614f64 mul fk 6.077100506506192601e-11f64
36z = mul r r
37s = 1.0f64
38co = 1.0f64
39for j ulong 10
40m = f64 mul 2 sub 10 j
41s = sub 1.0f64 mul div z mul m add m 1.0f64 s
42co = sub 1.0f64 mul div z mul sub m 1.0f64 m co
43end
44s = mul r s
45q = k
46if c
47q = add q 1
48end
49q = and q 3
50if eq q 0
51ret s
52end
53if eq q 1
54ret co
55end
56if eq q 2
57ret neg s
58end
59ret neg co
60end
61
62note log1p(exp(x)) = max(x,0) + 2 atanh(t/(2+t)), t=exp(-abs(x)).
63note The series argument has magnitude at most one third.
64fn qb_softplus x f32 to f32
65if gt x 20.0f32
66ret x
67end
68t = qb_exp neg fabs f64 x
69u = div t add 2.0f64 t
70v = mul u u
71p = 0.0f64
72for j ulong 14
73p = add div 1.0f64 f64 sub 27 mul 2 j mul p v
74end
75ret f32 add f64 fmax x 0.0f32 mul mul 2.0f64 u p
76end
77
78global qb_next ulong 64
79global qb_model ulong 64
80
81fn qb_check model ulong layer ulong pos ulong full bool to void
82if and and ne model 27 ne model 35 ne model 38
83trap 5
84end
85if or or ge layer 64 and eq model 35 ge layer 40 and eq model 38 ge layer 48
86trap 5
87end
88if ne full eq rem layer 4 3
89trap 5
90end
91if ge pos 8192
92trap 5
93end
94if ne pos 0
95if or ne pos get qb_next layer ne model get qb_model layer
96trap 5
97end
98end
99end