symbol -> bareword

This commit is contained in:
~d6 2022-11-30 10:27:20 -05:00
parent c1a11c0522
commit 9bfc460499
3 changed files with 92 additions and 60 deletions

View File

@ -1,28 +1,28 @@
( bfloat16.tal )
( )
( This file implements the bfloat16 format. )
( )
( bfloat16.tal )
( )
( This file implements the bfloat16 format. )
( )
( This differs from IEEE float-16 by providing more exponent bits )
( in exchange for fewer mantissa bits. In other words it trades )
( coarser precision for larger numerical range. )
( )
( The bfloat16 value uses 16-bits divided as follows: )
( - sign (1 bit, 0-1) )
( - exponent (8 bits, 0-255) )
( - mantissa (7 bits, 0-127) )
( )
( Kinds of values: )
( - zeros (exponent==0 mantissa==0) )
( - subnormal (exponent==0 mantissa!=0) )
( - infinities (exponent==255 mantissa==0) )
( - nans (exponent==255 mantissa!=0) )
( - normal (everything else) )
( )
( )
( The bfloat16 value uses 16-bits divided as follows: )
( - sign (1 bit, 0-1) )
( - exponent (8 bits, 0-255) )
( - mantissa (7 bits, 0-127) )
( )
( Kinds of values: )
( - zeros (exponent==0 mantissa==0) )
( - subnormal (exponent==0 mantissa!=0) )
( - infinities (exponent==255 mantissa==0) )
( - nans (exponent==255 mantissa!=0) )
( - normal (everything else) )
( )
( Equations: )
( - normal = -1^sign * 2^(exponent - 127) * (1 + mantissa/128) )
( - subnormal = -1^sign * 2^-126 * mantissa/128 )
( (exponent ranges from 1 to 254 since 0 and 255 are special) )
( )
( )
( VALUE SIGN EXPONENT MANTISSA NOTES )
( 0 0 00000000 0000000 )
( -0 1 00000000 0000000 mostly equivalent to zero )
@ -36,30 +36,30 @@
( +inf 0 11111111 0000000 positive infinity )
( -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) )
( )
( )
( 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: )
( )
( )
( HEXADECIMAL SIGN EXPONENT MANTISSA DECIMAL )
( 0x1.00p+00 1 10000000 0000000 1.0 )
( 0x0.01p-7f 0 00000000 0000001 ~9.184e-41 )
( -0x1.80p+02 1 10000010 1000000 -6.0 )
( 0x1.c0p+02 0 10000010 1100000 7.0 )
( )
( )
( Eventually I'd like to display integral part of the number )
( in a more natural way but the 1.xx format is OK for now. )
( )
( )
( For consistency zeros are emitted as 0x00p+00 and -0x00p+00. )
( Infinities are "inf" and "-inf" and NaN is "nan". )
@ -76,7 +76,7 @@
( #00 #86 #7f ;bf16-join JSR2 ;emit-bf16 JSR2 NEWLINE )
( #ff ;byte-to-bf16 JSR2 ;test JSR2 )
( #ff ;byte-to-bf16 JSR2 #01 ;round-shift JSR2 ;test JSR2
#03 ;byte-to-bf16 JSR2 ;test JSR2
#03 ;byte-to-bf16 JSR2 ;test JSR2 )
#7f80 ;test JSR2
#ff80 ;test JSR2
#ff81 ;test JSR2
@ -87,12 +87,18 @@
#3f80 ;test JSR2
#bf80 ;test JSR2
#4000 ;test JSR2
#4080 ;test JSR2 )
#00 #00 DIV ( exit )
BRK
#4080 ;test JSR2
#4100 ;test JSR2
#3f80 ;test JSR2
#3f80 DUP2 ;add-bf16 JSR2 ;test JSR2
#010f DEO BRK
@test ( x* -> )
DUP2 ;emit-u16 JSR2 SPACE LIT '- EMIT LIT '> EMIT SPACE ;emit-bf16 JSR2 NEWLINE JMP2r
DUP2 ;emit-u16 JSR2 SPACE
LIT "- EMIT LIT "> EMIT SPACE
;emit-bf16 JSR2 NEWLINE JMP2r
@emit-digit ( d^ -> )
DUP #0a LTH
@ -111,36 +117,36 @@
JMP2r
@emit-s8 ( x^ -> )
DUP #07 SFT ,&is-negative JCN LIT '+ EMIT ;emit-u8 JSR2 JMP2r
&is-negative LIT '- EMIT #7f AND #80 SWP SUB ;emit-u8 JSR2 JMP2r
DUP #07 SFT ,&is-negative JCN LIT "+ EMIT ;emit-u8 JSR2 JMP2r
&is-negative LIT "- EMIT #7f AND #80 SWP SUB ;emit-u8 JSR2 JMP2r
@emit-s16 ( x* -> )
DUP2 #0f SFT2 SWP POP ,&is-negative JCN LIT '+ EMIT ;emit-u16 JSR2 JMP2r
&is-negative LIT '- EMIT #7fff AND2 #8000 SWP2 SUB2 ;emit-u16 JSR2 JMP2r
DUP2 #0f SFT2 SWP POP ,&is-negative JCN LIT "+ EMIT ;emit-u16 JSR2 JMP2r
&is-negative LIT "- EMIT #7fff AND2 #8000 SWP2 SUB2 ;emit-u16 JSR2 JMP2r
@emit-bf16 ( x* -> )
;bf16-split JSR2 ( sgn exp mnt )
( sentinel or value )
OVR #ff NEQ ,&non-sentinal JCN
,&is-nan JCN POP #00 EQU ,&pos-inf JCN LIT '- EMIT
&pos-inf LIT 'i EMIT LIT 'n EMIT LIT 'f EMIT JMP2r
&is-nan LIT 'n EMIT LIT 'a EMIT LIT 'n EMIT JMP2r
,&is-nan JCN POP #00 EQU ,&pos-inf JCN LIT "- EMIT
&pos-inf LIT "i EMIT LIT "n EMIT LIT "f EMIT JMP2r
&is-nan LIT "n EMIT LIT "a EMIT LIT "n EMIT JMP2r
( zero or non-zero )
&non-sentinal DUP2 ORA ,&non-zero JCN
POP2 ,&is-negative-zero JCN ,&zero-suffix JMP
&is-negative-zero LIT '- EMIT
&zero-suffix LIT '0 EMIT LIT 'x EMIT LIT '0 EMIT LIT '. EMIT
#00 ;emit-u8 JSR2 LIT 'p EMIT #00 ;emit-s8 JSR2 JMP2r
&is-negative-zero LIT "- EMIT
&zero-suffix LIT "0 EMIT LIT "x EMIT LIT "0 EMIT LIT ". EMIT
#00 ;emit-u8 JSR2 LIT "p EMIT #00 ;emit-s8 JSR2 JMP2r
( normal or subnormal )
&non-zero ROT ,&is-negative JCN ,&post-sgn JMP
&is-negative LIT '- EMIT
&post-sgn LIT '0 EMIT LIT 'x EMIT
OVR ,&is-normal JCN LIT '0 ,&suffix JMP &is-normal LIT '1
&suffix EMIT LIT '. EMIT ;emit-u8 JSR2
LIT 'p EMIT #7f SUB ;emit-s8 JSR2
&is-negative LIT "- EMIT
&post-sgn LIT "0 EMIT LIT "x EMIT
OVR ,&is-normal JCN LIT "0 ,&suffix JMP &is-normal LIT "1
&suffix EMIT LIT ". EMIT ;emit-u8 JSR2
LIT "p EMIT #7f SUB ;emit-s8 JSR2
JMP2r
@bf16-join ( sgn^ exp^ mta^ -> x* )
@ -162,6 +168,8 @@
%EXPONENT { #10 SFT2 POP }
%MANTISSA { NIP #7f AND }
%MAX { GTHk JMP SWP POP }
( returns full mta: #00 to #ff )
( normal numbers will be >= #80 )
( subnormal numbers will be < #80 )
@ -306,12 +314,25 @@
( 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-nan JSR2 STH SWP2 ( y x [ynan?] )
DUP2 ;is-nan JSR2 STH SWP2 ( x y [xnan? ynan? ] )
STH2r ORA ,&nan JCN ( x y )
DUP2 ;is-inf ,&rhs-inf JCN ( is rhs inf? )
SWP2 ;is-inf ,&lhs-inf JCN ( is lhs inf? )
DUP2 ;is-inf JSR2 ,&y-inf JCN ( x y )
OVR2 ;is-inf JSR2 ,&x-inf JCN ( x y )
OVR2 OVR2 ( x y x y )
EXPONENT STH EXPONENT STHr ( x* y* ex^ ey^ )
EQUk ,&same-exponent JCN
LTHk ,&smaller-x JCN
SWP STH2 SWP2 STH2r
&smaller-x ( s* b* es^ eb^ )
STHk SWP SUB ( s* b* delta^ [eb] )
&same-epxponent ( x* y* ex^ ey^ )
( TODO: determine exponent, round, and add )
( stack is [rhs lhs] but order doesn't matter )
@ -319,8 +340,8 @@
JMP2r
&nan POP2 POP2 #ffff JMP2r
&rhs-inf SWP2 #8000 EOR2 EQUk ,&nan JCN POP2 JMP2r
&lhs-inf SWP2 POP2 JMP2r
&y-inf SWP2 #8000 EOR2 EQU2k ,&nan JCN POP2 JMP2r
&x-inf POP2 JMP2r
( TODO )
( lots of stuff including: )

View File

@ -219,6 +219,16 @@
@x16-remainder ( x* y* -> x%y* )
DIV2k MUL2 SUB2 JMP2r
@x16-from-s8 ( n^ -> x* )
#00 JMP2r
@x16-from-s16 ( n* -> x* )
DUP2 #ff80 GTH2 ,&neg JCN
DUP2 #007f GTH2 ,&error JCN
NIP #00 SWP JMP2r
&neg NIP #ff SWP JMP2r
&error #0000 DIV
( 1.5 -> 1, 0.5 -> 0, -1.5 -> -1 )
@x16-to-s16 ( x* -> whole* )
DUP2 #7fff GTH2 ,&neg JCN ( x0 x1 )

View File

@ -86,6 +86,7 @@
( "abcd" 7 bytes )
( "abcde" 12 bytes )
( TODO: symbols should display as barewords by default )
( TODO: special case empty string? )
%NL { #0a18 DEO }