aien-train
AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.
Brought by drakestapleton
aien-train / evidence / ECOSYSTEM6 / ref32-review / mul-focused.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note Native IEEE binary32 multiplication represented as bits; no host floating runtime. |
| 3 | note Round-to-nearest ties-to-even, gradual underflow, signed zeros and infinities. |
| 4 | note Canonical positive quiet NaN for every NaN result. Inputs are low32 bits. |
| 5 | fn mround x ulong shift ulong to ulong |
| 6 | if eq shift 0 |
| 7 | ret x |
| 8 | end |
| 9 | if gt shift 63 |
| 10 | ret 0 |
| 11 | end |
| 12 | q = shr x shift |
| 13 | mask = sub shl 1 shift 1 |
| 14 | r = and x mask |
| 15 | half = shl 1 sub shift 1 |
| 16 | if or gt r half and eq r half ne and q 1 0 |
| 17 | q = add q 1 |
| 18 | end |
| 19 | ret q |
| 20 | end |
| 21 | fn mul32 aa ulong bb ulong to ulong |
| 22 | a = and aa 4294967295 |
| 23 | b = and bb 4294967295 |
| 24 | sign = and xor a b 2147483648 |
| 25 | ea = long and shr a 23 255 |
| 26 | eb = long and shr b 23 255 |
| 27 | ma = and a 8388607 |
| 28 | mb = and b 8388607 |
| 29 | if or and eq ea 255 ne ma 0 and eq eb 255 ne mb 0 |
| 30 | ret 2143289344 |
| 31 | end |
| 32 | if or eq ea 255 eq eb 255 |
| 33 | if or eq and a 2147483647 0 eq and b 2147483647 0 |
| 34 | ret 2143289344 |
| 35 | end |
| 36 | ret or sign 2139095040 |
| 37 | end |
| 38 | if or eq and a 2147483647 0 eq and b 2147483647 0 |
| 39 | ret sign |
| 40 | end |
| 41 | if eq ea 0 |
| 42 | ea = 1 |
| 43 | while lt ma 8388608 |
| 44 | ma = shl ma 1 |
| 45 | ea = sub ea 1 |
| 46 | end |
| 47 | else |
| 48 | ma = or ma 8388608 |
| 49 | end |
| 50 | if eq eb 0 |
| 51 | eb = 1 |
| 52 | while lt mb 8388608 |
| 53 | mb = shl mb 1 |
| 54 | eb = sub eb 1 |
| 55 | end |
| 56 | else |
| 57 | mb = or mb 8388608 |
| 58 | end |
| 59 | product = mul ma mb |
| 60 | e = sub add ea eb 127 |
| 61 | shift = long 23 |
| 62 | if ge product 140737488355328 |
| 63 | shift = 24 |
| 64 | e = add e 1 |
| 65 | end |
| 66 | if lt e 1 |
| 67 | shift = add shift sub 1 e |
| 68 | e = 0 |
| 69 | end |
| 70 | q = mround product ulong shift |
| 71 | if eq e 0 |
| 72 | if ge q 8388608 |
| 73 | e = 1 |
| 74 | end |
| 75 | else |
| 76 | if ge q 16777216 |
| 77 | q = shr q 1 |
| 78 | e = add e 1 |
| 79 | end |
| 80 | end |
| 81 | if ge e 255 |
| 82 | ret or sign 2139095040 |
| 83 | end |
| 84 | q = and q 8388607 |
| 85 | e = shl e 23 |
| 86 | q = or q ulong e |
| 87 | ret or sign q |
| 88 | end |
| 89 | note SPDX-License-Identifier: AGPL-3.0-only |
| 90 | global failed ulong 1 |
| 91 | global tested ulong 1 |
| 92 | global line uchar 48 |
| 93 | fn report ok bool to void |
| 94 | id = add get tested 0 1 |
| 95 | put tested 0 id |
| 96 | put line 0 48 |
| 97 | put line 1 uchar add 48 rem div id 100 10 |
| 98 | put line 2 uchar add 48 rem div id 10 10 |
| 99 | put line 3 uchar add 48 rem id 10 |
| 100 | put line 4 32 |
| 101 | put line 5 48 |
| 102 | if not ok |
| 103 | put line 5 49 |
| 104 | put failed 0 add get failed 0 1 |
| 105 | end |
| 106 | put line 6 10 |
| 107 | w = write 1 line 0 7 |
| 108 | if ne w 7 |
| 109 | exit 74 |
| 110 | end |
| 111 | end |
| 112 | note Binary32 multiply RNE. Expected bits independently checked with IEEE host oracle. |
| 113 | note All NaNs canonicalized to positive quiet NaN; gradual underflow; signed zero. |
| 114 | fn main to int |
| 115 | report eq mul32 1065353216 1073741824 1073741824 |
| 116 | report eq mul32 3212836864 1073741824 3221225472 |
| 117 | report eq mul32 0 3212836864 2147483648 |
| 118 | report eq mul32 2147483648 3212836864 0 |
| 119 | report eq mul32 2139095040 0 2143289344 |
| 120 | report eq mul32 2139095040 3212836864 4286578688 |
| 121 | report eq mul32 2143289345 1065353216 2143289344 |
| 122 | report eq mul32 1 1065353216 1 |
| 123 | report eq mul32 8388608 1056964608 4194304 |
| 124 | report eq mul32 8388607 1073741824 16777214 |
| 125 | report eq mul32 2139095039 1073741824 2139095040 |
| 126 | report eq mul32 1069547520 1069547520 1074790400 |
| 127 | report eq mul32 1065353217 1065353217 1065353218 |
| 128 | report eq mul32 1 1056964608 0 |
| 129 | report eq mul32 3 1056964608 2 |
| 130 | if ne get failed 0 0 |
| 131 | ret 1 |
| 132 | end |
| 133 | ret 0 |
| 134 | end |