/
fp-math.asm
1352 lines (1328 loc) · 30.4 KB
/
fp-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
; FP Math TODO:
; 1. Addition and subtraction
; 2. Multiplication and division
; 3. Miscellaneous math operations:
; - Roots/powers
; - Logarithms
; - Trigonometry
; - etc.
; 4. Conversion to and from IEEE 754 floats/doubles
;; itofp [Decimal Floating Point]
;; Converts a 32-bit unsigned integer into a floating-point
;; binary coded decimal format and stores it to the buffer at HL.
;; Inputs:
;; ACIX: Unsigned integer
;; HL: Pointer to 9-byte destination buffer
;; Notes:
;; The result is in the following format:
;;
;; - 1 byte flags:
;; - 7: Sign bit
;; - 4-6: Reserved for kernel
;; - 0-3: Available for program use
;; - 1 byte signed exponent, normalized to 0x80 instead of 0
;; - 7 byte mantissa, BCD encoded with two digits per byte
itofp:
; Modified version of itostr
push hl
push ix
push bc
push af
push de
push iy
; Move HL to IY for convenience
push hl \ pop iy
; Clear the flags
ld (iy), 0
ld b, 0 ; Our digit counter
.loop:
; Check if ACIX is zero
push bc
; If a is not zero
or a
jp nz, .notZero
; If c is not zero
ld b, a
ld a, c
or a
ld a, b
jp nz, .notZero
; If ixh is not zero
ld b, a
ld a, ixh
or a
ld a, b
jp nz, .notZero
; If ixl is not zero
ld b, a
ld a, ixl
or a
ld a, b
.notZero:
pop bc
; If ACIX is down to zero, exit loop
jr z, _
; Divide ACIX by 10
push iy
ld iyh, b
ld de, 10
call div32By16
ld b, iyh
pop iy
push hl ; Push the remainder to the stack
inc b ; Increment our digit counter
jr .loop
_:
; Write a zero if ACIX is zero
ld a, b
or a
jr nz, .writeExp
ld (iy), 0
inc iy
.writeExp:
ld a, b
add a, 0x7f
ld (iy+1), a
.writeMantissa:
ld a, b
or a
jr z, _ ; If our digit counter is zero, exit loop
pop de ; Pop our digit from the stack
dec b ; Decrement our digit counter
ld a, b
or a
jr z, .lastDigit ; If we have an odd number of digits, don't pop
pop hl ; Pop our digit from the stack
dec b ; Decrement our digit counter
jr .writeDigits
.lastDigit:
ld hl, 0 ; Load a zero instead if there are no more digits
.writeDigits:
; Pack the two digits
ld a, e
rrca
rrca
rrca
rrca
or l
; Write the digits
ld (iy+2), a
inc iy
jr .writeMantissa
_:
pop iy
pop de
pop af
pop bc
pop ix
pop hl
ret
;; strtofp [Decimal Floating Point]
;; Converts an ASCII-encoded signed decimal into a floating-point
;; binary coded decimal format and stores it to the buffer at HL.
;; Inputs:
;; IX: Pointer to string
;; HL: Pointer to 9-byte destination buffer
;; Outputs:
;; Z: Set on success, reset on error
;; Notes:
;; See `itofp` for the result format.
;;
;; Only the first 14 significant digits are converted. The rest are truncated
;; but still used for exponent calculation.
;;
;; In case of error, the destination buffer's contents are undefined.
;;
;; The destination buffer must be zeroed before calling strtofp.
strtofp:
push ix
push hl
push bc
push de
push iy
push af
; Store HL into IY for convenience
push hl \ pop iy
; Check for a negative sign at the beginning
ld a, (ix)
cp '-'
jr nz, _
ld (hl), 0x80
inc ix
_:
inc hl
inc hl
ld b, 14 ; Remaining digits counter
ld de, 0x0080 ; Parser flags and place value counter
.loop:
; Check if we are at the end of the buffer
xor a
cp b
jr z, .loopEnd
; Check if we've reached a null terminator
ld a, (ix)
or a
jr z, .loopEnd
cp '.'
jr nz, _
; Make sure we haven't already seen a decimal point
bit 0, d
jr nz, .error
set 0, d
jr .loopCont
_:
; Error on non-numeric characters
cp '9' + 1
jr nc, .error
sub '0'
jr c, .error
jr nz, _
; Handle zero
bit 1, d
jr z, .loopCont ; Skip leading zero
_:
set 1, d ; Finished with all leading zeroes
rld ; Shift the digit into the current BCD byte
bit 0, b ; Only increment HL every other digit
jr z, .loopIter
inc hl
.loopIter:
; Adjust place value for digits before decimal
ld a, d
cp 0x02
jr nz, _
inc e
_:
inc ix
djnz .loop
jr .loopEnd
.loopCont:
; Adjust place value for leading zeroes after decimal
ld a, d
cp 0x01
jr nz, _
dec e
_:
inc ix
jr .loop
.loopEnd:
; Shift the last digit if there were an odd number
bit 0, b
jr z, _
xor a
rld
_:
; Check if there may be more place values that need counted
ld a, b
or a
jr nz, .remainderLoopEnd
bit 0, d
jr nz, .remainderLoopEnd
.remainderLoop:
; Count remaining place values
ld a, (ix)
or a
jr z, .remainderLoopEnd
cp '.'
jr z, .remainderLoopEnd
cp '9' + 1
jr nc, .error
cp '0'
jr c, .error
inc e
inc ix
jr .remainderLoop
.remainderLoopEnd:
; Correct for one's place value
ld a, 0x80
cp e
jr nc, _
dec e
_:
; Save the place value counter into the exponent byte
ld (iy + 1), e
pop af
cp a ; Set Z flag
jr .exit
.error:
pop af
; Save A
ld b, a
or 1 ; Reset Z flag
; Restore A
ld a, b
.exit:
pop iy
pop de
pop bc
pop hl
pop ix
ret
;; fptostr [Decimal Floating Point]
;; Converts a floating point number into an ASCII-encoded decimal string.
;; Inputs:
;; IX: Pointer to floating point number
;; HL: Pointer to destination buffer
;; A: Flags / digits
;; Notes:
;; The destination buffer must be at least 20 characters in length.
;;
;; The most significant nibble of A should be set to flags. `FP_STR_*`
;; macros in kernel.inc can be ORed and stored in A for convenience.
;;
;; The least significant nibble of A is the number of digits to display
;; after the decimal: 0-9 for that many digits, or 0xF for as many are
;; non-zero.
;;
;; Examples:
;;
;; All nonzero decimals in scientific notation, with decimal as ',':
;;
;; ld a, FP_STR_INV_PUNC | FP_DISP_SCIENTIFIC | 0xF
;;
;; 5 fixed digits grouped with ',':
;;
;; ld a, FP_GROUP_DIGITS | 5
; TODO:
; * Rounding last digit - buggy, currently abandoned
; * Never show exponent if significand is 0 - not started
.macro fptostrIter1(reg)
; Output the first digit in the byte pointed to by reg
ld a, (reg)
rrca
rrca
rrca
rrca
and 0x0F
add a, '0'
ld (hl), a
inc hl
.endmacro
.macro fptostrIter2(reg)
; Output the second digit in the byte pointed to by reg
ld a, (reg)
and 0x0F
add a, '0'
ld (hl), a
inc hl
.endmacro
.macro fptostrIter1Round(reg)
; Like fptostrIter1 but it rounds the last digit
ld a, (reg)
rrca
rrca
rrca
rrca
and 0x0F
dec b
jr nz, .fptostrIter1RoundSkip
ld c, a
ld a, (reg)
and 0x0F
cp 5
jr c, .fptostrIter1RoundSkip
ld a, c
inc a
.fptostrIter1RoundSkip:
inc b
add a, '0'
ld (hl), a
inc hl
.endmacro
.macro fptostrIter2Round(reg)
; Like fptostrIter2 but it rounds the last digit
ld a, (reg)
and 0x0F
dec b
jr nz, .fptostrIter2RoundSkip
ld c, a
inc reg
ld a, (reg)
dec reg
rrca
rrca
rrca
rrca
and 0x0F
cp 5
jr c, .fptostrIter2RoundSkip
ld a, c
inc a
.fptostrIter2RoundSkip:
inc b
add a, '0'
ld (hl), a
inc hl
.endmacro
.macro fptostrI18N(normal, alt)
; Output a different character depending on the bit 7 flag
ex (sp), hl
bit 7, h
ex (sp), hl
jr nz, $+6
ld (hl), normal
jr $+4
ld (hl), alt
inc hl
.endmacro
.macro fptostrInsertPVSep
; Output a place value separator depending on the bit 6 flag
pop af \ push af
bit 6, a
jr z, ++_
push de
ld d, b
dec d
ld e, 3
call div8By8
or a
jr nz, _
or d
jr z, _
pop de
fptostrI18N(',', '.')
jr ++_
_:
pop de
_:
.endmacro
fptostr:
push hl
push bc
push de
push af
; Check which mode to use
bit 4, a
jp nz, _
ld a, (ix + 1)
cp 0x8a
jp nc, _
cp 0x7d
jp c, _
; Normal mode
pop af \ push af
jr ++_
_:
; Scientific mode
pop af \ push af
set 4, a
res 5, a
_:
push af
; Negative sign
ld a, (ix)
and 0x80
jr z, _
ld (hl), '-'
inc hl
_:
; Calculate number of digits to display
pop af \ push af
bit 4, a
jr z, _
; Scientific notation
ld a, 1
jr .beginIPart
_:
; Normal mode
ld a, (ix + 1)
cp 0x80
jr c, _
sub 0x7F
jr .beginIPart
_:
; Negative exponent
ld (hl), '0'
inc hl
push ix \ pop de
inc de \ inc de
ld c, 10 ; Should be 9, but that causes it to skip the last digit
xor a
jp .doneWithIPart
.beginIPart:
ld b, a ; Number of pre-decimal digits
ld a, 10
sub b
ld c, a ; (Maximum) number of post-decimal digits
push ix \ pop de
inc de \ inc de
.iPartLoop:
fptostrIter1(de)
fptostrInsertPVSep
dec b
ld a, 1 ; Mark that this byte isn't complete yet
jr z, .doneWithIPart
fptostrIter2(de)
fptostrInsertPVSep
inc de
xor a ; Mark that this byte is complete
djnz .iPartLoop
.doneWithIPart:
; Check if we are using fixed point or not
push af
inc sp \ inc sp
pop af \ push af
dec sp \ dec sp
and 0x0F
jr z, ++_
cp 10
jr nc, +++_
cp c
jr nc, _
ld c, a
_:
xor a
or c
jr z, _
pop af
jr .skipFloating
_:
pop af
jr .end
_:
pop af
; Calculate how many fractional digits there are
push af
push hl
push de
; Escape early if there isn't room for fractional digits
xor a
or c
jr z, _
push ix \ pop hl
ld de, 6 ; Ignore last 2 bytes for display purposes
add hl, de
.countFractionalDigitsLoop:
ld a, (hl)
and 0x0F
jr nz, ++_
dec c
jr z, _
ld a, (hl)
and 0xF0
jr nz, ++_
dec hl
dec c
jr nz, .countFractionalDigitsLoop
_:
pop de
pop hl
pop af
jp .end
_:
pop de
pop hl
pop af
.skipFloating:
fptostrI18N('.', ',')
; Check if we need to add leading zeroes
push af
; Make sure we aren't in scientific notation mode
inc sp \ inc sp
pop af \ push af
dec sp \ dec sp
bit 4, a
jr nz, _
; Check the exponent
ld a, (ix + 1)
sub 0x80
jr nc, _
neg
dec a
jr z, _
ld b, a
.leadingZeroLoop:
ld (hl), '0'
inc hl
dec c
djnz .leadingZeroLoop
_:
pop af
ld b, c
dec a
jr z, .fPartLoopHalf
.fPartLoop:
fptostrIter1(de)
; fptostrIter1Round(de)
dec b
jr z, .end
.fPartLoopHalf:
fptostrIter2(de)
; fptostrIter2Round(de)
inc de
djnz .fPartLoop
.end:
; Check if need to include exponent or not
pop af
bit 4, a
jr z, .terminate
ld (hl), 'E'
inc hl
; Exponent
ld a, (ix + 1)
sub 0x80
jr nc, _
ld (hl), '-'
inc hl
neg
_:
ld d, a
ld e, 10
ld b, 0 ; Exponent digit counter
.calcExponentLoop:
call div8By8
add a, '0'
push af
inc b
ld a, d
or a
jr nz, .calcExponentLoop
.writeExponentLoop:
pop af
ld (hl), a
inc hl
djnz .writeExponentLoop
.terminate:
; Null terminator
ld (hl), 0
pop af
pop de
pop bc
pop hl
ret
.undefine fptostrIter1
.undefine fptostrIter2
.undefine fptostrIter1Round
.undefine fptostrIter2Round
.undefine fptostrI18N
.undefine fptostrInsertPVSep
;; fpLdConst [Decimal Floating Point]
;; Loads a floating point constant specified by A into HL.
;; Inputs:
;; A: Constant to load, use `FP_*` macros from kernel.inc
;; HL: Pointer to destination buffer
fpLdConst:
push af
push bc
push de
push hl
ex de, hl
ld hl, .lookupTable
ld bc, 9
.macro fpLdConstIter
add hl, bc
dec a
jr z, _
.endmacro
or a
jr z, _
fpLdConstIter
fpLdConstIter
fpLdConstIter
fpLdConstIter
fpLdConstIter
fpLdConstIter
fpLdConstIter
fpLdConstIter
fpLdConstIter
.undefine fpLdConstIter
; None matched - return unchanged
jr .end
_:
ldir
.end:
pop hl
pop de
pop bc
pop af
ret
.lookupTable:
; 0
.db 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
; 1
.db 0x00, 0x80, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
; pi
.db 0x00, 0x80, 0x31, 0x41, 0x59, 0x26, 0x53, 0x58, 0x98
; pi/2
.db 0x00, 0x80, 0x15, 0x70, 0x79, 0x63, 0x26, 0x79, 0x49
; pi/4
.db 0x00, 0x7F, 0x78, 0x53, 0x98, 0x16, 0x33, 0x97, 0x45
; pi/180
.db 0x00, 0x7E, 0x17, 0x45, 0x32, 0x92, 0x51, 0x99, 0x43
; 180/pi
.db 0x00, 0x81, 0x57, 0x29, 0x57, 0x79, 0x51, 0x30, 0x82
; e
.db 0x00, 0x80, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90
; log(e)
.db 0x00, 0x7F, 0x43, 0x42, 0x94, 0x48, 0x19, 0x03, 0x25
; ln(10)
.db 0x00, 0x80, 0x23, 0x02, 0x58, 0x50, 0x92, 0x99, 0x40
; (Internal) Normalize the floating point number at HL.
fpNormalize:
push af
push bc
push de
inc hl \ inc hl
push hl
xor a
ld c, a
.macro fpNormalizeCountLeading
; Find the first nonzero byte
or (hl)
jr nz, _
inc hl
inc c
.endmacro
fpNormalizeCountLeading
fpNormalizeCountLeading
fpNormalizeCountLeading
fpNormalizeCountLeading
fpNormalizeCountLeading
fpNormalizeCountLeading
fpNormalizeCountLeading
.undefine fpNormalizeCountLeading
; It's all zero, so clean it up and exit
pop hl
dec hl
ld (hl), 0x80
dec hl
ld (hl), 0
jr .end
_:
; HL points to the first nonzero byte
; Copy from HL to the beginning of the significand
pop de \ push de
push bc
ld a, 7
sub c
ld c, a
ld b, 0
ldir
pop bc
; Zero out the trailing bytes left over from copying
ld b, c
sla c
jr z, _
dec hl
xor a
.zeroLoop:
ld (hl), a
dec hl
djnz .zeroLoop
_:
; Shift out the last leading zero if necessary
pop hl \ push hl
ld a, (hl)
and 0xF0
jr nz, _
inc c
ld de, 6
add hl, de
.macro fpNormalizeShiftLeft
rld
dec hl
.endmacro
fpNormalizeShiftLeft
fpNormalizeShiftLeft
fpNormalizeShiftLeft
fpNormalizeShiftLeft
fpNormalizeShiftLeft
fpNormalizeShiftLeft
fpNormalizeShiftLeft
.undefine fpNormalizeShiftLeft
_:
; Fix exponent
pop hl
dec hl
ld a, (hl)
sub c
ld (hl), a
dec hl
.end:
pop de
pop bc
pop af
ret
;; fpAbs [Decimal Floating Point]
;; Takes the absolute value of the floating point number at IX.
;; Inputs:
;; IX: Pointer to operand
;; Outputs:
;; IX: Pointer to result
fpAbs:
res 7, (ix)
ret
;; fpNeg [Decimal Floating Point]
;; Negates the floating point number at IX.
;; Inputs:
;; IX: Pointer to operand
;; Outputs:
;; IX: Pointer to result
fpNeg:
push af
; Make sure operand is not zero
ld a, (ix + 2)
or a
jr z, .end
; Invert the sign bit
ld a, (ix)
xor 0x80
ld (ix), a
.end:
pop af
ret
;; fpSub [Decimal Floating Point]
;; Subtracts the two floating point numbers.
;; Inputs:
;; IX: Pointer to operand 1 (minuend)
;; IY: Pointer to operand 2 (subtrahend)
;; HL: Pointer to destination buffer
fpSub:
push af
; Make sure operand is not zero
ld a, (iy + 2)
or a
jr z, .end
; Invert the sign bit
ld a, (iy)
xor 0x80
ld (iy), a
.end:
pop af
; Fall through to fpAdd
;; fpAdd [Decimal Floating Point]
;; Adds the two floating point numbers.
;; Inputs:
;; IX, IY: Pointers to operands
;; HL: Pointer to destination buffer
;; Notes:
;; May destroy one or both operands.
fpAdd:
push ix
push iy
push hl
push af
push bc
; Make sure that IY has the smaller exponent
ld a, (ix + 1)
cp (iy + 1)
jr nc, _
; IY is larger, so swap with IX
push ix \ push iy \ pop ix \ pop iy
_:
; Set the result's exponent to IX's for now
inc hl
ld a, (ix + 1)
ld (hl), a
dec hl
; Align smaller operand's radix point with the larger one's
sub (iy + 1)
jr z, .skipShifting
cp 2
jr c, .shiftLast
push hl
push de
push af
push iy \ pop hl
push iy \ pop de
; Point DE to the end of the buffer
ld bc, 8
add hl, bc
ex de, hl
; Point HL to the byte that will be moved to the end
srl a ; Number of bytes to shift right
cp 6
jr c, _
ld a, 6
_:
neg
add a, 8
ld c, a
add hl, bc
dec bc
lddr
; Replace shifted digits with leading zeroes
pop af
ld b, 0
ld c, a
srl c
add hl, bc
ld b, c
ld c, 0
.zeroLoop:
ld (hl), c
dec hl
djnz .zeroLoop
pop de
pop hl
.shiftLast:
; Shift last digit if necessary
and 0x01
jr z, _
push hl
; Swap with HL to make shifting easier
push iy \ pop hl
inc hl
xor a
.macro fpAddShiftRight
inc hl
rrd
.endmacro
fpAddShiftRight
fpAddShiftRight
fpAddShiftRight
fpAddShiftRight
fpAddShiftRight
fpAddShiftRight
fpAddShiftRight
.undefine fpAddShiftRight
pop hl
.skipShifting:
; Invert negative operands using 10's complement
.macro fpAddTensComplIter(r)
ld a, 0x99
sub (r)
ld (r), a
.endmacro
bit 7, (ix)
jr z, _ ; Not negative
fpAddTensComplIter(ix + 2)
fpAddTensComplIter(ix + 3)
fpAddTensComplIter(ix + 4)
fpAddTensComplIter(ix + 5)
fpAddTensComplIter(ix + 6)
fpAddTensComplIter(ix + 7)
fpAddTensComplIter(ix + 8)
_:
bit 7, (iy)
jr z, _ ; Not negative
fpAddTensComplIter(iy + 2)
fpAddTensComplIter(iy + 3)
fpAddTensComplIter(iy + 4)
fpAddTensComplIter(iy + 5)
fpAddTensComplIter(iy + 6)
fpAddTensComplIter(iy + 7)
fpAddTensComplIter(iy + 8)
.undefine fpAddTensComplIter
_:
; Start at the end of the buffer
ld bc, 8
add hl, bc
; Clear the carry flag if operands have the same sign, else set it
ld a, (ix)
and 0x80
ld b, a
ld a, (iy)
and 0x80
cp b
scf
; jr nz, _ ; TODO: find a way to only add 1 when it will overflow
ccf
_:
.macro fpAddSumIter(x, y)
ld a, (x)
adc a, (y)
daa ; Adjust the addition for BCD
ld (hl), a
dec hl
.endmacro
fpAddSumIter(ix + 8, iy + 8)
fpAddSumIter(ix + 7, iy + 7)
fpAddSumIter(ix + 6, iy + 6)
fpAddSumIter(ix + 5, iy + 5)
fpAddSumIter(ix + 4, iy + 4)
fpAddSumIter(ix + 3, iy + 3)
fpAddSumIter(ix + 2, iy + 2)
.undefine fpAddSumIter
; Handle carry
jr nc, _
ld a, 1
push hl
.macro fpAddCarryIter
inc hl
rrd
.endmacro
fpAddCarryIter
fpAddCarryIter
fpAddCarryIter
fpAddCarryIter
fpAddCarryIter
fpAddCarryIter
fpAddCarryIter
.undefine fpAddCarryIter
pop hl
inc (hl)
_:
pop bc
pop af
pop hl
pop iy
pop ix
ret
;; fpMulPow10 [Decimal Floating Point]
;; Multiplies the floating point number in IX by 10^E.
;; Inputs:
;; IX: Pointer to operand
;; E: Signed exponent (i.e. 2 -> 100, -3 -> 0.001)
;; Outputs:
;; IX: Pointer to result
;; Notes:
;; Does not check for overflow.
fpMulPow10:
push af
; Make sure operand is not zero
ld a, (ix + 2)
or a
jr z, .end
; Add the exponents
ld a, (ix + 1)
add e
ld (ix + 1), a
.end:
pop af
ret
;; fpAnd [Decimal Floating Point]
;; Performs a logical AND on the two floating point numbers.