4 * Copyright Roman Zippel, 1997. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, and the entire permission notice in its entirety,
11 * including the disclaimer of warranties.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior
19 * ALTERNATIVELY, this product may be distributed under the terms of
20 * the GNU General Public License, in which case the provisions of the GPL are
21 * required INSTEAD OF the above restrictions. (This clause is
22 * necessary due to a potential bad interaction between the GPL and
23 * the restrictions contained in a BSD-style copyright.)
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 * Here are lots of conversion and normalization functions mainly
43 * Note that these functions are optimized for "normal" numbers,
44 * these are handled first and exit as fast as possible, this is
45 * especially important for fp_normalize_ext/fp_conv_ext2ext, as
46 * it's called very often.
47 * The register usage is optimized for fp_scan.S and which register
48 * is currently at that time unused, be careful if you want change
49 * something here. %d0 and %d1 is always usable, sometimes %d2 (or
50 * only the lower half) most function have to return the %a0
51 * unmodified, so that the caller can immediately reuse it.
59 printf ,"fp_illegal\n"
61 | completed instruction
69 1: printf ,"oops:%p,%p,%p\n",3,%a2@(TASK_MM-8),%a2@(TASK_MM-4),%a2@(TASK_MM)
73 .globl fp_conv_long2ext, fp_conv_single2ext
74 .globl fp_conv_double2ext, fp_conv_ext2ext
75 .globl fp_normalize_ext, fp_normalize_double
76 .globl fp_normalize_single, fp_normalize_single_fast
77 .globl fp_conv_ext2double, fp_conv_ext2single
78 .globl fp_conv_ext2long, fp_conv_ext2short
79 .globl fp_conv_ext2byte
80 .globl fp_finalrounding_single, fp_finalrounding_single_fast
81 .globl fp_finalrounding_double
82 .globl fp_finalrounding, fp_finaltest, fp_final
85 * First several conversion functions from a source operand
86 * into the extended format. Note, that only fp_conv_ext2ext
87 * normalizes the number and is always called after the other
88 * conversion functions, which only move the information into
94 | args: %d0 = source (32-bit long)
95 | %a0 = destination (ptr to struct fp_ext)
98 printf PCONV,"l2e: %p -> %p(",2,%d0,%a0
99 clr.l %d1 | sign defaults to zero
101 jeq fp_l2e_zero | is source zero?
106 move.w #0x3fff+31,%d1
107 move.l %d1,(%a0)+ | set sign / exp
108 move.l %d0,(%a0)+ | set mantissa
110 subq.l #8,%a0 | restore %a0
125 | args: %d0 = source (single-precision fp value)
126 | %a0 = dest (struct fp_ext *)
129 printf PCONV,"s2e: %p -> %p(",2,%d0,%a0
131 lsl.l #8,%d0 | shift mantissa
132 lsr.l #8,%d1 | exponent / sign
135 jeq fp_s2e_small | zero / denormal?
136 cmp.w #0xff,%d1 | NaN / Inf?
138 bset #31,%d0 | set explizit bit
139 add.w #0x3fff-0x7f,%d1 | re-bias the exponent.
140 9: move.l %d1,(%a0)+ | fp_ext.sign, fp_ext.exp
141 move.l %d0,(%a0)+ | high lword of fp_ext.mant
142 clr.l (%a0) | low lword = 0
147 | zeros and denormalized
149 | exponent is zero, so explizit bit is already zero too
152 move.w #0x4000-0x7f,%d1
156 bclr #31,%d0 | clear explizit bit
162 getuser.l %a1@(0),%d0,fp_err_ua2,%a1
163 getuser.l %a1@(4),%d1,fp_err_ua2,%a1
164 printf PCONV,"d2e: %p%p -> %p(",3,%d0,%d1,%a0
166 getuser.l (%a1)+,%d0,fp_err_ua2,%a1
168 lsl.l #8,%d0 | shift high mantissa
170 lsr.l #8,%d1 | exponent / sign
173 jeq fp_d2e_small | zero / denormal?
174 cmp.w #0x7ff,%d1 | NaN / Inf?
176 bset #31,%d0 | set explizit bit
177 add.w #0x3fff-0x3ff,%d1 | re-bias the exponent.
178 9: move.l %d1,(%a0)+ | fp_ext.sign, fp_ext.exp
180 getuser.l (%a1)+,%d0,fp_err_ua2,%a1
192 | zeros and denormalized
194 | exponent is zero, so explizit bit is already zero too
197 move.w #0x4000-0x3ff,%d1
201 bclr #31,%d0 | clear explizit bit
206 | originally used to get longdouble from userspace, now it's
207 | called before arithmetic operations to make sure the number
208 | is normalized [maybe rename it?].
209 | args: %a0 = dest (struct fp_ext *)
210 | returns 0 in %d0 for a NaN, otherwise 1
213 printf PCONV,"e2e: %p(",1,%a0
217 cmp.w #0x7fff,%d0 | Inf / NaN?
220 jpl fp_e2e_small | zero / denorm?
221 | The high bit is set, so normalization is irrelevant.
224 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
228 printf PCONV,"%p(",1,%a0
233 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
235 fp_set_sr FPSR_EXC_INEX2
237 move.w (FPD_RND,FPDATA),%d2
238 jne fp_e2e_roundother | %d2 == 0, round to nearest
239 tst.b %d0 | test guard bit
240 jpl 9f | zero is closer
241 btst #0,(11,%a0) | test lsb bit
242 jne fp_e2e_doroundup | round to infinity
243 lsl.b #1,%d0 | check low bits
244 jeq 9f | round to zero
250 move.w #0x8000,(4,%a0)
252 9: printf PNORM,"%p(",1,%a0
258 jcs 9b | %d2 < 2, round to zero
259 jhi 1f | %d2 > 2, round to +infinity
260 tst.b (1,%a0) | to -inf
261 jne fp_e2e_doroundup | negative, round to infinity
262 jra 9b | positive, round to zero
263 1: tst.b (1,%a0) | to +inf
264 jeq fp_e2e_doroundup | positive, round to infinity
265 jra 9b | negative, round to zero
267 | zeros and subnormals:
268 | try to normalize these anyway.
270 jne fp_e2e_small1 | high lword zero?
273 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
281 printf PNORM,"%p(",1,%a0
286 | definitely subnormal, need to shift all 64 bits
288 bfffo %d0{#0,#32},%d1
292 | Pathologically small, denormalize.
297 jeq fp_e2e_checkround
298 | fancy 64-bit double-shift begins here
309 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
319 jra fp_e2e_checkround
326 jra fp_e2e_checkround
327 | pathologically small subnormal
329 bfffo %d0{#0,#32},%d1
334 | Beyond pathologically small, denormalize.
339 jeq fp_e2e_checkround
343 lsl.l %d1,%d0 | lower lword needs only to be shifted
344 move.l %d0,(%a0) | into the higher lword
345 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
351 bfins %d0,(%a0){%d1,#8}
353 jra fp_e2e_checkround
354 1: neg.w %d1 | lower lword is splitted between
355 bfins %d0,(%a0){%d1,#32} | higher and lower lword
356 #ifndef CONFIG_M68KFPU_EMU_EXTRAPREC
357 jra fp_e2e_checkround
361 | These are extremely small numbers, that will mostly end up as zero
362 | anyway, so this is only important for correct rounding.
364 bfffo %d0{#24,#8},%d1
369 | Pathologically small, denormalize.
374 jeq fp_e2e_checkround
383 jra fp_e2e_checkround
385 bfins %d0,(%a0){%d1,#8}
386 jra fp_e2e_checkround
391 jra fp_e2e_checkround
393 1: move.l %d0,%d1 | lower lword is splitted between
394 lsl.l %d2,%d0 | higher and lower lword
401 jra fp_e2e_checkround
402 | Infinities and NaNs
410 printf PCONV,"%p(",1,%a0
414 | we have maybe a NaN, shift off the highest bit
417 | we have a NaN, clear the return value
423 * Normalization functions. Call these on the output of general
424 * FP operators, and before any conversion into the destination
425 * formats. fp_normalize_ext has always to be called first, the
426 * following conversion functions expect an already normalized
431 | normalize an extended in extended (unpacked) format, basically
432 | it does the same as fp_conv_ext2ext, additionally it also does
433 | the necessary postprocessing checks.
434 | args: %a0 (struct fp_ext *)
435 | NOTE: it does _not_ modify %a0/%a1 and the upper word of %d2
438 printf PNORM,"ne: %p(",1,%a0
442 cmp.w #0x7fff,%d0 | Inf / NaN?
445 jpl fp_ne_small | zero / denorm?
446 | The high bit is set, so normalization is irrelevant.
449 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
453 printf PNORM,"%p(",1,%a0
457 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
459 fp_set_sr FPSR_EXC_INEX2
461 move.w (FPD_RND,FPDATA),%d2
462 jne fp_ne_roundother | %d2 == 0, round to nearest
463 tst.b %d0 | test guard bit
464 jpl 9f | zero is closer
465 btst #0,(11,%a0) | test lsb bit
466 jne fp_ne_doroundup | round to infinity
467 lsl.b #1,%d0 | check low bits
468 jeq 9f | round to zero
475 move.w #0x8000,(4,%a0)
476 9: printf PNORM,"%p(",1,%a0
482 jcs 9b | %d2 < 2, round to zero
483 jhi 1f | %d2 > 2, round to +infinity
484 tst.b (1,%a0) | to -inf
485 jne fp_ne_doroundup | negative, round to infinity
486 jra 9b | positive, round to zero
487 1: tst.b (1,%a0) | to +inf
488 jeq fp_ne_doroundup | positive, round to infinity
489 jra 9b | negative, round to zero
491 | Zeros and subnormal numbers
492 | These are probably merely subnormal, rather than "denormalized"
493 | numbers, so we will try to make them normal again.
495 jne fp_ne_small1 | high lword zero?
498 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
506 printf PNORM,"%p(",1,%a0
512 bfffo %d0{#0,#32},%d1
516 | Pathologically small, denormalize.
519 fp_set_sr FPSR_EXC_UNFL
523 | This is exactly the same 64-bit double shift as seen above.
534 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
552 | May or may not be subnormal, if so, only 32 bits to shift.
554 bfffo %d0{#0,#32},%d1
559 | Beyond pathologically small, denormalize.
562 fp_set_sr FPSR_EXC_UNFL
569 lsl.l %d1,%d0 | lower lword needs only to be shifted
570 move.l %d0,(%a0) | into the higher lword
571 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
577 bfins %d0,(%a0){%d1,#8}
580 1: neg.w %d1 | lower lword is splitted between
581 bfins %d0,(%a0){%d1,#32} | higher and lower lword
582 #ifndef CONFIG_M68KFPU_EMU_EXTRAPREC
587 | These are extremely small numbers, that will mostly end up as zero
588 | anyway, so this is only important for correct rounding.
590 bfffo %d0{#24,#8},%d1
595 | Pathologically small, denormalize.
611 bfins %d0,(%a0){%d1,#8}
619 | Infinities and NaNs, again, same as above.
626 printf PNORM,"%p(",1,%a0
630 | we have maybe a NaN, shift off the highest bit
636 | we have a NaN, test if it is signaling
639 fp_set_sr FPSR_EXC_SNAN
643 | these next two do rounding as per the IEEE standard.
644 | values for the rounding modes appear to be:
645 | 0: Round to nearest
647 | 2: Round to -Infinity
648 | 3: Round to +Infinity
649 | both functions expect that fp_normalize was already
650 | called (and extended argument is already normalized
651 | as far as possible), these are used if there is different
652 | rounding precision is selected and before converting
655 | fp_normalize_double:
656 | normalize an extended with double (52-bit) precision
657 | args: %a0 (struct fp_ext *)
660 printf PNORM,"nd: %p(",1,%a0
665 jeq fp_nd_zero | zero / denormalized
667 jeq fp_nd_huge | NaN / infinitive.
668 sub.w #0x4000-0x3ff,%d2 | will the exponent fit?
669 jcs fp_nd_small | too small.
671 jcc fp_nd_large | too big.
673 move.l (%a0),%d0 | low lword of mantissa
674 | now, round off the low 11 bits.
677 lsl.l %d1,%d0 | keep 11 low bits.
678 jne fp_nd_checkround | Are they non-zero?
681 printf PNORM,"%p(",1,%a0
685 | Be careful with the X bit! It contains the lsb
686 | from the shift above, it is needed for round to nearest.
688 fp_set_sr FPSR_EXC_INEX2 | INEX2 bit
689 and.w #0xf800,(2,%a0) | clear bits 0-10
690 move.w (FPD_RND,FPDATA),%d2 | rounding mode
691 jne 2f | %d2 == 0, round to nearest
692 tst.l %d0 | test guard bit
693 jpl 9b | zero is closer
694 | here we test the X bit by adding it to %d2
695 clr.w %d2 | first set z bit, addx only clears it
696 addx.w %d2,%d2 | test lsb bit
697 | IEEE754-specified "round to even" behaviour. If the guard
698 | bit is set, then the number is odd, so rounding works like
699 | in grade-school arithmetic (i.e. 1.5 rounds to 2.0)
700 | Otherwise, an equal distance rounds towards zero, so as not
701 | to produce an odd number. This is strange, but it is what
703 jne fp_nd_doroundup | round to infinity
704 lsl.l #1,%d0 | check low bits
705 jeq 9b | round to zero
707 | round (the mantissa, that is) towards infinity
709 jcc 9b | no overflow, good.
710 addq.l #1,-(%a0) | extend to high lword
711 jcc 1f | no overflow, good.
712 | Yow! we have managed to overflow the mantissa. Since this
713 | only happens when %d1 was 0xfffff800, it is now zero, so
714 | reset the high bit, and increment the exponent.
717 cmp.w #0x43ff,(%a0)+ | exponent now overflown?
718 jeq fp_nd_large | yes, so make it infinity.
720 printf PNORM,"%p(",1,%a0
725 jcs 9b | %d2 < 2, round to zero
726 jhi 3f | %d2 > 2, round to +infinity
727 | Round to +Inf or -Inf. High word of %d2 contains the
728 | sign of the number, by the way.
731 jne fp_nd_doroundup | negative, round to infinity
732 jra 9b | positive, round to zero
733 3: swap %d2 | to +inf
735 jeq fp_nd_doroundup | positive, round to infinity
736 jra 9b | negative, round to zero
737 | Exponent underflow. Try to make a denormal, and set it to
738 | the smallest possible fraction if this fails.
740 fp_set_sr FPSR_EXC_UNFL | set UNFL bit
741 move.w #0x3c01,(-2,%a0) | 2**-1022
742 neg.w %d2 | degree of underflow
743 cmp.w #32,%d2 | single or double shift?
745 | Again, another 64-bit double shift.
758 | Check to see if we shifted off any significant bits
760 jeq fp_nd_round | Nope, round.
761 bset #0,%d0 | Yes, so set the "sticky bit".
762 jra fp_nd_round | Now, round.
763 | Another 64-bit single shift and store
765 cmp.w #32,%d2 | Do we really need to shift?
766 jcc 2f | No, the number is too small.
773 | Again, check to see if we shifted off any significant bits.
776 bset #0,%d0 | Sticky bit.
782 | Sorry, the number is just too small.
785 moveq #1,%d0 | Smallest possible fraction,
786 jra fp_nd_round | round as desired.
787 | zero and denormalized
794 printf PNORM,"%p(",1,%a0
797 rts | zero. nothing to do.
798 | These are not merely subnormal numbers, but true denormals,
799 | i.e. pathologically small (exponent is 2**-16383) numbers.
800 | It is clearly impossible for even a normal extended number
801 | with that exponent to fit into double precision, so just
802 | write these ones off as "too darn small".
803 1: fp_set_sr FPSR_EXC_UNFL | Set UNFL bit
806 move.w #0x3c01,-(%a0) | i.e. 2**-1022
809 jra fp_nd_round | round.
810 | Exponent overflow. Just call it infinity.
815 fp_set_sr FPSR_EXC_INEX2
816 1: fp_set_sr FPSR_EXC_OVFL
817 move.w (FPD_RND,FPDATA),%d2
818 jne 3f | %d2 = 0 round to nearest
819 1: move.w #0x7fff,(-2,%a0)
823 printf PNORM,"%p(",1,%a0
828 jcs 5f | %d2 < 2, round to zero
829 jhi 4f | %d2 > 2, round to +infinity
830 tst.b (-3,%a0) | to -inf
833 4: tst.b (-3,%a0) | to +inf
835 5: move.w #0x43fe,(-2,%a0)
844 printf PNORM,"%p(",1,%a0
849 | fp_normalize_single:
850 | normalize an extended with single (23-bit) precision
851 | args: %a0 (struct fp_ext *)
854 printf PNORM,"ns: %p(",1,%a0
859 jeq fp_ns_zero | zero / denormalized
861 jeq fp_ns_huge | NaN / infinitive.
862 sub.w #0x4000-0x7f,%d2 | will the exponent fit?
863 jcs fp_ns_small | too small.
865 jcc fp_ns_large | too big.
866 move.l (%a0)+,%d0 | get high lword of mantissa
868 tst.l (%a0) | check the low lword
870 | Set a sticky bit if it is non-zero. This should only
871 | affect the rounding in what would otherwise be equal-
872 | distance situations, which is what we want it to do.
874 1: clr.l (%a0) | zap it from memory.
875 | now, round off the low 8 bits of the hi lword.
876 tst.b %d0 | 8 low bits.
877 jne fp_ns_checkround | Are they non-zero?
880 printf PNORM,"%p(",1,%a0
885 fp_set_sr FPSR_EXC_INEX2 | INEX2 bit
886 clr.b -(%a0) | clear low byte of high lword
888 move.w (FPD_RND,FPDATA),%d2 | rounding mode
889 jne 2f | %d2 == 0, round to nearest
890 tst.b %d0 | test guard bit
891 jpl 9f | zero is closer
892 btst #8,%d0 | test lsb bit
893 | round to even behaviour, see above.
894 jne fp_ns_doroundup | round to infinity
895 lsl.b #1,%d0 | check low bits
896 jeq 9f | round to zero
898 | round (the mantissa, that is) towards infinity
900 jcc 9f | no overflow, good.
901 | Overflow. This means that the %d1 was 0xffffff00, so it
902 | is now zero. We will set the mantissa to reflect this, and
903 | increment the exponent (checking for overflow there too)
906 cmp.w #0x407f,(%a0)+ | exponent now overflown?
907 jeq fp_ns_large | yes, so make it infinity.
909 printf PNORM,"%p(",1,%a0
913 | check nondefault rounding modes
915 jcs 9b | %d2 < 2, round to zero
916 jhi 3f | %d2 > 2, round to +infinity
917 tst.b (-3,%a0) | to -inf
918 jne fp_ns_doroundup | negative, round to infinity
919 jra 9b | positive, round to zero
920 3: tst.b (-3,%a0) | to +inf
921 jeq fp_ns_doroundup | positive, round to infinity
922 jra 9b | negative, round to zero
923 | Exponent underflow. Try to make a denormal, and set it to
924 | the smallest possible fraction if this fails.
926 fp_set_sr FPSR_EXC_UNFL | set UNFL bit
927 move.w #0x3f81,(-2,%a0) | 2**-126
928 neg.w %d2 | degree of underflow
929 cmp.w #32,%d2 | single or double shift?
936 | Check to see if we shifted off any significant bits.
941 bset #0,%d0 | Sticky bit.
942 | Check the lower lword
946 bset #0,%d0 | Sticky bit.
948 | Sorry, the number is just too small.
951 moveq #1,%d0 | Smallest possible fraction,
952 jra fp_ns_round | round as desired.
953 | Exponent overflow. Just call it infinity.
957 fp_set_sr FPSR_EXC_INEX2
958 1: fp_set_sr FPSR_EXC_OVFL
959 move.w (FPD_RND,FPDATA),%d2
960 jne 3f | %d2 = 0 round to nearest
961 1: move.w #0x7fff,(-2,%a0)
965 printf PNORM,"%p(",1,%a0
970 jcs 5f | %d2 < 2, round to zero
971 jhi 4f | %d2 > 2, round to +infinity
972 tst.b (-3,%a0) | to -inf
975 4: tst.b (-3,%a0) | to +inf
977 5: move.w #0x407e,(-2,%a0)
978 move.l #0xffffff00,(%a0)+
981 | zero and denormalized
988 printf PNORM,"%p(",1,%a0
991 rts | zero. nothing to do.
992 | These are not merely subnormal numbers, but true denormals,
993 | i.e. pathologically small (exponent is 2**-16383) numbers.
994 | It is clearly impossible for even a normal extended number
995 | with that exponent to fit into single precision, so just
996 | write these ones off as "too darn small".
997 1: fp_set_sr FPSR_EXC_UNFL | Set UNFL bit
1000 move.w #0x3f81,-(%a0) | i.e. 2**-126
1003 jra fp_ns_round | round.
1004 | Infinities or NaNs
1007 printf PNORM,"%p(",1,%a0
1012 | fp_normalize_single_fast:
1013 | normalize an extended with single (23-bit) precision
1014 | this is only used by fsgldiv/fsgdlmul, where the
1015 | operand is not completly normalized.
1016 | args: %a0 (struct fp_ext *)
1018 fp_normalize_single_fast:
1019 printf PNORM,"nsf: %p(",1,%a0
1025 jeq fp_nsf_huge | NaN / infinitive.
1026 move.l (%a0)+,%d0 | get high lword of mantissa
1028 tst.l (%a0) | check the low lword
1030 | Set a sticky bit if it is non-zero. This should only
1031 | affect the rounding in what would otherwise be equal-
1032 | distance situations, which is what we want it to do.
1034 1: clr.l (%a0) | zap it from memory.
1035 | now, round off the low 8 bits of the hi lword.
1036 tst.b %d0 | 8 low bits.
1037 jne fp_nsf_checkround | Are they non-zero?
1038 | nothing to do here
1040 printf PNORM,"%p(",1,%a0
1045 fp_set_sr FPSR_EXC_INEX2 | INEX2 bit
1046 clr.b -(%a0) | clear low byte of high lword
1048 move.w (FPD_RND,FPDATA),%d2 | rounding mode
1049 jne 2f | %d2 == 0, round to nearest
1050 tst.b %d0 | test guard bit
1051 jpl 9f | zero is closer
1052 btst #8,%d0 | test lsb bit
1053 | round to even behaviour, see above.
1054 jne fp_nsf_doroundup | round to infinity
1055 lsl.b #1,%d0 | check low bits
1056 jeq 9f | round to zero
1058 | round (the mantissa, that is) towards infinity
1060 jcc 9f | no overflow, good.
1061 | Overflow. This means that the %d1 was 0xffffff00, so it
1062 | is now zero. We will set the mantissa to reflect this, and
1063 | increment the exponent (checking for overflow there too)
1064 move.w #0x8000,(%a0)
1066 cmp.w #0x407f,(%a0)+ | exponent now overflown?
1067 jeq fp_nsf_large | yes, so make it infinity.
1069 printf PNORM,"%p(",1,%a0
1073 | check nondefault rounding modes
1075 jcs 9b | %d2 < 2, round to zero
1076 jhi 3f | %d2 > 2, round to +infinity
1077 tst.b (-3,%a0) | to -inf
1078 jne fp_nsf_doroundup | negative, round to infinity
1079 jra 9b | positive, round to zero
1080 3: tst.b (-3,%a0) | to +inf
1081 jeq fp_nsf_doroundup | positive, round to infinity
1082 jra 9b | negative, round to zero
1083 | Exponent overflow. Just call it infinity.
1087 fp_set_sr FPSR_EXC_INEX2
1088 1: fp_set_sr FPSR_EXC_OVFL
1089 move.w (FPD_RND,FPDATA),%d2
1090 jne 3f | %d2 = 0 round to nearest
1091 1: move.w #0x7fff,(-2,%a0)
1095 printf PNORM,"%p(",1,%a0
1100 jcs 5f | %d2 < 2, round to zero
1101 jhi 4f | %d2 > 2, round to +infinity
1102 tst.b (-3,%a0) | to -inf
1105 4: tst.b (-3,%a0) | to +inf
1107 5: move.w #0x407e,(-2,%a0)
1108 move.l #0xffffff00,(%a0)+
1111 | Infinities or NaNs
1114 printf PNORM,"%p(",1,%a0
1119 | conv_ext2int (macro):
1120 | Generates a subroutine that converts an extended value to an
1121 | integer of a given size, again, with the appropriate type of
1125 | s: size, as given in an assembly instruction.
1126 | b: number of bits in that size.
1128 | Subroutine arguments:
1129 | %a0: source (struct fp_ext *)
1131 | Returns the integer in %d0 (like it should)
1133 .macro conv_ext2int s,b
1134 .set inf,(1<<(\b-1))-1 | i.e. MAXINT
1135 printf PCONV,"e2i%d: %p(",2,#\b,%a0
1139 move.w (%a0)+,%d2 | exponent
1140 jeq fp_e2i_zero\b | zero / denorm (== 0, here)
1142 jeq fp_e2i_huge\b | Inf / NaN
1160 printf PCONV,"-> %p\n",1,%d0
1165 1: printf PCONV,"-> %p\n",1,%d0
1168 fp_set_sr FPSR_EXC_INEX2 | INEX2 bit
1175 move.w (FPD_RND,FPDATA),%d2 | rounding mode
1176 jne 2f | %d2 == 0, round to nearest
1177 tst.l %d1 | test guard bit
1178 jpl 9b | zero is closer
1179 btst %d2,%d0 | test lsb bit (%d2 still 0)
1180 jne fp_e2i_doroundup\b
1181 lsl.l #1,%d1 | check low bits
1182 jne fp_e2i_doroundup\b
1188 | check nondefault rounding modes
1190 jcs 9b | %d2 < 2, round to zero
1191 jhi 3f | %d2 > 2, round to +infinity
1192 tst.w (-4,%a0) | to -inf
1193 jne fp_e2i_doroundup\b | negative, round to infinity
1194 jra 9b | positive, round to zero
1195 3: tst.w (-4,%a0) | to +inf
1196 jeq fp_e2i_doroundup\b | positive, round to infinity
1197 jra 9b | negative, round to zero
1198 | we are only want -2**127 get correctly rounded here,
1199 | since the guard bit is in the lower lword.
1200 | everything else ends up anyway as overflow.
1202 5: move.w (FPD_RND,FPDATA),%d2 | rounding mode
1203 jne 2b | %d2 == 0, round to nearest
1204 move.l (4,%a0),%d1 | test guard bit
1205 jpl 9b | zero is closer
1206 lsl.l #1,%d1 | check low bits
1207 jne fp_e2i_doroundup\b
1217 fp_clr_sr FPSR_EXC_UNFL | fp_normalize_ext has set this bit
1219 fp_set_sr FPSR_EXC_INEX2
1221 move.w (FPD_RND,FPDATA),%d2 | rounding mode
1223 jcs 3f | %d2 < 2, round to nearest/zero
1224 jhi 2f | %d2 > 2, round to +infinity
1225 tst.w (-4,%a0) | to -inf
1229 2: tst.w (-4,%a0) | to +inf
1232 3: printf PCONV,"-> %p\n",1,%d0
1235 fp_set_sr FPSR_EXC_OPERR
1240 1: printf PCONV,"-> %p\n",1,%d0
1248 | fp_normalize_ext has set this bit already
1249 | and made the number nonsignaling
1250 1: fp_tst_sr FPSR_EXC_SNAN
1252 fp_set_sr FPSR_EXC_OPERR
1253 1: printf PCONV,"-> %p\n",1,%d0
1267 jsr fp_normalize_double
1268 printf PCONV,"e2d: %p(",1,%a0
1277 1: sub.w #0x3fff-0x3ff,%d2
1289 putuser.l %d0,(%a1)+,fp_err_ua2,%a1
1296 putuser.l %d0,(%a1),fp_err_ua2,%a1
1297 #ifdef FPU_EMU_DEBUG
1298 getuser.l %a1@(-4),%d0,fp_err_ua2,%a1
1299 getuser.l %a1@(0),%d1,fp_err_ua2,%a1
1300 printf PCONV,"%p(%08x%08x)\n",3,%a1,%d0,%d1
1305 jsr fp_normalize_single
1306 printf PCONV,"e2s: %p(",1,%a0
1315 1: sub.w #0x3fff-0x7f,%d1
1325 printf PCONV,"%08x\n",1,%d0
1328 | special return addresses for instr that
1329 | encode the rounding precision in the opcode
1330 | (e.g. fsmove,fdmove)
1332 fp_finalrounding_single:
1334 jsr fp_normalize_ext
1335 jsr fp_normalize_single
1338 fp_finalrounding_single_fast:
1340 jsr fp_normalize_ext
1341 jsr fp_normalize_single_fast
1344 fp_finalrounding_double:
1346 jsr fp_normalize_ext
1347 jsr fp_normalize_double
1351 | set the emulated status register based on the outcome of an
1352 | emulated instruction.
1356 | printf ,"f: %p\n",1,%a0
1357 jsr fp_normalize_ext
1358 move.w (FPD_PREC,FPDATA),%d0
1362 jsr fp_normalize_single
1364 1: jsr fp_normalize_double
1365 2:| printf ,"f: %p\n",1,%a0
1367 | First, we do some of the obvious tests for the exception
1368 | status byte and condition code bytes of fp_sr here, so that
1369 | they do not have to be handled individually by every
1370 | emulated instruction.
1375 bset #FPSR_CC_NEG-24,%d0 | N bit
1376 1: cmp.w #0x7fff,(%a0)+ | exponent
1379 moveq #FPSR_CC_Z-24,%d1
1386 2: moveq #FPSR_CC_NAN-24,%d1
1388 lsl.l #1,%d2 | ignore high bit
1392 moveq #FPSR_CC_INF-24,%d1
1394 9: move.b %d0,(FPD_FPSR+0,FPDATA) | set condition test result
1395 | move instructions enter here
1396 | Here, we test things in the exception status byte, and set
1397 | other things in the accrued exception byte accordingly.
1398 | Emulated instructions can set various things in the former,
1399 | as defined in fp_emu.h.
1401 move.l (FPD_FPSR,FPDATA),%d0
1403 btst #FPSR_EXC_SNAN,%d0 | EXC_SNAN
1405 btst #FPSR_EXC_OPERR,%d0 | EXC_OPERR
1407 1: bset #FPSR_AEXC_IOP,%d0 | set IOP bit
1408 2: btst #FPSR_EXC_OVFL,%d0 | EXC_OVFL
1410 bset #FPSR_AEXC_OVFL,%d0 | set OVFL bit
1411 1: btst #FPSR_EXC_UNFL,%d0 | EXC_UNFL
1413 btst #FPSR_EXC_INEX2,%d0 | EXC_INEX2
1415 bset #FPSR_AEXC_UNFL,%d0 | set UNFL bit
1416 1: btst #FPSR_EXC_DZ,%d0 | EXC_INEX1
1418 bset #FPSR_AEXC_DZ,%d0 | set DZ bit
1419 1: btst #FPSR_EXC_OVFL,%d0 | EXC_OVFL
1421 btst #FPSR_EXC_INEX2,%d0 | EXC_INEX2
1423 btst #FPSR_EXC_INEX1,%d0 | EXC_INEX1
1425 1: bset #FPSR_AEXC_INEX,%d0 | set INEX bit
1426 2: move.l %d0,(FPD_FPSR,FPDATA)
1428 | same as above, greatly optimized, but untested (yet)
1448 move.l %d2,(FPD_FPSR,FPDATA)
1450 move.b (FPD_FPSR+2,FPDATA),%d0
1451 and.b (FPD_FPCR+2,FPDATA),%d0
1453 printf ,"send signal!!!\n"