cortex-rs
Pure compiled native Rust memory engine with vector search and SQLite WAL
Brought by drakestapleton
| 1 | note ===== flog.om : IEEE f64 fused multiply-add in integer arithmetic, and natural log ===== |
| 2 | note Owner FtsEngine. Globals q*, functions flog*. |
| 3 | note API |
| 4 | note flog_init decode the glibc table once (fts_init calls it) |
| 5 | note flog_fma a b c = RN(a*b+c) with one rounding, the AArch64 FMADD result, for finite |
| 6 | note operands (any normal, subnormal or zero); an inf or NaN operand falls |
| 7 | note back to (a*b)+c with two roundings |
| 8 | note flog x = bit-exact replica of glibc 2.39 aarch64 log (libm.so.6 sha256 |
| 9 | note 5e039156...1b4a, log@@GLIBC_2.29 at 0x34cd0): same table, same |
| 10 | note operation order, same fused operations (see evidence/fts/glibc-log.dis) |
| 11 | note glibc's log is not correctly rounded (bound 0.519 ulp), so a correctly rounded log would |
| 12 | note not bit-match it; this file replicates the glibc instruction sequence instead. |
| 13 | note scratch for 128-bit values: qfh:qfl |
| 14 | global qfh ulong 1 |
| 15 | global qfl ulong 1 |
| 16 | note decoded __log_data: 0 ln2hi, 1 ln2lo, 2..6 poly A0..A4, 7..17 poly1 B0..B10, 18+2i invc, 19+2i logc |
| 17 | global qlgt ulong 274 |
| 18 | global qlgi ulong 1 |
| 19 | note generated by tests/fts/gen_flog_table.py from /usr/lib/aarch64-linux-gnu/libm.so.6 sha256 5e0391562de36de323fb9cb0f2c1b6bd6d034c840b1dac018499f5fe4ded1b4a |
| 20 | text 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" |
| 21 | fn flog_init to void |
| 22 | if eq get qlgi 0 1 |
| 23 | ret |
| 24 | end |
| 25 | k = ulong 0 |
| 26 | v = ulong 0 |
| 27 | dg = false |
| 28 | for i ulong len qlgx |
| 29 | c = ulong get qlgx i |
| 30 | if and ge c 48 le c 57 |
| 31 | v = add mul v 10 sub c 48 |
| 32 | dg = true |
| 33 | else |
| 34 | if dg |
| 35 | put qlgt k v |
| 36 | k = add k 1 |
| 37 | end |
| 38 | v = ulong 0 |
| 39 | dg = false |
| 40 | end |
| 41 | end |
| 42 | if dg |
| 43 | put qlgt k v |
| 44 | k = add k 1 |
| 45 | end |
| 46 | if ne k 274 |
| 47 | trap 9 |
| 48 | end |
| 49 | put qlgi 0 1 |
| 50 | end |
| 51 | fn flog_t i ulong to f64 |
| 52 | ret bitcast f64 get qlgt i |
| 53 | end |
| 54 | note bit length of a 64-bit value (0 for 0) |
| 55 | fn flog_bl x ulong to ulong |
| 56 | n = ulong 0 |
| 57 | y = x |
| 58 | if ge y 4294967296 |
| 59 | y = shr y 32 |
| 60 | n = add n 32 |
| 61 | end |
| 62 | if ge y 65536 |
| 63 | y = shr y 16 |
| 64 | n = add n 16 |
| 65 | end |
| 66 | if ge y 256 |
| 67 | y = shr y 8 |
| 68 | n = add n 8 |
| 69 | end |
| 70 | if ge y 16 |
| 71 | y = shr y 4 |
| 72 | n = add n 4 |
| 73 | end |
| 74 | if ge y 4 |
| 75 | y = shr y 2 |
| 76 | n = add n 2 |
| 77 | end |
| 78 | if ge y 2 |
| 79 | y = shr y 1 |
| 80 | n = add n 1 |
| 81 | end |
| 82 | ret add n y |
| 83 | end |
| 84 | fn flog_bl2 h ulong l ulong to ulong |
| 85 | if ne h 0 |
| 86 | ret add 64 flog_bl h |
| 87 | end |
| 88 | ret flog_bl l |
| 89 | end |
| 90 | note 64x64 -> 128 product into qfh:qfl |
| 91 | fn flog_mul a ulong b ulong to void |
| 92 | a0 = and a 4294967295 |
| 93 | a1 = shr a 32 |
| 94 | b0 = and b 4294967295 |
| 95 | b1 = shr b 32 |
| 96 | flog_mul2 mul a0 b0 mul a0 b1 mul a1 b0 mul a1 b1 |
| 97 | end |
| 98 | fn flog_mul2 p00 ulong p01 ulong p10 ulong p11 ulong to void |
| 99 | md = add shr p00 32 and p01 4294967295 |
| 100 | md = add md and p10 4294967295 |
| 101 | hi = add p11 shr p01 32 |
| 102 | hi = add hi shr p10 32 |
| 103 | hi = add hi shr md 32 |
| 104 | put qfh 0 hi |
| 105 | put qfl 0 or shl md 32 and p00 4294967295 |
| 106 | end |
| 107 | note (h:l) << s, s 0..127, into qfh:qfl (bits shifted past 127 are lost; callers never do that) |
| 108 | fn flog_shl h ulong l ulong s ulong to void |
| 109 | if eq s 0 |
| 110 | put qfh 0 h |
| 111 | put qfl 0 l |
| 112 | ret |
| 113 | end |
| 114 | if ge s 64 |
| 115 | put qfh 0 shl l sub s 64 |
| 116 | put qfl 0 0 |
| 117 | ret |
| 118 | end |
| 119 | put qfh 0 or shl h s shr l sub 64 s |
| 120 | put qfl 0 shl l s |
| 121 | end |
| 122 | note (h:l) >> s with every lost bit ORed into bit 0 (sticky jam), into qfh:qfl |
| 123 | fn flog_shrs h ulong l ulong s ulong to void |
| 124 | if eq s 0 |
| 125 | put qfh 0 h |
| 126 | put qfl 0 l |
| 127 | ret |
| 128 | end |
| 129 | if ge s 128 |
| 130 | put qfh 0 0 |
| 131 | if ne or h l 0 |
| 132 | put qfl 0 1 |
| 133 | else |
| 134 | put qfl 0 0 |
| 135 | end |
| 136 | ret |
| 137 | end |
| 138 | lost = false |
| 139 | nl = ulong 0 |
| 140 | nh = ulong 0 |
| 141 | if ge s 64 |
| 142 | t = sub s 64 |
| 143 | lost = ne l 0 |
| 144 | if gt t 0 |
| 145 | if ne and h sub shl ulong 1 t 1 0 |
| 146 | lost = true |
| 147 | end |
| 148 | nl = shr h t |
| 149 | else |
| 150 | nl = h |
| 151 | end |
| 152 | else |
| 153 | lost = ne and l sub shl ulong 1 s 1 0 |
| 154 | nl = or shr l s shl h sub 64 s |
| 155 | nh = shr h s |
| 156 | end |
| 157 | if lost |
| 158 | nl = or nl 1 |
| 159 | end |
| 160 | put qfh 0 nh |
| 161 | put qfl 0 nl |
| 162 | end |
| 163 | note bit k of (h:l) |
| 164 | fn flog_bit h ulong l ulong k ulong to ulong |
| 165 | if ge k 64 |
| 166 | ret and shr h sub k 64 1 |
| 167 | end |
| 168 | ret and shr l k 1 |
| 169 | end |
| 170 | note true when some bit below k of (h:l) is set |
| 171 | fn flog_low h ulong l ulong k ulong to bool |
| 172 | if ge k 64 |
| 173 | if ne l 0 |
| 174 | ret true |
| 175 | end |
| 176 | t = sub k 64 |
| 177 | if eq t 0 |
| 178 | ret false |
| 179 | end |
| 180 | ret ne and h sub shl ulong 1 t 1 0 |
| 181 | end |
| 182 | if eq k 0 |
| 183 | ret false |
| 184 | end |
| 185 | ret ne and l sub shl ulong 1 k 1 0 |
| 186 | end |
| 187 | note round (h:l) * 2^ex (h:l nonzero, below 2^127) to binary64 with sign sg, nearest-even |
| 188 | fn flog_pack sg ulong ex long h ulong l ulong to f64 |
| 189 | lb = long flog_bl2 h l |
| 190 | sh = sub lb 53 |
| 191 | er = add ex sh |
| 192 | if lt er -1074 |
| 193 | sh = add sh sub -1074 er |
| 194 | er = long -1074 |
| 195 | end |
| 196 | m = ulong 0 |
| 197 | if le sh 0 |
| 198 | m = shl l ulong neg sh |
| 199 | else |
| 200 | if ge sh 128 |
| 201 | m = ulong 0 |
| 202 | else |
| 203 | s = ulong sh |
| 204 | if ge s 64 |
| 205 | m = shr h sub s 64 |
| 206 | else |
| 207 | m = or shr l s shl h sub 64 s |
| 208 | end |
| 209 | g = flog_bit h l sub s 1 |
| 210 | if eq g 1 |
| 211 | if or flog_low h l sub s 1 eq and m 1 1 |
| 212 | m = add m 1 |
| 213 | end |
| 214 | end |
| 215 | end |
| 216 | end |
| 217 | if eq m 9007199254740992 |
| 218 | m = ulong 4503599627370496 |
| 219 | er = add er 1 |
| 220 | end |
| 221 | if gt er 971 |
| 222 | ret bitcast f64 or shl sg 63 9218868437227405312 |
| 223 | end |
| 224 | if ge m 4503599627370496 |
| 225 | ret bitcast f64 or or shl sg 63 shl ulong add er 1075 52 sub m 4503599627370496 |
| 226 | end |
| 227 | ret bitcast f64 or shl sg 63 m |
| 228 | end |
| 229 | note significand and exponent of a finite binary64 bit pattern: value = m * 2^e |
| 230 | fn flog_m u ulong to ulong |
| 231 | f = and u 4503599627370495 |
| 232 | if eq and shr u 52 2047 0 |
| 233 | ret f |
| 234 | end |
| 235 | ret or f 4503599627370496 |
| 236 | end |
| 237 | fn flog_e u ulong to long |
| 238 | b = and shr u 52 2047 |
| 239 | if eq b 0 |
| 240 | ret long -1074 |
| 241 | end |
| 242 | ret sub long b 1075 |
| 243 | end |
| 244 | fn flog_nf u ulong to bool |
| 245 | ret eq and shr u 52 2047 2047 |
| 246 | end |
| 247 | note fused a*b+c, one rounding to nearest-even |
| 248 | fn flog_fma a f64 b f64 c f64 to f64 |
| 249 | ua = bitcast ulong a |
| 250 | ub = bitcast ulong b |
| 251 | uc = bitcast ulong c |
| 252 | if or or flog_nf ua flog_nf ub flog_nf uc |
| 253 | ret add mul a b c |
| 254 | end |
| 255 | ma = flog_m ua |
| 256 | mb = flog_m ub |
| 257 | if or eq ma 0 eq mb 0 |
| 258 | ret add mul a b c |
| 259 | end |
| 260 | if eq flog_m uc 0 |
| 261 | ret mul a b |
| 262 | end |
| 263 | ret flog_fmb ua ub uc |
| 264 | end |
| 265 | fn flog_fmb ua ulong ub ulong uc ulong to f64 |
| 266 | flog_mul flog_m ua flog_m ub |
| 267 | ep = add flog_e ua flog_e ub |
| 268 | ph = get qfh 0 |
| 269 | pl = get qfl 0 |
| 270 | s = sub 126 flog_bl2 ph pl |
| 271 | flog_shl ph pl s |
| 272 | ret flog_fma2 xor shr ua 63 shr ub 63 sub ep long s shr uc 63 flog_m uc flog_e uc |
| 273 | end |
| 274 | note second half: product P = qfh:qfl * 2^ep (top bit 125), addend C = mc * 2^ec |
| 275 | fn flog_fma2 sp ulong ep long sc ulong mc ulong ec long to f64 |
| 276 | ph = get qfh 0 |
| 277 | pl = get qfl 0 |
| 278 | s = sub 126 flog_bl mc |
| 279 | flog_shl 0 mc s |
| 280 | ch = get qfh 0 |
| 281 | cl = get qfl 0 |
| 282 | ec2 = sub ec long s |
| 283 | if ge ep ec2 |
| 284 | flog_shrs ch cl ulong sub ep ec2 |
| 285 | ret flog_addx sp ep ph pl sc |
| 286 | end |
| 287 | flog_shrs ph pl ulong sub ec2 ep |
| 288 | ret flog_addx sc ec2 ch cl sp |
| 289 | end |
| 290 | note X = (xh:xl) * 2^ex with sign sx, Y = qfh:qfl (already aligned) with sign sy |
| 291 | fn flog_addx sx ulong ex long xh ulong xl ulong sy ulong to f64 |
| 292 | yh = get qfh 0 |
| 293 | yl = get qfl 0 |
| 294 | if eq sx sy |
| 295 | lo = add xl yl |
| 296 | cy = ulong 0 |
| 297 | if lt lo xl |
| 298 | cy = ulong 1 |
| 299 | end |
| 300 | ret flog_pack sx ex add add xh yh cy lo |
| 301 | end |
| 302 | if or gt xh yh and eq xh yh ge xl yl |
| 303 | if and eq xh yh eq xl yl |
| 304 | ret 0.0 |
| 305 | end |
| 306 | bw = ulong 0 |
| 307 | if lt xl yl |
| 308 | bw = ulong 1 |
| 309 | end |
| 310 | ret flog_pack sx ex sub sub xh yh bw sub xl yl |
| 311 | end |
| 312 | bw = ulong 0 |
| 313 | if lt yl xl |
| 314 | bw = ulong 1 |
| 315 | end |
| 316 | ret flog_pack sy ex sub sub yh xh bw sub yl xl |
| 317 | end |
| 318 | note glibc 2.39 log, main path; ix already adjusted for subnormals |
| 319 | fn flog_main ix ulong to f64 |
| 320 | tmp = sub ix 4604367669032910848 |
| 321 | i = and shr tmp 45 127 |
| 322 | k = long shr tmp 52 |
| 323 | if ge tmp 9223372036854775808 |
| 324 | k = sub k 4096 |
| 325 | end |
| 326 | ret 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 |
| 327 | end |
| 328 | fn flog_main2 kd f64 z f64 iv f64 lc f64 to f64 |
| 329 | w = flog_fma flog_t 0 kd lc |
| 330 | r = flog_fma iv z -1.0 |
| 331 | hi = add r w |
| 332 | t5 = flog_fma flog_t 6 r flog_t 5 |
| 333 | t3 = flog_fma flog_t 4 r flog_t 3 |
| 334 | r2 = mul r r |
| 335 | d = sub w hi |
| 336 | r3 = mul r r2 |
| 337 | d = add d r |
| 338 | p = flog_fma t5 r2 t3 |
| 339 | d = flog_fma flog_t 1 kd d |
| 340 | d = flog_fma flog_t 2 r2 d |
| 341 | d = flog_fma r3 p d |
| 342 | ret add d hi |
| 343 | end |
| 344 | note glibc 2.39 log, |x - 1| small path |
| 345 | fn flog_near x f64 to f64 |
| 346 | r = sub x 1.0 |
| 347 | c27 = bitcast f64 ulong 4728779608739020800 |
| 348 | d1 = flog_fma r c27 r |
| 349 | r2 = mul r r |
| 350 | d4 = flog_fma flog_t 15 r flog_t 14 |
| 351 | rh = flog_fma neg r c27 d1 |
| 352 | r3 = mul r r2 |
| 353 | d4 = flog_fma flog_t 16 r2 d4 |
| 354 | d3 = flog_fma flog_t 12 r flog_t 11 |
| 355 | d4 = flog_fma flog_t 17 r3 d4 |
| 356 | rr = mul rh rh |
| 357 | d3 = flog_fma flog_t 13 r2 d3 |
| 358 | hi = flog_fma rr flog_t 7 r |
| 359 | d2 = flog_fma flog_t 9 r flog_t 8 |
| 360 | d3 = flog_fma d4 r3 d3 |
| 361 | d2 = flog_fma flog_t 10 r2 d2 |
| 362 | ret flog_near2 r rh hi rr d2 d3 |
| 363 | end |
| 364 | fn flog_near2 r f64 rh f64 hi f64 rr f64 d2 f64 d3 f64 to f64 |
| 365 | r3 = mul r mul r r |
| 366 | rl = sub r rh |
| 367 | d1 = add r rh |
| 368 | d6 = mul flog_t 7 rl |
| 369 | d0 = sub r hi |
| 370 | d0 = flog_fma rr flog_t 7 d0 |
| 371 | d2 = flog_fma d3 r3 d2 |
| 372 | d0 = flog_fma d6 d1 d0 |
| 373 | d0 = flog_fma d2 r3 d0 |
| 374 | ret add hi d0 |
| 375 | end |
| 376 | fn flog x f64 to f64 |
| 377 | ix = bitcast ulong x |
| 378 | if lt sub ix 4606619468846596096 854320534781952 |
| 379 | if eq ix 4607182418800017408 |
| 380 | ret 0.0 |
| 381 | end |
| 382 | ret flog_near x |
| 383 | end |
| 384 | top = shr ix 48 |
| 385 | if gt and sub top 16 4294967295 32735 |
| 386 | if eq shl ix 1 0 |
| 387 | ret div -1.0f64 0.0f64 |
| 388 | end |
| 389 | if eq ix 9218868437227405312 |
| 390 | ret x |
| 391 | end |
| 392 | if or ne and top 32768 0 eq and top 32752 32752 |
| 393 | ret div sub x x sub x x |
| 394 | end |
| 395 | ix = sub bitcast ulong mul x bitcast f64 ulong 4841369599423283200 234187180623265792 |
| 396 | end |
| 397 | ret flog_main ix |
| 398 | end |