about summary refs log tree commit diff stats
path: root/408print-float.mu
blob: 67e46f032bf14dc19a013787e75d63763373684a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# quick-n-dirty way to print out floats in hex

# examples:
#   0.5 = 0x3f000000 = 0011| 1111 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000
#                    = 0 | 01111110 | 00000000000000000000000
#                      +   exponent   mantissa
#                    = 0 | 00000000000000000000000 | 01111110
#                          mantissa                  exponent
#                    = 0 | 000000000000000000000000 | 01111110
#                          zero-pad mantissa          exponent
#                   =   +1.000000                   P -01
fn test-print-float-normal {
  var screen-on-stack: screen
  var screen/esi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 5, 0x20  # 32 columns should be more than enough
  # print 0.5
  var one/eax: int <- copy 1
  var half/xmm0: float <- convert one
  var two/eax: int <- copy 2
  var two-f/xmm1: float <- convert two
  half <- divide two-f
  print-float screen, half
  #
  check-screen-row screen, 1, "1.000000P-01 ", "F - test-print-float-normal"
}

fn test-print-float-zero {
  var screen-on-stack: screen
  var screen/esi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 5, 0x20  # 32 columns should be more than enough
  # print 0
  var zero: float
  print-float screen, zero
  #
  check-screen-row screen, 1, "0 ", "F - test-print-float-zero"
}

fn test-print-float-negative-zero {
  var screen-on-stack: screen
  var screen/esi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 5, 0x20  # 32 columns should be more than enough
  # print 0
  var n: int
  copy-to n, 0x80000000
  var negative-zero/xmm0: float <- reinterpret n
  print-float screen, negative-zero
  #
  check-screen-row screen, 1, "-0 ", "F - test-print-float-negative-zero"
}

fn test-print-float-infinity {
  var screen-on-stack: screen
  var screen/esi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 5, 0x20  # 32 columns should be more than enough
  # print
  var n: int
  #          0|11111111|00000000000000000000000
  #          0111|1111|1000|0000|0000|0000|0000|0000
  copy-to n, 0x7f800000
  var infinity/xmm0: float <- reinterpret n
  print-float screen, infinity
  #
  check-screen-row screen, 1, "Inf ", "F - test-print-float-infinity"
}

fn test-print-float-negative-infinity {
  var screen-on-stack: screen
  var screen/esi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 5, 0x20  # 32 columns should be more than enough
  # print
  var n: int
  copy-to n, 0xff800000
  var negative-infinity/xmm0: float <- reinterpret n
  print-float screen, negative-infinity
  #
  check-screen-row screen, 1, "-Inf ", "F - test-print-float-negative-infinity"
}

fn test-print-float-not-a-number {
  var screen-on-stack: screen
  var screen/esi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 5, 0x20  # 32 columns should be more than enough
  # print
  var n: int
  copy-to n, 0xffffffff  # exponent must be all 1's, and mantissa must be non-zero
  var negative-infinity/xmm0: float <- reinterpret n
  print-float screen, negative-infinity
  #
  check-screen-row screen, 1, "Nan ", "F - test-print-float-not-a-number"
}

fn print-float screen: (addr screen), n: float {
$print-float:body: {
  # - special names
  var bits/eax: int <- reinterpret n
  compare bits, 0
  {
    break-if-!=
    print-string screen, "0"
    break $print-float:body
  }
  compare bits, 0x80000000
  {
    break-if-!=
    print-string screen, "-0"
    break $print-float:body
  }
  compare bits, 0x7f800000
  {
    break-if-!=
    print-string screen, "Inf"
    break $print-float:body
  }
  compare bits, 0xff800000
  {
    break-if-!=
    print-string screen, "-Inf"
    break $print-float:body
  }
  var exponent/ecx: int <- copy bits
  exponent <- shift-right 0x17  # 23 bits of mantissa
  exponent <- and 0xff
  compare exponent, 0xff
  {
    break-if-!=
    print-string screen, "Nan"
    break $print-float:body
  }
  # - regular numbers
  var sign/edx: int <- copy bits
  sign <- shift-right 0x1f
  {
    compare sign, 1
    break-if-!=
    print-string screen, "-"
  }
  $print-float:leading-digit: {
    # check for subnormal numbers
    compare exponent, 0
    {
      break-if-!=
      print-string screen, "0."
      exponent <- increment
      break $print-float:leading-digit
    }
    # normal numbers
    print-string screen, "1."
  }
  var mantissa/ebx: int <- copy bits
  mantissa <- and 0x7fffff
  print-int32-hex-bits screen, mantissa, 0x18
  # print exponent
  print-string screen, "P"
  exponent <- subtract 0x7f
  compare exponent, 0
  {
    break-if->=
    print-string screen, "-"
  }
  var exp-magnitude/eax: int <- abs exponent
  print-int32-hex-bits screen, exp-magnitude, 8
}
}

#? fn main -> r/ebx: int {
#?   run-tests
#? #?   test-print-float-negative-zero
#? #?   print-int32-hex 0, 0
#? #?   test-print-float-normal
#?   r <- copy 0
#? }