1 # quick-n-dirty way to print out floats in hex
2 # https://www.exploringbinary.com/hexadecimal-floating-point-constants
5 # 0.5 = 0x3f000000 = 0011| 1111 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000
6 # = 0 | 01111110 | 00000000000000000000000
8 # = 0 | 00000000000000000000000 | 01111110
10 # = 0 | 000000000000000000000000 | 01111110
11 # zero-pad mantissa exponent
13 fn test-print-float-hex-normal {
14 var screen-on-stack: screen
15 var screen/esi: (addr screen) <- address screen-on-stack
16 initialize-screen screen, 5, 0x20 # 32 columns should be more than enough
18 var half/xmm0: float <- rational 1, 2
19 print-float-hex screen, half
20 check-screen-row screen, 1, "1.000000P-01 ", "F - test-print-float-hex-normal 0.5"
23 var quarter/xmm0: float <- rational 1, 4
24 print-float-hex screen, quarter
25 check-screen-row screen, 1, "1.000000P-02 ", "F - test-print-float-hex-normal 0.25"
28 var three-quarters/xmm0: float <- rational 3, 4
29 print-float-hex screen, three-quarters
30 check-screen-row screen, 1, "1.800000P-01 ", "F - test-print-float-hex-normal 0.75"
33 var tenth/xmm0: float <- rational 1, 0xa
34 print-float-hex screen, tenth
35 check-screen-row screen, 1, "1.99999aP-04 ", "F - test-print-float-hex-normal 0.1"
38 fn test-print-float-hex-integer {
39 var screen-on-stack: screen
40 var screen/esi: (addr screen) <- address screen-on-stack
41 initialize-screen screen, 5, 0x20 # 32 columns should be more than enough
43 var one-f/xmm0: float <- rational 1, 1
44 print-float-hex screen, one-f
45 check-screen-row screen, 1, "1.000000P00 ", "F - test-print-float-hex-integer 1"
48 var two-f/xmm0: float <- rational 2, 1
49 print-float-hex screen, two-f
50 check-screen-row screen, 1, "1.000000P01 ", "F - test-print-float-hex-integer 2"
53 var ten-f/xmm0: float <- rational 0xa, 1
54 print-float-hex screen, ten-f
55 check-screen-row screen, 1, "1.400000P03 ", "F - test-print-float-hex-integer 10"
58 var minus-ten-f/xmm0: float <- rational -0xa, 1
59 print-float-hex screen, minus-ten-f
60 check-screen-row screen, 1, "-1.400000P03 ", "F - test-print-float-hex-integer -10"
63 fn test-print-float-hex-zero {
64 var screen-on-stack: screen
65 var screen/esi: (addr screen) <- address screen-on-stack
66 initialize-screen screen, 5, 0x20 # 32 columns should be more than enough
68 print-float-hex screen, zero
69 check-screen-row screen, 1, "0 ", "F - test-print-float-hex-zero"
72 fn test-print-float-hex-negative-zero {
73 var screen-on-stack: screen
74 var screen/esi: (addr screen) <- address screen-on-stack
75 initialize-screen screen, 5, 0x20 # 32 columns should be more than enough
78 var negative-zero/xmm0: float <- reinterpret n
79 print-float-hex screen, negative-zero
80 check-screen-row screen, 1, "-0 ", "F - test-print-float-hex-negative-zero"
83 fn test-print-float-hex-infinity {
84 var screen-on-stack: screen
85 var screen/esi: (addr screen) <- address screen-on-stack
86 initialize-screen screen, 5, 0x20 # 32 columns should be more than enough
88 # 0|11111111|00000000000000000000000
89 # 0111|1111|1000|0000|0000|0000|0000|0000
91 var infinity/xmm0: float <- reinterpret n
92 print-float-hex screen, infinity
93 check-screen-row screen, 1, "Inf ", "F - test-print-float-hex-infinity"
96 fn test-print-float-hex-negative-infinity {
97 var screen-on-stack: screen
98 var screen/esi: (addr screen) <- address screen-on-stack
99 initialize-screen screen, 5, 0x20 # 32 columns should be more than enough
101 copy-to n, 0xff800000
102 var negative-infinity/xmm0: float <- reinterpret n
103 print-float-hex screen, negative-infinity
104 check-screen-row screen, 1, "-Inf ", "F - test-print-float-hex-negative-infinity"
107 fn test-print-float-hex-not-a-number {
108 var screen-on-stack: screen
109 var screen/esi: (addr screen) <- address screen-on-stack
110 initialize-screen screen, 5, 0x20 # 32 columns should be more than enough
112 copy-to n, 0xffffffff # exponent must be all 1's, and mantissa must be non-zero
113 var negative-infinity/xmm0: float <- reinterpret n
114 print-float-hex screen, negative-infinity
115 check-screen-row screen, 1, "NaN ", "F - test-print-float-hex-not-a-number"
118 fn print-float-hex screen: (addr screen), n: float {
120 var bits/eax: int <- reinterpret n
124 print-string screen, "0"
127 compare bits, 0x80000000
130 print-string screen, "-0"
133 compare bits, 0x7f800000
136 print-string screen, "Inf"
139 compare bits, 0xff800000
142 print-string screen, "-Inf"
145 var exponent/ecx: int <- copy bits
146 exponent <- shift-right 0x17 # 23 bits of mantissa
148 exponent <- subtract 0x7f
149 compare exponent, 0x80
152 print-string screen, "NaN"
156 var sign/edx: int <- copy bits
157 sign <- shift-right 0x1f
161 print-string screen, "-"
163 $print-float-hex:leading-digit: {
164 # check for subnormal numbers
165 compare exponent, -0x7f
168 print-string screen, "0."
169 exponent <- increment
170 break $print-float-hex:leading-digit
173 print-string screen, "1."
175 var mantissa/ebx: int <- copy bits
176 mantissa <- and 0x7fffff
177 mantissa <- shift-left 1 # pad to whole nibbles
178 print-int32-hex-bits screen, mantissa, 0x18
180 print-string screen, "P"
184 print-string screen, "-"
186 var exp-magnitude/eax: int <- abs exponent
187 print-int32-hex-bits screen, exp-magnitude, 8
190 #? fn main -> _/ebx: int {
192 #? #? test-print-float-hex-negative-zero
193 #? #? print-int32-hex 0, 0
194 #? #? test-print-float-hex-normal