-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmath.asm
1397 lines (1201 loc) · 40.2 KB
/
math.asm
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;-------------------------------------------------------------------------------
;
; Z80 IEEE 754 FLOATING POINT WITH TRUNCATION, NO INF/NAN
;
; Author:
; Dr. Robert van Engelen, Copyright 2022
;
; Features:
; - IEEE 754 single precision floating point:
; addition, subtraction, multiplication, division, negation, absolute,
; truncation, flooring, rounding, integer to/from float conversion,
; string to/from float conversion
; - "memoryless" using registers only (+shadow), at most one push+pop per flop
; - optimized for speed and reduced code size (no loop unrolling)
; - no rounding modes, result is truncated
; (floating point string parsing and output are rounded)
; - no inf/nan; routines return error condition (cf set)
; - no negative zero
; - no subnormals
; - extensively tested
;
;-------------------------------------------------------------------------------
;
; BSD 3-Clause License
;
; Copyright (c) 2022, Robert van Engelen
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;
; 1. Redistributions of source code must retain the above copyright notice, this
; list of conditions and the following disclaimer.
;
; 2. Redistributions in binary form must reproduce the above copyright notice,
; this list of conditions and the following disclaimer in the documentation
; and/or other materials provided with the distribution.
;
; 3. Neither the name of the copyright holder nor the names of its
; contributors may be used to endorse or promote products derived from
; this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
; CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;
; FLOATING POINT REPRESENTATION
;
;-------------------------------------------------------------------------------
;
; IEEE 754 binary32 single precision floating point
; assigned to CPU registers BCDE
; _____________________________________
; |s| exp | mantissa |
; |s|_________|_man2__|__man1__|__man0__|
; |s eeeeeee|e mmmmmmm|mmmmmmmm|mmmmmmmm|
; |____B____|____C____|____D___|____E___|
;
; exponent bits: 8
; exponent bias: 127 (0x7f) for IEEE 754 or 128 (0x80) symmetry
; exponent zero: indicates zero floating point value
; mantissa bits: 24 (including implicit msb of 1)
; infinity/nan: no (errors are indicated with cf set)
; range: 2^-126 to 2^+127 assuming bias = 127
; 2^-127 to 2^+127 assuming bias = 128
;
; examples with exponent bias = 127:
;
; 0 = 00 00 00 00
; 1 = 3f 80 00 00
; 2 = 40 00 00 00
; 3 = 40 40 00 00
; -0 = 80 00 00 00
; -1 = bf 80 00 00
; -2 = c0 00 00 00
; -3 = c0 40 00 00
; inf = 7f 80 00 00 n/a (invalid value)
; -inf = ff 80 00 00 n/a (invalid value)
; nan = s 11111111 xxxxxxx xxxxxxxx xxxxxxxx at least one x is 1
; n/a (invalid value)
;
; IEEE 754 binary floating point allows floating point values to
; be compared as if comparing 32 bit signed integers with 'i<':
;
; For positive, zero and opposite signs:
; zero: 0 = 00 00 00 00 (-0 = 80 00 00 00 is never produced)
; 1 < 2: 1 = 3f 80 00 00 i< 2 = 40 00 00 00
; -1 < 2: -1 = bf 80 00 00 i< 2 = 40 00 00 00
; When both signs are negative the comparison is inverted:
; -2 < -1: -2 = c0 00 00 00 i> -1 = bf 80 00 00
;
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;
; CONFIGURATION
;
;-------------------------------------------------------------------------------
; Allow empty exponent values with no digits after E (1) or return error (0)
EXPEMPTY = 1
;-------------------------------------------------------------------------------
;
; CONSTANTS
;
;-------------------------------------------------------------------------------
bias .equ 127 ; exponent bias 127 IEEE 754 or 128
;-------------------------------------------------------------------------------
;
; ASSEMBLY MACROS
;
;-------------------------------------------------------------------------------
; Extract the exponent of float bcde -> a
.macro EXPA
ld a,c ;
add a ; set cf to lowest order exponent bit
ld a,b ;
adc a ; exponent -> a, set z if zero, set cf if negative
.endm
; Extract the exponent of float bcde -> h, modifies l with c << 1 -> l
.macro EXPH
ld h,b ;
ld l,c ;
add hl,hl ; exponent -> h, set cf if negative
.endm
; Test if float bcde is zero, modifies a
.macro ISZERO
ld a,c ;
and 0x80 ;
or b ; set z if float is zero (no negative zero)
.endm
; Test if float bcde is negative or zero, modifies a
.macro ISNEGZ
EXPA ; set z if float is zero, set cf if negative
.endm
; Test if float bcde is positive or zero
.macro ISPOSZ
bit 7,b ; set z if float is positive or zero
.endm
;-------------------------------------------------------------------------------
;
; FLOATING POINT NEGATION
;
; fneg: -bcde -> bcde
; no errors (cf reset)
; a,b modified
;
;-------------------------------------------------------------------------------
fneg: ld a,b ;
or c ;
ret z ; if bcde is zero then return zero (cf reset)
ld a,b ; sign bit 7 and exponent
xor 0x80 ; invert sign bit 7
ld b,a ; set new sign bit 7
ret ; return float bcde (cf reset)
;-------------------------------------------------------------------------------
;
; FLOATING POINT ABSOLUTE VALUE
;
; fabs: |bcde| -> bcde
; no errors (cf reset)
; a,b modified
;
;-------------------------------------------------------------------------------
fabs: res 7,b ; reset sign b bit 7
or a ; reset cf
ret ;
;-------------------------------------------------------------------------------
;
; FLOATING POINT SUBTRACTION
;
; fsubx: bcde - bcde' -> bcde
; fsuby: bcde' - bcde -> bcde
; cf set on overflow
; a,b,c,d,e,h,l,a',b',c',d',e',h',l' modified
;
;-------------------------------------------------------------------------------
fsubx: exx ; swap bcde with bcde'
fsuby: call fneg ; -bcde -> bcde
; FALL THROUGH ; -bcde + bcde' -> bcde
;-------------------------------------------------------------------------------
;
; FLOATING POINT ADDITION
;
; fadd: bcde + bcde' -> bcde
; cf set on overflow
; a,b,c,d,e,h,l,a',b',c',d',e',h',l' modified
;
;-------------------------------------------------------------------------------
fadd: EXPH ; exponent -> h
ld a,h ;
exx ; activate bcdehl'
or a ; check if exponent is zero (no subnormals)
ret z ; if bcde is zero then return float bcde' (cf reset)
EXPH ; exponent' -> h'
ld a,h ;
exx ; activate bcdehl
or a ; check if exponent' is zero (no subnormals)
ret z ; if bcde' is zero then return float bcde (cf reset)
; compare the exponents
sub h ; exponent' - exponent -> a
jr nc,1$ ; if exponent' < exponent then
neg ; exponent - exponent' -> a
exx ; swap bcdehl with bcdehl' (commutative symmetry)
1$: exx ; activate bcdehl' (symmetry allows ignoring the swap above)
cp 24 ; if exponent' - exponent >= 24 then
ret nc ; return float bcde' (cf reset)
; save signs and set mantissas high bits
ld l,b ; save sign' b' bit 7 to l'
ld b,h ; save h' -> b' exponent'
set 7,c ; set bit 7 of man2' c'
exx ; activate bcdehl
ld l,b ; save sign b bit 7 to l
set 7,c ; set bit 7 of man2 c
; align mantissa cde to cde' and set result exponent b = exponent'
or a ;
jr z,3$ ; if exponent' <> exponent then
ld b,a ; exponent' - exponent -> b loop counter
2$: srl c ; 8 ; loop
rr d ; 8 ;
rr e ; 8 ; cde >> 1 -> cde
djnz 2$ ; 13(37); repeat exponent' - exponent times
3$: add h ;
ld b,a ; exponent' - exponent + exponent -> b = exponent'
; compare signs
ld a,l ; sign' l bit 7 -> a
exx ; activate bcdehl'
xor l ; sign' xor sign -> a, reset cf
jp m,subtract ; if signs differ then subtract
; add mantissa cde to cde' to produce result mantissa ahl
ld a,c ; c' -> a
push de ; push de'
exx ; activate bcdehl
pop hl ; pop hl with de'
add hl,de ;
adc c ; cde' + cde -> ahl with result mantissa
shiftoncarry: ; shift mantissa ahl right if carry
jr nc,finalize ; if no carry then finalize
inc b ; increment result exponent b
ret z ; if exponent is zero then return overflow error (cf set)
rra ;
rr h ;
rr l ; 1.ahl >> 1 -> ahl
finalize: ; finalize bahl to return float bcde with sign' l' bit 7
.if bias - 128 ; check overflow when bias = 127, for bias = 128 the result exponent <= bias + 127 = 255
inc b ; check if result exponent overflowed
scf ; set cf
ret z ; if result exponent overflowed then return error (cf set)
dec b ;
.endif
add a ; shift left man2 a to assign man2 bit 7 below
exx ;
rl l ; set cf to the sign' l' bit 7
exx ;
rr b ; rotate right result exponent b and assign sign' bit
rra ; rotate right result man2 a and assign exponent bit
ld c,a ;
ex de,hl ; ahl -> cde set result mantissa
xor a ; 0 -> a, reset cf, reset v, set z
ret ; return float bcde (cf reset)
resubtract: ; redo subtract mantissa cde from cde' to produce result bcde
exx ;
sbc a ; 0xff -> a because cf is set
sub l ; complement l -> a, reset cf
exx ;
ld l,a ; restore sign' l' bit 7 to the complement of sign l bit 7
subtract: ; subtract mantissa cde from cde' to produce ahl and nornalized result bcde
ld a,c ; c' -> a
push de ; push de'
exx ; activate bcdehl
pop hl ; pop hl with de'
sbc hl,de ; de' - de -> hl (cf was reset)
sbc c ; c' - c -> a
jr c,resubtract ; if cde' < cde then swap cde with cde' and redo subtract
jp m,finalize ; if man2 a bit 7 is set then finalize bahl to return float bcde
normalize: ; normalize bahl to return inexact result bcde with sign' l' bit 7
ld c,a ; save a -> c
or h ;
or l ;
jr z,fzero ; if ahl = 0 then return zero (underflow, cf reset)
ld a,c ; restore c -> a
or a ;
1$: jp m,finalize ; 10 ; loop while a bit 7 is clear
add hl,hl ; 11 ;
adc a ; 4 ; ahl << 1 -> ahl
djnz 1$ ; 13(38); until --b = 0 ; finalize bahl to return float bcde
; FALL THROUGH
;-------------------------------------------------------------------------------
;
; FLOATING POINT CONSTANT ZERO
;
; fzero: 0.0 -> bcde
; cf reset
; a,b,c,d,e modified
;
;-------------------------------------------------------------------------------
fzero: xor a ; 0 -> a and reset cf
ld b,a ;
ld c,a ;
ld d,a ; 0 -> bcde
ld e,a ;
ret ; return float bcde (cf reset, v reset, z set)
;-------------------------------------------------------------------------------
;
; FLOATING POINT MULTIPLICATION
;
; fmul: bcde * bcde' -> bcde
; cf set on overflow
; a,b,c,d,e,h,l,a',b',c',d',e',h',l' modified
;
;-------------------------------------------------------------------------------
fmul: EXPA ; exponent -> a
jr z,fzero ; if bcde is zero then return zero (cf reset)
sub bias ; subtract exponent bias
ld h,a ; exponent - bias -> h
exx ; activate bcdehl'
EXPA ; exponent' -> a
jr z,fzero ; if bcde' is zero then return zero (cf reset)
sub bias ; subtract exponent bias
exx ; activate bcdehl
; add unbiased exponents to produce result exponent
add h ; (exponent' - bias) + (exponent - bias) -> a
jp pe,outofrange ; if out of range then return zero (underflow, cf reset) or overflow (cf set)
; save biased result exponent and sign
add bias ; bias the result exponent
jr z,fzero ; if result exponent is zero then return zero (underflow, cf reset)
ex af,af' ; save result biased exponent to a'
ld a,b ; b -> a with sign bit 7
set 7,c ; set bit 7 of man2 c
exx ; activate bcdehl'
xor b ; sign xor sign' -> a with result sign bit 7
ld l,a ; save result sign to l' bit 7
set 7,c ; set bit 7 of man2' c'
exx ; activate bcdehl
; multiply mantissas cde * cde' -> ahl.cde'
xor a ; 0 -> a and reset cf
ld l,a ;
ld h,a ; 0 -> hl
ld b,24 ; 24 -> b loop counter
1$: rra ; 4 ; loop
rr h ; 8 ;
rr l ; 8 ;
exx ; 4 ;
rr c ; 8 ;
rr d ; 8 ;
rr e ; 8 ; cf.ahl.cde' >> 1 -> ahl.cde'.cf
exx ; 4 ;
jr nc,2$ ; 12/7 ; if carry then
add hl,de ; 11 ;
adc c ; 4 ; ahl + cde -> ahl
2$: djnz 1$ ; 13(87); until --b = 0
; restore result exponent b and normalize bahl to return float bcde
ex af,af' ; restore result exponent a', save a and cf
ld b,a ; a' -> b with result exponent
ex af,af' ; restore a and cf
jp shiftoncarry ; normalize bahl to return float bcde
; out of range, return zero (underflow, cf reset) or overflow (cf set)
outofrange: add a ; carry if bit 7 set
jr nc,fzero ; if incorrect positive then return zero (underflow, cf reset)
ret ; return with overflow error (cf set)
;-------------------------------------------------------------------------------
;
; FLOATING POINT DIVISION
;
; fdivx: bcde / bcde' -> bcde
; fdivy: bcde' / bcde -> bcde
; cf set on overflow or when dividing by zero
; a,b,c,d,e,h,l,a',b',c',d',e',h',l' modified
;
;-------------------------------------------------------------------------------
fdivx: exx ;
fdivy: EXPA ; exponent -> a
scf ; set cf
ret z ; if divisor bcde is zero then division by zero error (cf set)
sub bias ; subtract exponent bias
ld h,a ; exponent - bias -> h'
exx ; activate bcdehl'
EXPA ; exponent' -> a
jr z,fzero ; if dividend bcde' is zero then return zero (cf reset)
sub bias ; subtract exponent bias
exx ; activate bcdehl
; subtract unbiased exponents to produce result exponent
sub h ; (exponent' - bias) - (exponent - bias) -> a
jp pe,outofrange ; if out of range then return zero (underflow, cf reset) or overflow (cf set)
; save biased result exponent to b' and result sign to l'
add bias ; bias the result exponent
jr z,fzero ; if result exponent is zero then return zero (underflow, cf reset)
ex af,af' ; save a with result exponent
ld a,b ; b -> a' with sign bit 7
exx ; activate bcdehl'
xor b ; sign xor sign' -> a' with result sign bit 7
ex af,af' ; restore a with result exponent, save a' with result sign
ld b,a ; a -> b' with result exponent
set 7,c ; set bit 7 of man2' c'
exx ; activate bcdehl
set 7,c ; set bit 7 of man2 c
; divide mantissas cde' / cde -> chl'
xor a ;
ld h,a ;
ld l,a ;
sbc hl,de ;
ex de,hl ;
sbc c ;
ld c,a ; -cde -> cde we use -cde to add in the loop below
exx ;
ld a,c ; c' -> a
push de ; save de'
exx ;
pop hl ; restore de' -> hl
ld b,24 ; 24 -> b loop counter
1$: add hl,de ; 11 ; loop
adc c ; 4 ; ahl + -cde -> cf.ahl
jr c,2$ ; 12/7 ; if no carry then
sbc hl,de ; 15 ;
sbc c ; 4 ; ahl - -cde -> ahl undo add, no carry
2$: exx ; 4 ;
adc hl,hl ; 15 ;
rl c ; 8 ; chl'.cf << 1 -> chl' shift in carry
exx ; 4 ;
add hl,hl ; 11 ;
rla ; 4 ; ahl << 1 -> ahl where rla carry means cf.ahl > cde
jr c,5$ ; 7/12 ; if carry then force add, shift carry, and loop again
djnz 1$ ; 13(107); until --b = 0
3$: ; normalize result mantissa chl'
exx ; activate bcdehl'
bit 7,c ; test c' bit 7
jr nz,4$ ; if zero then
dec b ; decrement result exponent b'
exx ;
jp z,fzero ; if result exponent is zero then return zero (underflow, cf reset)
inc b ; 1 -> b loop counter
jr nc,1$ ; loop without rla carry for final mantissa bit
jr 2$ ; loop with rla carry for final mantissa bit
4$: ; finalize bchl' to return float bcde with sign a' bit 7
exx ; activate bcdehl
ex af,af' ; restore a' with result sign
ld l,a ; a -> l with result sign bit 7
exx ; now make bchl' with sign l the active bchl with sign l'
ld a,c ; c -> a
jp finalize ; finalize bahl to return float bcde
5$: ; when cf.ahl > cde then add -cde and shift 1 into chl'
add hl,de ; 11 ;
adc c ; 4 ; ahl + -cde -> ahl
scf ; 4 ; 1 -> cf
djnz 2$ ; 13 ; until --b = 0
jr 3$ ; normalize result mantissa chl'
;-------------------------------------------------------------------------------
;
; CONVERT FLOAT TO INTEGER
;
; ftoi: int(bcde) -> signed integer bcde truncated towards zero
; cf set when out of range (bcde unchanged)
; a,b,c,d,e,h,l modified
;
;-------------------------------------------------------------------------------
ftoi: EXPA ; exponent -> a
jp z,fzero ; if bcde is zero then return zero (cf reset)
sub bias ; subtract exponent bias
jp c,fzero ; if exponent is negative then return zero (cf reset)
ld l,b ; save sign bit 7 to l
ld h,a ; unbiased exponent -> h
ld a,30 ;
sub h ; 30 - unbiased exponent -> a
ret c ; if carry then out of range (cf set)
set 7,c ; set bit 7 of man2 c
; shift mantissa to remove fractional part
inc a ;
ld b,a ; loop counter = 31 - unbiased exponent
xor a ; 0 -> a low order byte of the integer result
1$: srl c ; loop
rr d ;
rr e ;
rra ; cdea >> 1 -> cdea
djnz 1$ ; until --b = 0
2$: ; rearrange cdea to the result integer bcde
ld b,c ;
ld c,d ;
ld d,e ;
ld e,a ;
3$: ; return positive integer bcde if float is positive
rl l ; test l bit 7
ret nc ; return positive bcde (cf reset)
inegate: ; negate integer bcde
xor a ;
sub e ;
ld e,a ;
ld a,0 ;
sbc d ;
ld d,a ;
ld a,0 ;
sbc c ;
ld c,a ;
sbc a ;
sub b ;
ld b,a ;
or a ; reset cf
ret ; return negated integer bcde (cf reset)
;-------------------------------------------------------------------------------
;
; CONVERT INTEGER TO FLOAT
;
; itof: float(bcde) -> bcde
; no errors (cf reset)
; a,b,c,d,e,h,l,l' modified
;
;-------------------------------------------------------------------------------
itof: ld a,b ;
or c ;
or d ;
or e ;
ret z ; return if bcde is zero (cf reset)
; save sign to l' and negate integer bcde when negative
ld a,b ;
exx ;
ld l,a ; save sign b bit 7 to l'
exx ;
or a ; if sign is negative then
call m,inegate ; negate integer bcde
; rearrange integer bcde to mantissa ahl.e
ld a,b ;
ld h,c ;
ld l,d ;
; set result exponent b and normalize nonzero mantissa ahl.e
ld b,bias+31 ; set exponent b
or a ;
jp m,finalize ; if a bit 7 not set then
1$: dec b ; 4 ; loop, decrement exponent b (cannot underflow)
sla e ; 8 ;
adc hl,hl ; 15 ;
adc a ; 4 ; ahl.e << 1 -> ahl.e
jp p,1$ ; 10(51); until a bit 7 set
jp finalize ; finalize bahl with sign l' bit 7
;-------------------------------------------------------------------------------
;
; TRUNCATION
;
; ftrunc: trunc(bcde) -> bcde round towards zero
; no errors (cf reset)
; a,b,c,d,e,h,l modified
;
; trunc(-bcde) = -trunc(bcde)
; frac(bcde) = bcde - trunc(bcde)
;
;-------------------------------------------------------------------------------
ftrunc: push bc ;
push de ; save bcde
call ftoi ; convert to integer if possible
jr c,1$ ; if converted then
pop af ;
pop af ; discard old bcde
jp itof ; convert back to float and return
1$: pop de ;
pop bc ; restore old bcde
or a ; reset cf
ret ; return
;-------------------------------------------------------------------------------
;
; FLOORING
;
; ffloor: floor(bcde) -> bcde round towards -infinity
; cf set on overflow
; a,b,c,d,e,h,l,a',b',c',d',e',h',l' modified
;
; floor(|bcde|) = trunc(|bcde|)
; ceil(bcde) = -floor(-bcde)
;
;-------------------------------------------------------------------------------
ffloor: push bc ;
push de ; save bcde
call ftrunc ; trunc(bcde) -> bcde
exx ;
pop de ;
pop bc ; pop old bcde -> bcde'
exx ;
push bc ; save trunc(bcde)
push de ;
call fsuby ; bcde' - trunc(bcde) -> frac(bcde)
ISPOSZ ; test if frac(bcde) >= 0
pop de ;
pop bc ; restore trunc(bcde)
ret z ; if frac(bcde) >= 0 then return trunc(bcde)
exx ;
ld bc,bias + 0x100 << 7 ;
ld de,0x0000 ; -1.0e0 -> bcde'
jp fadd ; return trunc(bcde) - 1.0e0
;-------------------------------------------------------------------------------
;
; ROUNDING
;
; fround: round(bcde) -> bcde round to nearest, ties to away
; cf set on overflow
; a,b,c,d,e,h,l,a',b',c',d',e',h',l' modified
;
; round(bcde) = floor(bcde + 0.5e0)
;
;-------------------------------------------------------------------------------
fround: exx ;
ld bc,bias - 1 << 7 ;
ld de,0x0000 ; 0.5e0 -> bcde'
call fadd ; bcde + 0.5e0 -> bcde (cannot overflow)
jr ffloor ; return floor(bcde)
;-------------------------------------------------------------------------------
;
; FLOATING POINT MULTIPLICATION BY POWER OF 10
;
; fpow10: 10**a * bcde -> bcde
; cf set on overflow
; a,b,c,d,e,h,l,a',b',c',d',e',h',l' modified
;
; four computational methods compared, minimum and mean number of
; bits of accuracy (relative error) in 10 million random samples
;
; method min bits mean bits
; iterative multiplication by 10: 20.2439 23.4988
; iterative multiplication by 10**4: 21.3502 24.4916
; iterative multiplication by 10**5: 21.3243 24.5500
; iterative multiplication by 10**6: 21.3297 24.5937
; iterative multiplication by 10**7: 21.3253 24.5619
; iterative multiplication by 10**8: 21.3004 24.5260
; iterative multiplication by 10**10: 21.0809 24.4483
; iterative multiplication by 10**16: 20.7694 24.0836
; table lookup with 4 powers of 10: 21.4136 24.6670
; table lookup with 8 powers of 10: 21.9131 25.2981
; table lookup with 9 powers of 10: 21.8907 25.4628
; table lookup with 10 powers of 10: 22.0177 25.5388
; * table lookup with 12 powers of 10: 22.0482 25.7529
; table lookup with 16 powers of 10: 22.0004 25.4073
; exponentiation by squaring from bottom: 22.0004 25.3867
; exponentiation by squaring from top: 22.0023 25.3830
; table lookup with 38 powers of 10: exact exact
;
;-------------------------------------------------------------------------------
.if 1 ; table lookup with 12 powers of 10 for -128 <= a <= 127
fpow10: or a ; test a and reset cf
jp p,mulpow10 ; if a > 0 then multiply bcde by 10**a
neg ; -a -> a
cp 39 ;
jr c,1$ ; if a >= 39 then
sub 38 ; a - 38 -> a
call 1$ ; bcde / 10**a -> bcde
ld a,38 ; 38 -> a
1$: push bc ;
push de ;
call pow10 ; 10**a -> bcde
exx ; bcde -> bcde'
pop de ;
pop bc ; restore bcde
jp c,fzero ; if 10**a overflows then return zero (underflow, cf reset)
jp fdivx ; bcde / 10**a -> bcde
pow10: ; return float bcde = 10**a for 0 < a <= 38
ld bc,bias << 7 ;
ld de,0x0000 ; 1.0e0 -> bcde
mulpow10: ; return float bcde * 10**a for 0 <= a <= 127
ld l,11 ; 11 -> l use power 10**12 from table
jr 2$ ; jump into loop to check and update counters
1$: push hl ; loop, save counters
ld a,l ; l -> a ranging 0 to 11
exx ;
ld hl,powers ; powers -> hl' table indexed by a from 0
add a ;
add a ;
ld c,a ;
ld b,0 ;
add hl,bc ; hl' + 4 * a -> hl'
ld c,(hl) ;
inc hl ;
ld b,(hl) ;
inc hl ;
ld e,(hl) ;
inc hl ;
ld d,(hl) ; [hl'] -> bcde' with 10**2**(a-1)
call fmul ; 10**(2**(a-1)) * bcde -> bcde
pop hl ; restore hl
ret c ; if overflow then return error (cf set)
ld a,h ; h -> a
2$: sub 12 ;
ld h,a ; a - 12 -> h
jp p,1$ ; if h >= 0 then continue loop
add 11 ; a + 11 -> a, cf = 0 if a < 0
ret m ; if a < 0 then return (cf reset)
ld l,a ; a -> l ranging 0 to 10
jr 1$ ; repeat
powers: ; table of powers 10**i for i = 1 to 12
.dw 0x4120,0x0000 ; = 10**1
.dw 0x42c8,0x0000 ; = 10**2
.dw 0x447a,0x0000 ; = 10**3
.dw 0x461c,0x4000 ; = 10**4
.dw 0x47c3,0x5000 ; = 10**5
.dw 0x4974,0x2400 ; = 10**6
.dw 0x4b18,0x9680 ; = 10**7
.dw 0x4cbe,0xbc20 ; = 10**8
.dw 0x4e6e,0x6b28 ; = 10**9
.dw 0x5015,0x02f9 ; = 10**10
.dw 0x51ba,0x43b7 ; = 10**11
.dw 0x5368,0xd4a5 ; = 10**12
.else
.if 1 ; exponentiation by squaring from top with table lookup for -128 <= a <= 63
fpow10: or a ; test a and reset cf
jp p,mulpow10 ; if a > 0 then multiply bcde by 10**a
neg ; -a -> a
cp 39 ;
jr c,1$ ; if a >= 39 then
sub 38 ; a - 38 -> a
call 1$ ; bcde / 10**a -> bcde
ld a,38 ; 38 -> a
1$: push bc ;
push de ;
call pow10 ; 10**a -> bcde
exx ; bcde -> bcde'
pop de ;
pop bc ; restore bcde
jp c,fzero ; if 10**a overflows then return zero (underflow, cf reset)
jp fdivx ; bcde / 10**a -> bcde
pow10: ; return float 10**a for 0 < a <= 38
ld bc,bias << 7 ;
ld de,0x0000 ; 1.0e0 -> bcde
mulpow10: ; return float bcde * 10**a for 0 <= a <= 63
add a ;
ret c ; if a >= 128 return error (cf set)
add a ;
ret c ; if a >= 64 return error (cf set)
ld h,a ; a << 2 -> h to prep shifting h left 6 times
ld l,7 ; 7 -> l loop counter + 1
1$: dec l ; for l = 6 to 1 step -1
ret z ; if l = 0 then break (cf reset)
sla h ; h << 1 -> cf,h
jr nc,1$ ; if carry then
push hl ; save hl
ld a,l ;
exx ;
ld hl,powers - 4 ; powers - 4 -> hl' table indexed by a from 1
add a ;
add a ;
ld c,a ;
ld b,0 ;
add hl,bc ; hl' + 4 * a -> hl'
ld c,(hl) ;
inc hl ;
ld b,(hl) ;
inc hl ;
ld e,(hl) ;
inc hl ;
ld d,(hl) ; [hl'] -> bcde' with 10**2**(a-1)
call fmul ; 10**(2**(a-1)) * bcde -> bcde
pop hl ; restore hl
ret c ; if overflow then return error (cf set)
jr 1$ ; next
powers: ; table of powers 10**2**i for i = 0 to 5
.dw 0x4120,0x0000 ; = 10**1
.dw 0x42c8,0x0000 ; = 10**2
.dw 0x461c,0x4000 ; = 10**4
.dw 0x4cbe,0xbc20 ; = 10**8
.dw 0x5a0e,0x1bca ; = 10**16
.dw 0x749d,0xc5ae ; = 10**32
.else ; direct table lookup with for -128 <= a <= 76
fpow10: or a ; test a and reset cf
ret z ; if a = 0 then return float bcde unchanged (cf reset)
exx ;
jp p,2$ ; if a > 0 then multiply by 10th power
neg ; -a -> a
cp 39 ;
jr c,1$ ; if a >= 39 then
sub 38 ; a - 38 -> a
call pow10 ; 10**a -> bcde
jp c,fzero ; if 10**a overflows then return zero (underflow, cf reset)
call fdivy ; bcde' / 10**a -> bcde
exx ; bcde -> bcde'
ld a,38 ; 38 -> a
1$: call pow10 ; 10**a -> bcde'
jp c,fzero ; if 10**a overflows then return zero (underflow, cf reset)
jp fdivy ; bcde / 10**a -> bcde
2$: cp 39 ;
jr c,3$ ; if a >= 39 then
sub 38 ; a - 38 -> a
call pow10 ; 10**a -> bcde
ret c ; if 10**a overflows then return error (cf set)
call fmul ; bcde' / 10**a -> bcde
ret c ; if overflow then return overflow (cf set)
exx ; bcde -> bcde'
ld a,38 ; 38 -> a
3$: call pow10 ; 10**a -> bcde'
ret c ; if overflow then return overflow (cf set)
jp fmul ; 10*a * bcde -> bcde
pow10: ; return float 10**a for 0 < a <= 38
ld hl,powers - 4 ; power - 4 -> hl minus 4 since indexed from 1
add a ;
add a ;
ld c,a ;
ld b,0 ;
add hl,bc ; hl + 4 * a -> hl
ld c,(hl) ;
inc hl ;
ld b,(hl) ;
inc hl ;
ld e,(hl) ;
inc hl ;
ld d,(hl) ; [hl] -> bcde
ret ; return (cf reset)
powers: ; table of powers 10**i for i = 1 to 38
.dw 0x4120,0x0000 ; = 10**1
.dw 0x42c8,0x0000 ; = 10**2
.dw 0x447a,0x0000 ; = 10**3
.dw 0x461c,0x4000 ; = 10**4
.dw 0x47c3,0x5000 ; = 10**5
.dw 0x4974,0x2400 ; = 10**6
.dw 0x4b18,0x9680 ; = 10**7
.dw 0x4cbe,0xbc20 ; = 10**8
.dw 0x4e6e,0x6b28 ; = 10**9
.dw 0x5015,0x02f9 ; = 10**10
.dw 0x51ba,0x43b7 ; = 10**11
.dw 0x5368,0xd4a5 ; = 10**12
.dw 0x5511,0x84e7 ; = 10**13
.dw 0x56b5,0xe621 ; = 10**14
.dw 0x5863,0x5fa9 ; = 10**15
.dw 0x5a0e,0x1bca ; = 10**16
.dw 0x5bb1,0xa2bc ; = 10**17
.dw 0x5d5e,0x0b6b ; = 10**18
.dw 0x5f0a,0xc723 ; = 10**19
.dw 0x60ad,0x78ec ; = 10**20
.dw 0x6258,0xd727 ; = 10**21
.dw 0x6407,0x8678 ; = 10**22
.dw 0x65a9,0x6816 ; = 10**23
.dw 0x6753,0xc21c ; = 10**24
.dw 0x6904,0x5951 ; = 10**25
.dw 0x6aa5,0x6fa6 ; = 10**26
.dw 0x6c4e,0xcb8f ; = 10**27
.dw 0x6e01,0x3f39 ; = 10**28
.dw 0x6fa1,0x8f08 ; = 10**29
.dw 0x7149,0xf2ca ; = 10**30
.dw 0x72fc,0x6f7c ; = 10**31
.dw 0x749d,0xc5ae ; = 10**32
.dw 0x7645,0x3719 ; = 10**33
.dw 0x77f6,0x84df ; = 10**34
.dw 0x799a,0x130c ; = 10**35
.dw 0x7b40,0x97ce ; = 10**36
.dw 0x7cf0,0xbdc2 ; = 10**37
.dw 0x7e96,0x7699 ; = 10**38
.endif
.endif
;-------------------------------------------------------------------------------
;
; CONVERT STRING TO FLOAT
;
; atof: [hl..hl+a-1] -> bcde
; cf set on parsing error and hl points after the char
; a,b,c,d,e,h,l,a',b',c',d',e',h',l' modified
; b remaining string length counter
; c sign (bit 7), dp flag (bit 6), digit count (biased)
; d decimal exponent sign flag 0 or 1
; e (signed) decimal exponent
; hl string pointer
; bc' unscaled float result
; de' unscaled float result & mantissa accumulator
; hl' mantissa accumulator
;
; EXPEMPTY = 1 allows empty exponent values with no digits after E
;
;-------------------------------------------------------------------------------
atof: ld b,a ; a -> b string length
cp 1 ;
ret c ; if b = 0 then return error (cf set)
cp 32 ;
ccf ;
ret c ; if b > 31 then return error (cf set)
; initialize
ld c,32 ; 32 -> c sign (bit 7), no dp (bit 6), biased digit counter (bits 0 to 5)