This commit is contained in:
~d6 2022-01-18 00:10:12 -05:00
parent 9ab32991ed
commit 128093cf09
1 changed files with 47 additions and 16 deletions

View File

@ -37,6 +37,16 @@
( -inf 1 11111111 0000000 negative infinity ) ( -inf 1 11111111 0000000 negative infinity )
( nan * 11111111 ******* lots of nans; * is wild ) ( nan * 11111111 ******* lots of nans; * is wild )
( ) ( )
( Some hex constants: )
( 0 #0000 )
( -0 #8000 )
( 1 #3f80 )
( -1 #bf80 )
( 2 #4000 )
( +inf #7f80 )
( -inf #ff80 )
( nan #ffff (among others) )
( )
( This code doesn't distinguish between quiet and signaling NaNs. ) ( This code doesn't distinguish between quiet and signaling NaNs. )
( ) ( )
( Bfloat16 values are emitted in a hexadecimal format: ) ( Bfloat16 values are emitted in a hexadecimal format: )
@ -172,6 +182,19 @@
@non-zero ( x* -> bool^ ) @non-zero ( x* -> bool^ )
#7fff AND2 #0000 NEQ2 JMP2r #7fff AND2 #0000 NEQ2 JMP2r
@is-nan ( x* -> bool^ )
#7fff AND2 #7f80 GTH2 JMP2r
@non-nan ( x* -> bool^ )
#7fff AND2 #7f81 LTH2 JMP2r
@is-inf ( x* -> bool^ )
#7fff AND2 #7f80 EQU2 JMP2r
( not nan, not +/-inf )
@is-finite ( x* -> bool^ )
#7fff AND2 #7f80 LTH2 JMP2r
( Shift mantissa m right by n bits, with rounding ) ( Shift mantissa m right by n bits, with rounding )
( ) ( )
( We round differently depending on the value to be lost: ) ( We round differently depending on the value to be lost: )
@ -207,22 +230,7 @@
&rnd-up ( [mta n] ) &rnd-up ( [mta n] )
STH2r SFT INC JMP2r STH2r SFT INC JMP2r
@is-nan ( x* -> bool^ ) ( lift an integer byte into a bfloat16 value )
SWP ( xlo xhi )
#7f ANDk ( xlo xhi 7f xhi&7f )
EQU ( xlo xhi xhi&7f=7f )
NIP SWP #00 NEQ ( xhi&7f=7f xlo!=0 )
AND JMP2r
@non-nan ( x* -> bool^ )
#00 EQU STH ( xhi [xlo=0] )
#7f ANDk NEQ ( xhi 7f!=xhi&7f [xlo=0] )
NIP STHr ORA ( 7f!=xhi&7f|xlo=0 )
JMP2r
@add-bf16 ( x* y* -> z* )
DUP2 ;non-nan
@byte-to-bf16 ( n^ -> x* ) @byte-to-bf16 ( n^ -> x* )
#86 SWP ( exp n ) #86 SWP ( exp n )
&loop &loop
@ -298,4 +306,27 @@
( 5. inf + x = inf ) ( 5. inf + x = inf )
( 6. -inf + x = -inf ) ( 6. -inf + x = -inf )
@add-bf16 ( x* y* -> z* ) @add-bf16 ( x* y* -> z* )
DUP2 ;is-nan JMP2 STH SWP2
DUP2 ;is-nan JMP2 STH SWP2
STH2r ORA ,&nan JCN ( is lhs or rhs nan? )
DUP2 ;is-inf ,&rhs-inf JCN ( is rhs inf? )
SWP2 ;is-inf ,&lhs-inf JCN ( is lhs inf? )
( TODO: determine exponent, round, and add )
( stack is [rhs lhs] but order doesn't matter )
JMP2r JMP2r
&nan POP2 POP2 #ffff JMP2r
&rhs-inf SWP2 #8000 EOR2 EQUk ,&nan JCN POP2 JMP2r
&lhs-inf SWP2 POP2 JMP2r
( TODO )
( lots of stuff including: )
( - subtraction )
( - multiplication )
( - division )
( - floor/ceil/round )
( - min/max )
( - log2/exp2 )