From 128093cf097815486ca59fac7b5a50523667b368 Mon Sep 17 00:00:00 2001 From: d6 Date: Tue, 18 Jan 2022 00:10:12 -0500 Subject: [PATCH] update --- bfloat16.tal | 63 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/bfloat16.tal b/bfloat16.tal index 4cf8060..91bd5fa 100644 --- a/bfloat16.tal +++ b/bfloat16.tal @@ -37,6 +37,16 @@ ( -inf 1 11111111 0000000 negative infinity ) ( 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. ) ( ) ( Bfloat16 values are emitted in a hexadecimal format: ) @@ -172,6 +182,19 @@ @non-zero ( x* -> bool^ ) #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 ) ( ) ( We round differently depending on the value to be lost: ) @@ -207,22 +230,7 @@ &rnd-up ( [mta n] ) STH2r SFT INC JMP2r -@is-nan ( x* -> bool^ ) - 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 - +( lift an integer byte into a bfloat16 value ) @byte-to-bf16 ( n^ -> x* ) #86 SWP ( exp n ) &loop @@ -298,4 +306,27 @@ ( 5. inf + x = inf ) ( 6. -inf + x = -inf ) @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 + + &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 )