interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / om / flog.om

398 lines · 14100 bytes Raw Blame History
1note ===== flog.om : IEEE f64 fused multiply-add in integer arithmetic, and natural log =====
2note Owner FtsEngine. Globals q*, functions flog*.
3note API
4note flog_init decode the glibc table once (fts_init calls it)
5note flog_fma a b c = RN(a*b+c) with one rounding, the AArch64 FMADD result, for finite
6note operands (any normal, subnormal or zero); an inf or NaN operand falls
7note back to (a*b)+c with two roundings
8note flog x = bit-exact replica of glibc 2.39 aarch64 log (libm.so.6 sha256
9note 5e039156...1b4a, log@@GLIBC_2.29 at 0x34cd0): same table, same
10note operation order, same fused operations (see evidence/fts/glibc-log.dis)
11note glibc's log is not correctly rounded (bound 0.519 ulp), so a correctly rounded log would
12note not bit-match it; this file replicates the glibc instruction sequence instead.
13note scratch for 128-bit values: qfh:qfl
14global qfh ulong 1
15global qfl ulong 1
16note decoded __log_data: 0 ln2hi, 1 ln2lo, 2..6 poly A0..A4, 7..17 poly1 B0..B10, 18+2i invc, 19+2i logc
17global qlgt ulong 274
18global qlgi ulong 1
19note generated by tests/fts/gen_flog_table.py from /usr/lib/aarch64-linux-gnu/libm.so.6 sha256 5e0391562de36de323fb9cb0f2c1b6bd6d034c840b1dac018499f5fe4ded1b4a
20text qlgx "4604418534313441280 4408728642709317424 13826050856027422721 4599676419420794971 13821547256398693776 4596373889406271761 13818544996498196639 13826050856027422720 4599676419421066615 13821547256400051659 4596373779694083340 13818544856649647527 4594314991578177072 13817043655233518949 4592670532434581194 13815242567667706111 4591280744628736133 13814037675563369933 4609210952320081567 13823742429115092992 4609174151535323423 13823640652485558272 4609137762941711776 13823539447239663616 4609101780076432509 13823438808184027136 4609066195948516561 13823338728338257920 4609031003647708346 13823239200618053632 4608996196778918435 13823140219078086656 4608961770493910745 13823041781908066304 4608927715981993156 13822943875669114880 4608894029497490452 13822846501147660288 4608860704429464652 13822749650639759360 4608827733622991951 13822653314525870080 4608795113711964382 13822557493987864576 4608762837695273592 13822462179321683968 4608730900845713031 13822367367239729152 4608699297346055123 13822273050981228544 4608668021725560723 13822179224546084864 4608637069496305027 13822085884644134912 4608606436072799711 13821993027486939136 4608576115407380183 13821900644610078720 4608546104468711488 13821808736507762688 4608516395921613768 13821717290277343232 4608486988337653130 13821626310950369280 4608457874380981300 13821524313851228160 4608429050692185355 13821344166540980224 4608400513723603028 13821164919435833344 4608372259037676634 13820986562460626944 4608344281559988475 13820809081066614784 4608316577295561905 13820632467097620480 4608289143243931279 13820456718410780672 4608261974399864041 13820281819674775552 4608235066966982955 13820107762892525568 4608208418496699064 13819934548492980224 4608182024348356610 13819762162364444672 4608155880746416668 13819590595620384768 4608129984310884066 13819419841628848128 4608104331790373952 13819249894299545600 4608078919386516260 13819080743594389504 4608053745083813193 13818912391057334272 4608028803162061200 13818744813135568896 4608004092140582977 13818578014346424320 4607979607715064273 13818411979930480640 4607955348240203050 13818246712844746752 4607931308525231019 13818082191617490944 4607907488200963376 13817918427446706176 4607883882914095605 13817755403999789056 4607860489364257727 13817593111812612096 4607837304735659453 13817431544482799616 4607814327318677563 13817270703104663552 4607791553269285046 13817110573604577280 4607768980718633266 13816858654215471104 4607746606204386459 13816541216839319552 4607724427722704907 13816225174501539840 4607702441940444521 13815910503988895744 4607680648182201980 13815597219701547008 4607659041870259945 13815285279565053952 4607637621113100982 13814974679547559936 4607616384371159229 13814665420431163392 4607595329002388662 13814357486602813440 4607574451424080346 13814050848089014272 4607553751206027229 13813745520761495552 4607533225893614156 13813441490475196416 4607512871202519514 13813138715335548928 4607492687924081969 13812837228489089024 4607472672061011119 13812533925724160000 4607452821607836595 13811935932589752320 4607433134784028622 13811340407893983232 4607413609963496737 13810747343165898752 4607394244387313690 13810156694749233152 4607375037409146577 13809568482684469248 4607355985284043577 13808982631454703616 4607337087808759775 13808399173480169472 4607318342423685371 13807599678720278528 4607299746773962521 13806442102614294528 4607281300320876844 13805289202348294144 4607263000134495376 13804140868705845248 4607244845192359337 13802461362887655424 4607226833383535273 13800182875165425664 4607208962367583048 13796797265813962752 4607191232174928277 13790026497742077952 4607147371718441498 4571135975984463872 4607078088715659892 4577869718869573632 4607009867155216686 4581232448741998592 4606942683337764301 4583432697401638912 4606876514109166567 4585140340856520704 4606811335652258219 4586223922483150848 4606747126592769179 4587299414251962368 4606683865489775029 4588366935595909120 4606621530588224086 4589297319838007296 4606560103363492760 4589823285785337856 4606499562142339331 4590345449255731200 4606439889401765070 4590863851743330304 4606381066660273821 4591378546364645376 4606323074200710349 4591889600642572288 4606265896982771970 4592397044266393600 4606209516050516077 4592900944725221376 4606153914748590084 4593401352421269504 4606099078592044940 4593784960540819456 4606044990914488127 4594031733641490432 4605991636799559361 4594276827212165120 4605939000208149032 4594520269598838784 4605887068574268972 4594762074469634048 4605835828034898059 4595002262718939136 4605785262199222346 4595240868508725248 4605735360665939838 4595477899084623872 4605686108857056674 4595713382617092096 4605637494364657831 4595947338221678592 4605589505339650955 4596179783459962880 4605542130267147047 4596410735360753664 4605495355974611665 4596640220181168128 4605449172481162267 4596868249655824384 4605403567997038992 4597094845431726080 4605358530648067631 4597320030710423552 4605314051900459893 4597543812985176064 4605270121620391992 4597766208615342080 4605226727711920870 4597987244934402048 4605183861247170461 4598191076868601856 4605141513151447164 4598300254877765632 4605099672569037582 4598408778231476224 4605058332693305339 4598516648831078400 4605017483302377992 4598623877854349312 4604977115906260565 4598730472393834496 4604937222866862688 4598836437669079040 4604897794226955049 4598941785460615168 4604858822028998061 4599046522653597696 4604820300194325954 4599150651412060160 4604782219042948695 4599254183977103360 4604744573281243916 4599357121047289856"
21fn flog_init to void
22if eq get qlgi 0 1
23ret
24end
25k = ulong 0
26v = ulong 0
27dg = false
28for i ulong len qlgx
29c = ulong get qlgx i
30if and ge c 48 le c 57
31v = add mul v 10 sub c 48
32dg = true
33else
34if dg
35put qlgt k v
36k = add k 1
37end
38v = ulong 0
39dg = false
40end
41end
42if dg
43put qlgt k v
44k = add k 1
45end
46if ne k 274
47trap 9
48end
49put qlgi 0 1
50end
51fn flog_t i ulong to f64
52ret bitcast f64 get qlgt i
53end
54note bit length of a 64-bit value (0 for 0)
55fn flog_bl x ulong to ulong
56n = ulong 0
57y = x
58if ge y 4294967296
59y = shr y 32
60n = add n 32
61end
62if ge y 65536
63y = shr y 16
64n = add n 16
65end
66if ge y 256
67y = shr y 8
68n = add n 8
69end
70if ge y 16
71y = shr y 4
72n = add n 4
73end
74if ge y 4
75y = shr y 2
76n = add n 2
77end
78if ge y 2
79y = shr y 1
80n = add n 1
81end
82ret add n y
83end
84fn flog_bl2 h ulong l ulong to ulong
85if ne h 0
86ret add 64 flog_bl h
87end
88ret flog_bl l
89end
90note 64x64 -> 128 product into qfh:qfl
91fn flog_mul a ulong b ulong to void
92a0 = and a 4294967295
93a1 = shr a 32
94b0 = and b 4294967295
95b1 = shr b 32
96flog_mul2 mul a0 b0 mul a0 b1 mul a1 b0 mul a1 b1
97end
98fn flog_mul2 p00 ulong p01 ulong p10 ulong p11 ulong to void
99md = add shr p00 32 and p01 4294967295
100md = add md and p10 4294967295
101hi = add p11 shr p01 32
102hi = add hi shr p10 32
103hi = add hi shr md 32
104put qfh 0 hi
105put qfl 0 or shl md 32 and p00 4294967295
106end
107note (h:l) << s, s 0..127, into qfh:qfl (bits shifted past 127 are lost; callers never do that)
108fn flog_shl h ulong l ulong s ulong to void
109if eq s 0
110put qfh 0 h
111put qfl 0 l
112ret
113end
114if ge s 64
115put qfh 0 shl l sub s 64
116put qfl 0 0
117ret
118end
119put qfh 0 or shl h s shr l sub 64 s
120put qfl 0 shl l s
121end
122note (h:l) >> s with every lost bit ORed into bit 0 (sticky jam), into qfh:qfl
123fn flog_shrs h ulong l ulong s ulong to void
124if eq s 0
125put qfh 0 h
126put qfl 0 l
127ret
128end
129if ge s 128
130put qfh 0 0
131if ne or h l 0
132put qfl 0 1
133else
134put qfl 0 0
135end
136ret
137end
138lost = false
139nl = ulong 0
140nh = ulong 0
141if ge s 64
142t = sub s 64
143lost = ne l 0
144if gt t 0
145if ne and h sub shl ulong 1 t 1 0
146lost = true
147end
148nl = shr h t
149else
150nl = h
151end
152else
153lost = ne and l sub shl ulong 1 s 1 0
154nl = or shr l s shl h sub 64 s
155nh = shr h s
156end
157if lost
158nl = or nl 1
159end
160put qfh 0 nh
161put qfl 0 nl
162end
163note bit k of (h:l)
164fn flog_bit h ulong l ulong k ulong to ulong
165if ge k 64
166ret and shr h sub k 64 1
167end
168ret and shr l k 1
169end
170note true when some bit below k of (h:l) is set
171fn flog_low h ulong l ulong k ulong to bool
172if ge k 64
173if ne l 0
174ret true
175end
176t = sub k 64
177if eq t 0
178ret false
179end
180ret ne and h sub shl ulong 1 t 1 0
181end
182if eq k 0
183ret false
184end
185ret ne and l sub shl ulong 1 k 1 0
186end
187note round (h:l) * 2^ex (h:l nonzero, below 2^127) to binary64 with sign sg, nearest-even
188fn flog_pack sg ulong ex long h ulong l ulong to f64
189lb = long flog_bl2 h l
190sh = sub lb 53
191er = add ex sh
192if lt er -1074
193sh = add sh sub -1074 er
194er = long -1074
195end
196m = ulong 0
197if le sh 0
198m = shl l ulong neg sh
199else
200if ge sh 128
201m = ulong 0
202else
203s = ulong sh
204if ge s 64
205m = shr h sub s 64
206else
207m = or shr l s shl h sub 64 s
208end
209g = flog_bit h l sub s 1
210if eq g 1
211if or flog_low h l sub s 1 eq and m 1 1
212m = add m 1
213end
214end
215end
216end
217if eq m 9007199254740992
218m = ulong 4503599627370496
219er = add er 1
220end
221if gt er 971
222ret bitcast f64 or shl sg 63 9218868437227405312
223end
224if ge m 4503599627370496
225ret bitcast f64 or or shl sg 63 shl ulong add er 1075 52 sub m 4503599627370496
226end
227ret bitcast f64 or shl sg 63 m
228end
229note significand and exponent of a finite binary64 bit pattern: value = m * 2^e
230fn flog_m u ulong to ulong
231f = and u 4503599627370495
232if eq and shr u 52 2047 0
233ret f
234end
235ret or f 4503599627370496
236end
237fn flog_e u ulong to long
238b = and shr u 52 2047
239if eq b 0
240ret long -1074
241end
242ret sub long b 1075
243end
244fn flog_nf u ulong to bool
245ret eq and shr u 52 2047 2047
246end
247note fused a*b+c, one rounding to nearest-even
248fn flog_fma a f64 b f64 c f64 to f64
249ua = bitcast ulong a
250ub = bitcast ulong b
251uc = bitcast ulong c
252if or or flog_nf ua flog_nf ub flog_nf uc
253ret add mul a b c
254end
255ma = flog_m ua
256mb = flog_m ub
257if or eq ma 0 eq mb 0
258ret add mul a b c
259end
260if eq flog_m uc 0
261ret mul a b
262end
263ret flog_fmb ua ub uc
264end
265fn flog_fmb ua ulong ub ulong uc ulong to f64
266flog_mul flog_m ua flog_m ub
267ep = add flog_e ua flog_e ub
268ph = get qfh 0
269pl = get qfl 0
270s = sub 126 flog_bl2 ph pl
271flog_shl ph pl s
272ret flog_fma2 xor shr ua 63 shr ub 63 sub ep long s shr uc 63 flog_m uc flog_e uc
273end
274note second half: product P = qfh:qfl * 2^ep (top bit 125), addend C = mc * 2^ec
275fn flog_fma2 sp ulong ep long sc ulong mc ulong ec long to f64
276ph = get qfh 0
277pl = get qfl 0
278s = sub 126 flog_bl mc
279flog_shl 0 mc s
280ch = get qfh 0
281cl = get qfl 0
282ec2 = sub ec long s
283if ge ep ec2
284flog_shrs ch cl ulong sub ep ec2
285ret flog_addx sp ep ph pl sc
286end
287flog_shrs ph pl ulong sub ec2 ep
288ret flog_addx sc ec2 ch cl sp
289end
290note X = (xh:xl) * 2^ex with sign sx, Y = qfh:qfl (already aligned) with sign sy
291fn flog_addx sx ulong ex long xh ulong xl ulong sy ulong to f64
292yh = get qfh 0
293yl = get qfl 0
294if eq sx sy
295lo = add xl yl
296cy = ulong 0
297if lt lo xl
298cy = ulong 1
299end
300ret flog_pack sx ex add add xh yh cy lo
301end
302if or gt xh yh and eq xh yh ge xl yl
303if and eq xh yh eq xl yl
304ret 0.0
305end
306bw = ulong 0
307if lt xl yl
308bw = ulong 1
309end
310ret flog_pack sx ex sub sub xh yh bw sub xl yl
311end
312bw = ulong 0
313if lt yl xl
314bw = ulong 1
315end
316ret flog_pack sy ex sub sub yh xh bw sub yl xl
317end
318note glibc 2.39 log, main path; ix already adjusted for subnormals
319fn flog_main ix ulong to f64
320tmp = sub ix 4604367669032910848
321i = and shr tmp 45 127
322k = long shr tmp 52
323if ge tmp 9223372036854775808
324k = sub k 4096
325end
326ret flog_main2 f64 k bitcast f64 sub ix and tmp 18442240474082181120 flog_t add 18 mul i 2 flog_t add 19 mul i 2
327end
328fn flog_main2 kd f64 z f64 iv f64 lc f64 to f64
329w = flog_fma flog_t 0 kd lc
330r = flog_fma iv z -1.0
331hi = add r w
332t5 = flog_fma flog_t 6 r flog_t 5
333t3 = flog_fma flog_t 4 r flog_t 3
334r2 = mul r r
335d = sub w hi
336r3 = mul r r2
337d = add d r
338p = flog_fma t5 r2 t3
339d = flog_fma flog_t 1 kd d
340d = flog_fma flog_t 2 r2 d
341d = flog_fma r3 p d
342ret add d hi
343end
344note glibc 2.39 log, |x - 1| small path
345fn flog_near x f64 to f64
346r = sub x 1.0
347c27 = bitcast f64 ulong 4728779608739020800
348d1 = flog_fma r c27 r
349r2 = mul r r
350d4 = flog_fma flog_t 15 r flog_t 14
351rh = flog_fma neg r c27 d1
352r3 = mul r r2
353d4 = flog_fma flog_t 16 r2 d4
354d3 = flog_fma flog_t 12 r flog_t 11
355d4 = flog_fma flog_t 17 r3 d4
356rr = mul rh rh
357d3 = flog_fma flog_t 13 r2 d3
358hi = flog_fma rr flog_t 7 r
359d2 = flog_fma flog_t 9 r flog_t 8
360d3 = flog_fma d4 r3 d3
361d2 = flog_fma flog_t 10 r2 d2
362ret flog_near2 r rh hi rr d2 d3
363end
364fn flog_near2 r f64 rh f64 hi f64 rr f64 d2 f64 d3 f64 to f64
365r3 = mul r mul r r
366rl = sub r rh
367d1 = add r rh
368d6 = mul flog_t 7 rl
369d0 = sub r hi
370d0 = flog_fma rr flog_t 7 d0
371d2 = flog_fma d3 r3 d2
372d0 = flog_fma d6 d1 d0
373d0 = flog_fma d2 r3 d0
374ret add hi d0
375end
376fn flog x f64 to f64
377ix = bitcast ulong x
378if lt sub ix 4606619468846596096 854320534781952
379if eq ix 4607182418800017408
380ret 0.0
381end
382ret flog_near x
383end
384top = shr ix 48
385if gt and sub top 16 4294967295 32735
386if eq shl ix 1 0
387ret div -1.0f64 0.0f64
388end
389if eq ix 9218868437227405312
390ret x
391end
392if or ne and top 32768 0 eq and top 32752 32752
393ret div sub x x sub x x
394end
395ix = sub bitcast ulong mul x bitcast f64 ulong 4841369599423283200 234187180623265792
396end
397ret flog_main ix
398end