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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
; Zilog eZ80 ANSI C Compiler Release 3.4
; -optsize -nomodsect -peephole -globalopt -localcse
; -const=ROM
FILE "..\UFLTINFO.C"
.assume ADL=1
SEGMENT CODE
; 1 /*************************************************
; 2 * Copyright (C) 1999-2008 by Zilog, Inc.
; 3 * All Rights Reserved
; 4 *************************************************/
; 5
; 6 #include <stddef.h>
; 7 #include <format.h>
; 8 #include <string.h>
; 9
; 10 #define BIT_PACKING 0
; 11 #if (BIT_PACKING == 1)
; 12 typedef union
; 13 {
; 14 float f;
; 15 struct {
; 16 unsigned long sign:1,
; 17 exponent:8,
; 18 mantissa:23;
; 19 } s;
; 20 long l;
; 21 } ieee_795_float;
; 22 #else
; 23 typedef union {
; 24 float f;
; 25 unsigned long l;
; 26 struct {
; 27 unsigned int exponent1:1;
; 28 unsigned int mantissa:23;
; 29 unsigned int dummy:16;
; 30 unsigned int sign:1, exponent2:7;
; 31 } s;
; 32 }ieee_795_float;
; 33 #define EXP(e) ((e##2 << 1) | (e##1))
; 34 #endif
; 35
; 36
; 37 typedef long reg_t;
; 38 typedef unsigned long ureg_t;
; 39 /*************************************************
; 40 *
; 41 * _u_flt_info - Fills flt_info structure
; 42 *
; 43 * Inputs:
; 44 * val - value to be converted
; 45 * info - flt_info structure to be filled in.
; 46 *
; 47 * Outputs:
; 48 * info->sign - TRUE if val is negative; FALSE otherwise.
; 49 * info->exp - The power of ten in scientific notation.
; 50 * info->digits[] - The first digit is the ones place, the rest are
; 51 * decimal digits.
; 52 *
; 53 * Returns:
; 54 * nothing
; 55 *
; 56 * Notes:
; 57 * I moved the rounding out to the calling routine (which does it
; 58 * anyway). As a space optimization, we compute all the digits out
; 59 * to MAXDIGITS, so we no longer pay attention to ndig.<THH>
; 60 * This only works for floating-point numbers having the format as
; 61 * specified in the above structure.
; 62 * Algorithm:
; 63 * Too much precision is lost working in the floating-point domain, so we
; 64 * unpack the fields of the floating-point number, force the base-2
; 65 * exponent to zero and then convert the normalized mantissa to decimal
; 66 * digits.
; 67 *************************************************/
; 68 void _u_flt_info(float val,struct flt_info * info)
; 69 {
__u_flt_info:
LD HL,-25
CALL __frameset
; 70 ieee_795_float rep;
; 71 register reg_t exp; // The base-2 exponent as a signed value.
; 72 register reg_t exp10 = 0; // The base-10 exponent.
LD BC,0
LD (IX+-12),BC
XOR A,A
LD (IX+-9),A
; 73 register ureg_t mant; // The mantissa, as an unsigned value.
; 74 register ureg_t i;
; 75
; 76 // Clear the info structure (represents +0.000000000).
; 77 memset(info,0,sizeof(struct flt_info));
LD BC,12
PUSH BC
LD BC,0
PUSH BC
LD BC,(IX+12)
PUSH BC
CALL _memset
POP BC
POP BC
POP BC
; 78
; 79 rep.f = val;
LD BC,(IX+6)
LD A,(IX+9)
LD (IX+-18),BC
LD (IX+-15),A
; 80
; 81 // All zero means zero!
; 82 if (rep.l == 0) return;
LD HL,BC
LD E,(IX+-15)
CALL __lcmpzero
JR Z,L_26
; 83
; 84 if (rep.s.sign)
LD HL,(IX+-15)
LD A,16
CALL __ishl_b
LD A,23
CALL __ishru_b
CALL __icmpzero
JR Z,L_3
; 85 info->flags = FLT_INFO_SIGN;
LD IY,(IX+12)
LD (IY+0),1
L_3:
; 86
; 87 // OK, unpack some fields.
; 88
; 89 exp = EXP(rep.s.exponent);
LEA BC,IX+-18
LD (IX+-25),BC
LD IY,(IX+-25)
LD HL,(IY+3)
LD A,17
CALL __ishl_b
LD A,17
CALL __ishru_b
ADD HL,HL
LD DE,HL
LD HL,(IY+0)
LD A,23
CALL __ishru_b
LD BC,HL
LD HL,DE
CALL __ior
LD BC,HL
CALL __itol
LD (IX+-8),BC
LD (IX+-5),A
; 90
; 91 mant = rep.s.mantissa;
LD HL,(IY+0)
ADD HL,HL
LD A,1
CALL __ishru_b
LD BC,HL
CALL __itol
LD (IX+-4),BC
LD (IX+-1),A
; 92
; 93 // If the exponent is 0xff, it is a special representation (Inf or NaN).
; 94 if (exp == 0xff)
LD HL,(IX+-8)
LD E,(IX+-5)
XOR A,A
LD BC,255
CALL __lcmps
JR NZ,L_9
; 95 {
; 96 // If the mantissa is zero, it's Inf; otherwise, NaN.
; 97 if (rep.s.mantissa == 0)
LD HL,(IX+-18)
ADD HL,HL
LD A,1
CALL __ishru_b
CALL __icmpzero
JR NZ,L_5
; 98 info->flags |= FLT_INFO_INF;
LD IY,(IX+12)
LD A,(IY+0)
SET 1,A
LD (IY+0),A
; 99 else
JR L_26
L_5:
; 100 info->flags |= FLT_INFO_NAN;
LD IY,(IX+12)
LD A,(IY+0)
SET 2,A
LD (IY+0),A
; 101 return;
JR L_26
; 102 }
L_9:
; 103
; 104 // If the exponent is zero, this is a denormalized number.
; 105 if (exp != 0)
LD HL,(IX+-8)
LD E,(IX+-5)
CALL __lcmpzero
JR Z,L_10
; 106 {
; 107 mant |= 0x800000uL; // Or in the implicit bit.
LD HL,(IX+-4)
LD E,(IX+-1)
LD BC,8388608
XOR A,A
CALL __lor
LD (IX+-4),HL
LD (IX+-1),E
; 108 --exp; // And decrement the exponent.
LD HL,(IX+-8)
LD E,(IX+-5)
LD BC,16777215
LD A,255
CALL __ladd
LD (IX+-8),HL
LD (IX+-5),E
; 109 // (denormalized numbers are offset 126, not offset 127.
; 110 // In the assembly realm, we could handle this in one step by shifting the 1
; 111 // into the msb of a left-adjusted 23-bit mantissa.
; 112 }
L_10:
; 113
; 114 // Now, we have something that looks like
; 115 // 0000 0000 x.xxx xxxx xxxx xxxx xxxx xxxx
; 116 // in mant, and the binary exponent -- offset by +126 -- in exp.
; 117 //
; 118 // Now, we want to shift the mantissa left so we leave a few guard bits at the
; 119 // right end of the word, but we also want to leave four bits to the left of
; 120 // the decimal point so we can pick off the decimal digits later.
; 121 //
; 122 // If we move the decimal point from where it is to the right end of the first
; 123 // nibble, we essentially subtract 5 bits from the offset. But shifting the
; 124 // mantissa left by the same number of bits cancels the difference.
; 125 // Let's do the shift and subtract the remaining offset.
; 126 mant <<= 5;
LD BC,(IX+-4)
LD A,(IX+-1)
LD L,5
CALL __lshl
LD (IX+-4),BC
LD (IX+-1),A
; 127 exp -= 126;
LD HL,(IX+-8)
LD E,(IX+-5)
LD A,255
LD BC,16777090
CALL __ladd
LD (IX+-8),HL
LD (IX+-5),E
; 128
; 129 // Now the picture is
; 130 // 000x.xxxx xxxx xxxx xxxx xxxx xxx0 0000
; 131
; 132 // We want to drive the base-2 exponent to zero, and accumulate multiplication
; 133 // or division by 10 in the base-10 exponent.
; 134 // We start by decrementing the exponent while shifting the bits in the
; 135 // mantissa left by 1 each time. This changes the representation, but not its
; 136 // value.
; 137 // If the msb becomes true, a shift would cause us to lose information, so in
; 138 // that case, we divide the mantissa by 10 and increment the base-10
; 139 // exponent. Once again, this changes the bits in the representation, but not
; 140 // the value that is represented -- at least not very much. Division by 10
; 141 // may cause bits to fall of the end of the 32-bit word, and some precision is
; 142 // lost. But we always have a whole nibble, plus about 2/3 of a bit as guard
; 143 // bits.
; 144
; 145
; 146 // At most one of the following while loops will execute.
; 147
; 148 // If the base-2 exponent is positive, this while loop drives it to zero by
; 149 // repeatedly shifting the mantissa left by one bit and decrementing the exponent.
; 150 // This changes the representation, but leaves unchanged the value
; 151 // it represents.
; 152 while (exp > 0)
JR L_14
L_15:
; 153 {
; 154 if ((mant >> 27) > 9)
LD BC,(IX+-4)
LD A,(IX+-1)
LD L,27
CALL __lshru
LD HL,9
CALL __lcmpu
JR NC,L_12
; 155 {
; 156 // If a left shift would cause something greater than 9 to appear in the
; 157 // first nibble, normalize right.
; 158 mant /= 5;
LD HL,(IX+-4)
LD E,(IX+-1)
LD BC,5
XOR A,A
CALL __ldivu
LD (IX+-4),HL
LD (IX+-1),E
; 159 ++exp10;
LD HL,(IX+-12)
LD E,(IX+-9)
LD A,1
CALL __ladd_b
LD (IX+-12),HL
LD (IX+-9),E
; 160 }
; 161 else
JR L_13
L_12:
; 162 mant <<= 1;
LD BC,(IX+-4)
LD A,(IX+-1)
LD L,1
CALL __lshl
LD (IX+-4),BC
LD (IX+-1),A
L_13:
; 163 // Dividing by 5 and adding one to the base-10 exponent is the same as
; 164 // shifting left by one bit: They both multiply the representation by two.
; 165 // Decrementing the base-2 exponent leaves the entire representation unchanged.
; 166 --exp;
LD HL,(IX+-8)
LD E,(IX+-5)
LD BC,16777215
LD A,255
CALL __ladd
LD (IX+-8),HL
LD (IX+-5),E
; 167 }
L_14:
OR A,A
SBC HL,HL
LD E,0
LD BC,(IX+-8)
LD A,(IX+-5)
CALL __lcmps
CALL __setflag
JP M,L_15
; 168
; 169 // If the base-2 exponent is negative, this while loop drives it to zero by
; 170 // repeatedly shifting the mantissa right by one bit and incrementing the exponent.
; 171 while (exp < 0)
JR L_19
L_20:
; 172 {
; 173 if ((mant >> 28) < 2)
LD BC,(IX+-4)
LD A,(IX+-1)
LD L,28
CALL __lshru
LD E,A
LD HL,BC
LD BC,2
XOR A,A
CALL __lcmpu
JR NC,L_17
; 174 {
; 175 // If a right shift would cause the first nibble to go to zero, normalize left.
; 176 mant *= 5;
LD HL,(IX+-4)
LD E,(IX+-1)
LD BC,5
XOR A,A
CALL __lmulu
LD (IX+-4),HL
LD (IX+-1),E
; 177 --exp10;
LD HL,(IX+-12)
LD E,(IX+-9)
LD BC,16777215
LD A,255
CALL __ladd
LD (IX+-12),HL
LD (IX+-9),E
; 178 }
; 179 else
JR L_18
L_17:
; 180 mant >>= 1;
LD BC,(IX+-4)
LD A,(IX+-1)
LD L,1
CALL __lshru
LD (IX+-4),BC
LD (IX+-1),A
L_18:
; 181 // Multiplying by 5 and subtracting one from the base-10 exponent is the
; 182 // same as shifting right by one bit: They both divide the representation by 2.
; 183 // Incrementing the base-2 exponent leaves the entire representation unchanged.
; 184 ++exp;
LD HL,(IX+-8)
LD E,(IX+-5)
LD A,1
CALL __ladd_b
LD (IX+-8),HL
LD (IX+-5),E
; 185 }
L_19:
LD HL,(IX+-8)
LD E,(IX+-5)
CALL __lcmpzero
CALL __setflag
JP M,L_20
; 186
; 187 // Now, that we have driven the base-2 exponent to zero, the base-10 exponent
; 188 // is ready.
; 189 info->exp = exp10;
LD A,(IX+-12)
LD IY,(IX+12)
LD (IY+1),A
; 190
; 191 // Finally, we can pick off the digits.
; 192 // This is done by picking off the first nibble (which must be a decimal digit
; 193 // between 1 and 9 inclusive) and then multiplying the remaining 28 bits by
; 194 // 10. That leaves another decimal digit (in [0,9]) in the top nibble, so we
; 195 // just loop for as many digits as we want.
; 196 for (i=0; i < MAXDIGITS; ++i)
LD BC,0
LD (IX+-22),BC
XOR A,A
LD (IX+-19),A
JR L_25
L_23:
; 197 {
; 198 info->digits[i] = mant >> 28;
LD BC,(IX+-4)
LD A,(IX+-1)
LD L,28
CALL __lshru
LD IY,(IX+12)
LEA HL,IY+2
LD DE,(IX+-22)
ADD HL,DE
LD (HL),C
; 199 mant &= 0xfffffffuL;
LD HL,(IX+-4)
LD E,(IX+-1)
LD BC,16777215
LD A,15
CALL __land
LD (IX+-4),HL
LD (IX+-1),E
; 200 mant *= 10;
LD HL,(IX+-4)
LD E,(IX+-1)
LD BC,10
XOR A,A
CALL __lmulu
LD (IX+-4),HL
LD (IX+-1),E
LD HL,(IX+-22)
LD E,(IX+-19)
LD A,1
CALL __ladd_b
LD (IX+-22),HL
LD (IX+-19),E
; 201 }
L_25:
LD HL,(IX+-22)
LD E,(IX+-19)
LD BC,10
XOR A,A
CALL __lcmpu
JR C,L_23
; 202
; 203 /* Rounding moved out to callers of _u_flt_info(). */
; 204 }
L_26:
LD SP,IX
POP IX
RET
;**************************** __u_flt_info ***************************
;Name Addr/Register Size Type
;_memset IMPORT ----- function
;i IX-22 4 variable
;rep IX-18 6 variable
;exp10 IX-12 4 variable
;exp IX-8 4 variable
;mant IX-4 4 variable
;info IX+12 3 parameter
;val IX+6 4 parameter
; Stack Frame Size: 40 (bytes)
; Spill Code: 0 (instruction)
XREF _memset:ROM
XREF __lcmps:ROM
XREF __lcmpu:ROM
XREF __ladd:ROM
XREF __ldivu:ROM
XREF __lmulu:ROM
XREF __ior:ROM
XREF __lor:ROM
XREF __land:ROM
XREF __lshl:ROM
XREF __lshru:ROM
XREF __itol:ROM
XREF __frameset:ROM
XREF __setflag:ROM
XREF __icmpzero:ROM
XREF __lcmpzero:ROM
XREF __ladd_b:ROM
XREF __ishl_b:ROM
XREF __ishru_b:ROM
XDEF __u_flt_info
END
|