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 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.
38 #include <linux/config.h>
42 * Here are lots of conversion and normalization functions mainly
44 * Note that these functions are optimized for "normal" numbers,
45 * these are handled first and exit as fast as possible, this is
46 * especially important for fp_normalize_ext/fp_conv_ext2ext, as
47 * it's called very often.
48 * The register usage is optimized for fp_scan.S and which register
49 * is currently at that time unused, be careful if you want change
50 * something here. %d0 and %d1 is always usable, sometimes %d2 (or
51 * only the lower half) most function have to return the %a0
52 * unmodified, so that the caller can immediatly reuse it.
60 printf ,"fp_illegal\n"
62 | completed instruction
70 1: printf ,"oops:%p,%p,%p\n",3,%a2@(TASK_MM-8),%a2@(TASK_MM-4),%a2@(TASK_MM)
74 .globl fp_conv_long2ext, fp_conv_single2ext
75 .globl fp_conv_double2ext, fp_conv_ext2ext
76 .globl fp_normalize_ext, fp_normalize_double
77 .globl fp_normalize_single, fp_normalize_single_fast
78 .globl fp_conv_ext2double, fp_conv_ext2single
79 .globl fp_conv_ext2long, fp_conv_ext2short
80 .globl fp_conv_ext2byte
81 .globl fp_finalrounding_single, fp_finalrounding_single_fast
82 .globl fp_finalrounding_double
83 .globl fp_finalrounding, fp_finaltest, fp_final
86 * First several conversion functions from a source operand
87 * into the extended format. Note, that only fp_conv_ext2ext
88 * normalizes the number and is always called after the other
89 * conversion functions, which only move the information into
95 | args: %d0 = source (32-bit long)
96 | %a0 = destination (ptr to struct fp_ext)
99 printf PCONV,"l2e: %p -> %p(",2,%d0,%a0
100 clr.l %d1 | sign defaults to zero
102 jeq fp_l2e_zero | is source zero?
107 move.w #0x3fff+31,%d1
108 move.l %d1,(%a0)+ | set sign / exp
109 move.l %d0,(%a0)+ | set mantissa
111 subq.l #8,%a0 | restore %a0
126 | args: %d0 = source (single-precision fp value)
127 | %a0 = dest (struct fp_ext *)
130 printf PCONV,"s2e: %p -> %p(",2,%d0,%a0
132 lsl.l #8,%d0 | shift mantissa
133 lsr.l #8,%d1 | exponent / sign
136 jeq fp_s2e_small | zero / denormal?
137 cmp.w #0xff,%d1 | NaN / Inf?
139 bset #31,%d0 | set explizit bit
140 add.w #0x3fff-0x7f,%d1 | re-bias the exponent.
141 9: move.l %d1,(%a0)+ | fp_ext.sign, fp_ext.exp
142 move.l %d0,(%a0)+ | high lword of fp_ext.mant
143 clr.l (%a0) | low lword = 0
148 | zeros and denormalized
150 | exponent is zero, so explizit bit is already zero too
153 move.w #0x4000-0x7f,%d1
157 bclr #31,%d0 | clear explizit bit
163 getuser.l %a1@(0),%d0,fp_err_ua2,%a1
164 getuser.l %a1@(4),%d1,fp_err_ua2,%a1
165 printf PCONV,"d2e: %p%p -> %p(",3,%d0,%d1,%a0
167 getuser.l (%a1)+,%d0,fp_err_ua2,%a1
169 lsl.l #8,%d0 | shift high mantissa
171 lsr.l #8,%d1 | exponent / sign
174 jeq fp_d2e_small | zero / denormal?
175 cmp.w #0x7ff,%d1 | NaN / Inf?
177 bset #31,%d0 | set explizit bit
178 add.w #0x3fff-0x3ff,%d1 | re-bias the exponent.
179 9: move.l %d1,(%a0)+ | fp_ext.sign, fp_ext.exp
181 getuser.l (%a1)+,%d0,fp_err_ua2,%a1
193 | zeros and denormalized
195 | exponent is zero, so explizit bit is already zero too
198 move.w #0x4000-0x3ff,%d1
202 bclr #31,%d0 | clear explizit bit
207 | originally used to get longdouble from userspace, now it's
208 | called before arithmetic operations to make sure the number
209 | is normalized [maybe rename it?].
210 | args: %a0 = dest (struct fp_ext *)
211 | returns 0 in %d0 for a NaN, otherwise 1
214 printf PCONV,"e2e: %p(",1,%a0
218 cmp.w #0x7fff,%d0 | Inf / NaN?
221 jpl fp_e2e_small | zero / denorm?
222 | The high bit is set, so normalization is irrelevant.
225 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
229 printf PCONV,"%p(",1,%a0
234 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
236 fp_set_sr FPSR_EXC_INEX2
238 move.w (FPD_RND,FPDATA),%d2
239 jne fp_e2e_roundother | %d2 == 0, round to nearest
240 tst.b %d0 | test guard bit
241 jpl 9f | zero is closer
242 btst #0,(11,%a0) | test lsb bit
243 jne fp_e2e_doroundup | round to infinity
244 lsl.b #1,%d0 | check low bits
245 jeq 9f | round to zero
251 move.w #0x8000,(4,%a0)
253 9: printf PNORM,"%p(",1,%a0
259 jcs 9b | %d2 < 2, round to zero
260 jhi 1f | %d2 > 2, round to +infinity
261 tst.b (1,%a0) | to -inf
262 jne fp_e2e_doroundup | negative, round to infinity
263 jra 9b | positive, round to zero
264 1: tst.b (1,%a0) | to +inf
265 jeq fp_e2e_doroundup | positive, round to infinity
266 jra 9b | negative, round to zero
268 | zeros and subnormals:
269 | try to normalize these anyway.
271 jne fp_e2e_small1 | high lword zero?
274 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
282 printf PNORM,"%p(",1,%a0
287 | definitely subnormal, need to shift all 64 bits
289 bfffo %d0{#0,#32},%d1
293 | Pathologically small, denormalize.
298 jeq fp_e2e_checkround
299 | fancy 64-bit double-shift begins here
310 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
320 jra fp_e2e_checkround
327 jra fp_e2e_checkround
328 | pathologically small subnormal
330 bfffo %d0{#0,#32},%d1
335 | Beyond pathologically small, denormalize.
340 jeq fp_e2e_checkround
344 lsl.l %d1,%d0 | lower lword needs only to be shifted
345 move.l %d0,(%a0) | into the higher lword
346 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
352 bfins %d0,(%a0){%d1,#8}
354 jra fp_e2e_checkround
355 1: neg.w %d1 | lower lword is splitted between
356 bfins %d0,(%a0){%d1,#32} | higher and lower lword
357 #ifndef CONFIG_M68KFPU_EMU_EXTRAPREC
358 jra fp_e2e_checkround
362 | These are extremely small numbers, that will mostly end up as zero
363 | anyway, so this is only important for correct rounding.
365 bfffo %d0{#24,#8},%d1
370 | Pathologically small, denormalize.
375 jeq fp_e2e_checkround
384 jra fp_e2e_checkround
386 bfins %d0,(%a0){%d1,#8}
387 jra fp_e2e_checkround
392 jra fp_e2e_checkround
394 1: move.l %d0,%d1 | lower lword is splitted between
395 lsl.l %d2,%d0 | higher and lower lword
402 jra fp_e2e_checkround
403 | Infinities and NaNs
411 printf PCONV,"%p(",1,%a0
415 | we have maybe a NaN, shift off the highest bit
418 | we have a NaN, clear the return value
424 * Normalization functions. Call these on the output of general
425 * FP operators, and before any conversion into the destination
426 * formats. fp_normalize_ext has always to be called first, the
427 * following conversion functions expect an already normalized
432 | normalize an extended in extended (unpacked) format, basically
433 | it does the same as fp_conv_ext2ext, additionally it also does
434 | the necessary postprocessing checks.
435 | args: %a0 (struct fp_ext *)
436 | NOTE: it does _not_ modify %a0/%a1 and the upper word of %d2
439 printf PNORM,"ne: %p(",1,%a0
443 cmp.w #0x7fff,%d0 | Inf / NaN?
446 jpl fp_ne_small | zero / denorm?
447 | The high bit is set, so normalization is irrelevant.
450 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
454 printf PNORM,"%p(",1,%a0
458 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
460 fp_set_sr FPSR_EXC_INEX2
462 move.w (FPD_RND,FPDATA),%d2
463 jne fp_ne_roundother | %d2 == 0, round to nearest
464 tst.b %d0 | test guard bit
465 jpl 9f | zero is closer
466 btst #0,(11,%a0) | test lsb bit
467 jne fp_ne_doroundup | round to infinity
468 lsl.b #1,%d0 | check low bits
469 jeq 9f | round to zero
476 move.w #0x8000,(4,%a0)
477 9: printf PNORM,"%p(",1,%a0
483 jcs 9b | %d2 < 2, round to zero
484 jhi 1f | %d2 > 2, round to +infinity
485 tst.b (1,%a0) | to -inf
486 jne fp_ne_doroundup | negative, round to infinity
487 jra 9b | positive, round to zero
488 1: tst.b (1,%a0) | to +inf
489 jeq fp_ne_doroundup | positive, round to infinity
490 jra 9b | negative, round to zero
492 | Zeros and subnormal numbers
493 | These are probably merely subnormal, rather than "denormalized"
494 | numbers, so we will try to make them normal again.
496 jne fp_ne_small1 | high lword zero?
499 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
507 printf PNORM,"%p(",1,%a0
513 bfffo %d0{#0,#32},%d1
517 | Pathologically small, denormalize.
520 fp_set_sr FPSR_EXC_UNFL
524 | This is exactly the same 64-bit double shift as seen above.
535 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
553 | May or may not be subnormal, if so, only 32 bits to shift.
555 bfffo %d0{#0,#32},%d1
560 | Beyond pathologically small, denormalize.
563 fp_set_sr FPSR_EXC_UNFL
570 lsl.l %d1,%d0 | lower lword needs only to be shifted
571 move.l %d0,(%a0) | into the higher lword
572 #ifdef CONFIG_M68KFPU_EMU_EXTRAPREC
578 bfins %d0,(%a0){%d1,#8}
581 1: neg.w %d1 | lower lword is splitted between
582 bfins %d0,(%a0){%d1,#32} | higher and lower lword
583 #ifndef CONFIG_M68KFPU_EMU_EXTRAPREC
588 | These are extremely small numbers, that will mostly end up as zero
589 | anyway, so this is only important for correct rounding.
591 bfffo %d0{#24,#8},%d1
596 | Pathologically small, denormalize.
612 bfins %d0,(%a0){%d1,#8}
620 | Infinities and NaNs, again, same as above.
627 printf PNORM,"%p(",1,%a0
631 | we have maybe a NaN, shift off the highest bit
637 | we have a NaN, test if it is signaling
640 fp_set_sr FPSR_EXC_SNAN
644 | these next two do rounding as per the IEEE standard.
645 | values for the rounding modes appear to be:
646 | 0: Round to nearest
648 | 2: Round to -Infinity
649 | 3: Round to +Infinity
650 | both functions expect that fp_normalize was already
651 | called (and extended argument is already normalized
652 | as far as possible), these are used if there is different
653 | rounding precision is selected and before converting
656 | fp_normalize_double:
657 | normalize an extended with double (52-bit) precision
658 | args: %a0 (struct fp_ext *)
661 printf PNORM,"nd: %p(",1,%a0
666 jeq fp_nd_zero | zero / denormalized
668 jeq fp_nd_huge | NaN / infinitive.
669 sub.w #0x4000-0x3ff,%d2 | will the exponent fit?
670 jcs fp_nd_small | too small.
672 jcc fp_nd_large | too big.
674 move.l (%a0),%d0 | low lword of mantissa
675 | now, round off the low 11 bits.
678 lsl.l %d1,%d0 | keep 11 low bits.
679 jne fp_nd_checkround | Are they non-zero?
682 printf PNORM,"%p(",1,%a0
686 | Be careful with the X bit! It contains the lsb
687 | from the shift above, it is needed for round to nearest.
689 fp_set_sr FPSR_EXC_INEX2 | INEX2 bit
690 and.w #0xf800,(2,%a0) | clear bits 0-10
691 move.w (FPD_RND,FPDATA),%d2 | rounding mode
692 jne 2f | %d2 == 0, round to nearest
693 tst.l %d0 | test guard bit
694 jpl 9b | zero is closer
695 | here we test the X bit by adding it to %d2
696 clr.w %d2 | first set z bit, addx only clears it
697 addx.w %d2,%d2 | test lsb bit
698 | IEEE754-specified "round to even" behaviour. If the guard
699 | bit is set, then the number is odd, so rounding works like
700 | in grade-school arithmetic (i.e. 1.5 rounds to 2.0)
701 | Otherwise, an equal distance rounds towards zero, so as not
702 | to produce an odd number. This is strange, but it is what
704 jne fp_nd_doroundup | round to infinity
705 lsl.l #1,%d0 | check low bits
706 jeq 9b | round to zero
708 | round (the mantissa, that is) towards infinity
710 jcc 9b | no overflow, good.
711 addq.l #1,-(%a0) | extend to high lword
712 jcc 1f | no overflow, good.
713 | Yow! we have managed to overflow the mantissa. Since this
714 | only happens when %d1 was 0xfffff800, it is now zero, so
715 | reset the high bit, and increment the exponent.
718 cmp.w #0x43ff,(%a0)+ | exponent now overflown?
719 jeq fp_nd_large | yes, so make it infinity.
721 printf PNORM,"%p(",1,%a0
726 jcs 9b | %d2 < 2, round to zero
727 jhi 3f | %d2 > 2, round to +infinity
728 | Round to +Inf or -Inf. High word of %d2 contains the
729 | sign of the number, by the way.
732 jne fp_nd_doroundup | negative, round to infinity
733 jra 9b | positive, round to zero
734 3: swap %d2 | to +inf
736 jeq fp_nd_doroundup | positive, round to infinity
737 jra 9b | negative, round to zero
738 | Exponent underflow. Try to make a denormal, and set it to
739 | the smallest possible fraction if this fails.
741 fp_set_sr FPSR_EXC_UNFL | set UNFL bit
742 move.w #0x3c01,(-2,%a0) | 2**-1022
743 neg.w %d2 | degree of underflow
744 cmp.w #32,%d2 | single or double shift?
746 | Again, another 64-bit double shift.
759 | Check to see if we shifted off any significant bits
761 jeq fp_nd_round | Nope, round.
762 bset #0,%d0 | Yes, so set the "sticky bit".
763 jra fp_nd_round | Now, round.
764 | Another 64-bit single shift and store
766 cmp.w #32,%d2 | Do we really need to shift?
767 jcc 2f | No, the number is too small.
774 | Again, check to see if we shifted off any significant bits.
777 bset #0,%d0 | Sticky bit.
783 | Sorry, the number is just too small.
786 moveq #1,%d0 | Smallest possible fraction,
787 jra fp_nd_round | round as desired.
788 | zero and denormalized
795 printf PNORM,"%p(",1,%a0
798 rts | zero. nothing to do.
799 | These are not merely subnormal numbers, but true denormals,
800 | i.e. pathologically small (exponent is 2**-16383) numbers.
801 | It is clearly impossible for even a normal extended number
802 | with that exponent to fit into double precision, so just
803 | write these ones off as "too darn small".
804 1: fp_set_sr FPSR_EXC_UNFL | Set UNFL bit
807 move.w #0x3c01,-(%a0) | i.e. 2**-1022
810 jra fp_nd_round | round.
811 | Exponent overflow. Just call it infinity.
816 fp_set_sr FPSR_EXC_INEX2
817 1: fp_set_sr FPSR_EXC_OVFL
818 move.w (FPD_RND,FPDATA),%d2
819 jne 3f | %d2 = 0 round to nearest
820 1: move.w #0x7fff,(-2,%a0)
824 printf PNORM,"%p(",1,%a0
829 jcs 5f | %d2 < 2, round to zero
830 jhi 4f | %d2 > 2, round to +infinity
831 tst.b (-3,%a0) | to -inf
834 4: tst.b (-3,%a0) | to +inf
836 5: move.w #0x43fe,(-2,%a0)
845 printf PNORM,"%p(",1,%a0
850 | fp_normalize_single:
851 | normalize an extended with single (23-bit) precision
852 | args: %a0 (struct fp_ext *)
855 printf PNORM,"ns: %p(",1,%a0
860 jeq fp_ns_zero | zero / denormalized
862 jeq fp_ns_huge | NaN / infinitive.
863 sub.w #0x4000-0x7f,%d2 | will the exponent fit?
864 jcs fp_ns_small | too small.
866 jcc fp_ns_large | too big.
867 move.l (%a0)+,%d0 | get high lword of mantissa
869 tst.l (%a0) | check the low lword
871 | Set a sticky bit if it is non-zero. This should only
872 | affect the rounding in what would otherwise be equal-
873 | distance situations, which is what we want it to do.
875 1: clr.l (%a0) | zap it from memory.
876 | now, round off the low 8 bits of the hi lword.
877 tst.b %d0 | 8 low bits.
878 jne fp_ns_checkround | Are they non-zero?
881 printf PNORM,"%p(",1,%a0
886 fp_set_sr FPSR_EXC_INEX2 | INEX2 bit
887 clr.b -(%a0) | clear low byte of high lword
889 move.w (FPD_RND,FPDATA),%d2 | rounding mode
890 jne 2f | %d2 == 0, round to nearest
891 tst.b %d0 | test guard bit
892 jpl 9f | zero is closer
893 btst #8,%d0 | test lsb bit
894 | round to even behaviour, see above.
895 jne fp_ns_doroundup | round to infinity
896 lsl.b #1,%d0 | check low bits
897 jeq 9f | round to zero
899 | round (the mantissa, that is) towards infinity
901 jcc 9f | no overflow, good.
902 | Overflow. This means that the %d1 was 0xffffff00, so it
903 | is now zero. We will set the mantissa to reflect this, and
904 | increment the exponent (checking for overflow there too)
907 cmp.w #0x407f,(%a0)+ | exponent now overflown?
908 jeq fp_ns_large | yes, so make it infinity.
910 printf PNORM,"%p(",1,%a0
914 | check nondefault rounding modes
916 jcs 9b | %d2 < 2, round to zero
917 jhi 3f | %d2 > 2, round to +infinity
918 tst.b (-3,%a0) | to -inf
919 jne fp_ns_doroundup | negative, round to infinity
920 jra 9b | positive, round to zero
921 3: tst.b (-3,%a0) | to +inf
922 jeq fp_ns_doroundup | positive, round to infinity
923 jra 9b | negative, round to zero
924 | Exponent underflow. Try to make a denormal, and set it to
925 | the smallest possible fraction if this fails.
927 fp_set_sr FPSR_EXC_UNFL | set UNFL bit
928 move.w #0x3f81,(-2,%a0) | 2**-126
929 neg.w %d2 | degree of underflow
930 cmp.w #32,%d2 | single or double shift?
937 | Check to see if we shifted off any significant bits.
942 bset #0,%d0 | Sticky bit.
943 | Check the lower lword
947 bset #0,%d0 | Sticky bit.
949 | Sorry, the number is just too small.
952 moveq #1,%d0 | Smallest possible fraction,
953 jra fp_ns_round | round as desired.
954 | Exponent overflow. Just call it infinity.
958 fp_set_sr FPSR_EXC_INEX2
959 1: fp_set_sr FPSR_EXC_OVFL
960 move.w (FPD_RND,FPDATA),%d2
961 jne 3f | %d2 = 0 round to nearest
962 1: move.w #0x7fff,(-2,%a0)
966 printf PNORM,"%p(",1,%a0
971 jcs 5f | %d2 < 2, round to zero
972 jhi 4f | %d2 > 2, round to +infinity
973 tst.b (-3,%a0) | to -inf
976 4: tst.b (-3,%a0) | to +inf
978 5: move.w #0x407e,(-2,%a0)
979 move.l #0xffffff00,(%a0)+
982 | zero and denormalized
989 printf PNORM,"%p(",1,%a0
992 rts | zero. nothing to do.
993 | These are not merely subnormal numbers, but true denormals,
994 | i.e. pathologically small (exponent is 2**-16383) numbers.
995 | It is clearly impossible for even a normal extended number
996 | with that exponent to fit into single precision, so just
997 | write these ones off as "too darn small".
998 1: fp_set_sr FPSR_EXC_UNFL | Set UNFL bit
1001 move.w #0x3f81,-(%a0) | i.e. 2**-126
1004 jra fp_ns_round | round.
1005 | Infinities or NaNs
1008 printf PNORM,"%p(",1,%a0
1013 | fp_normalize_single_fast:
1014 | normalize an extended with single (23-bit) precision
1015 | this is only used by fsgldiv/fsgdlmul, where the
1016 | operand is not completly normalized.
1017 | args: %a0 (struct fp_ext *)
1019 fp_normalize_single_fast:
1020 printf PNORM,"nsf: %p(",1,%a0
1026 jeq fp_nsf_huge | NaN / infinitive.
1027 move.l (%a0)+,%d0 | get high lword of mantissa
1029 tst.l (%a0) | check the low lword
1031 | Set a sticky bit if it is non-zero. This should only
1032 | affect the rounding in what would otherwise be equal-
1033 | distance situations, which is what we want it to do.
1035 1: clr.l (%a0) | zap it from memory.
1036 | now, round off the low 8 bits of the hi lword.
1037 tst.b %d0 | 8 low bits.
1038 jne fp_nsf_checkround | Are they non-zero?
1039 | nothing to do here
1041 printf PNORM,"%p(",1,%a0
1046 fp_set_sr FPSR_EXC_INEX2 | INEX2 bit
1047 clr.b -(%a0) | clear low byte of high lword
1049 move.w (FPD_RND,FPDATA),%d2 | rounding mode
1050 jne 2f | %d2 == 0, round to nearest
1051 tst.b %d0 | test guard bit
1052 jpl 9f | zero is closer
1053 btst #8,%d0 | test lsb bit
1054 | round to even behaviour, see above.
1055 jne fp_nsf_doroundup | round to infinity
1056 lsl.b #1,%d0 | check low bits
1057 jeq 9f | round to zero
1059 | round (the mantissa, that is) towards infinity
1061 jcc 9f | no overflow, good.
1062 | Overflow. This means that the %d1 was 0xffffff00, so it
1063 | is now zero. We will set the mantissa to reflect this, and
1064 | increment the exponent (checking for overflow there too)
1065 move.w #0x8000,(%a0)
1067 cmp.w #0x407f,(%a0)+ | exponent now overflown?
1068 jeq fp_nsf_large | yes, so make it infinity.
1070 printf PNORM,"%p(",1,%a0
1074 | check nondefault rounding modes
1076 jcs 9b | %d2 < 2, round to zero
1077 jhi 3f | %d2 > 2, round to +infinity
1078 tst.b (-3,%a0) | to -inf
1079 jne fp_nsf_doroundup | negative, round to infinity
1080 jra 9b | positive, round to zero
1081 3: tst.b (-3,%a0) | to +inf
1082 jeq fp_nsf_doroundup | positive, round to infinity
1083 jra 9b | negative, round to zero
1084 | Exponent overflow. Just call it infinity.
1088 fp_set_sr FPSR_EXC_INEX2
1089 1: fp_set_sr FPSR_EXC_OVFL
1090 move.w (FPD_RND,FPDATA),%d2
1091 jne 3f | %d2 = 0 round to nearest
1092 1: move.w #0x7fff,(-2,%a0)
1096 printf PNORM,"%p(",1,%a0
1101 jcs 5f | %d2 < 2, round to zero
1102 jhi 4f | %d2 > 2, round to +infinity
1103 tst.b (-3,%a0) | to -inf
1106 4: tst.b (-3,%a0) | to +inf
1108 5: move.w #0x407e,(-2,%a0)
1109 move.l #0xffffff00,(%a0)+
1112 | Infinities or NaNs
1115 printf PNORM,"%p(",1,%a0
1120 | conv_ext2int (macro):
1121 | Generates a subroutine that converts an extended value to an
1122 | integer of a given size, again, with the appropriate type of
1126 | s: size, as given in an assembly instruction.
1127 | b: number of bits in that size.
1129 | Subroutine arguments:
1130 | %a0: source (struct fp_ext *)
1132 | Returns the integer in %d0 (like it should)
1134 .macro conv_ext2int s,b
1135 .set inf,(1<<(\b-1))-1 | i.e. MAXINT
1136 printf PCONV,"e2i%d: %p(",2,#\b,%a0
1140 move.w (%a0)+,%d2 | exponent
1141 jeq fp_e2i_zero\b | zero / denorm (== 0, here)
1143 jeq fp_e2i_huge\b | Inf / NaN
1161 printf PCONV,"-> %p\n",1,%d0
1166 1: printf PCONV,"-> %p\n",1,%d0
1169 fp_set_sr FPSR_EXC_INEX2 | INEX2 bit
1176 move.w (FPD_RND,FPDATA),%d2 | rounding mode
1177 jne 2f | %d2 == 0, round to nearest
1178 tst.l %d1 | test guard bit
1179 jpl 9b | zero is closer
1180 btst %d2,%d0 | test lsb bit (%d2 still 0)
1181 jne fp_e2i_doroundup\b
1182 lsl.l #1,%d1 | check low bits
1183 jne fp_e2i_doroundup\b
1189 | check nondefault rounding modes
1191 jcs 9b | %d2 < 2, round to zero
1192 jhi 3f | %d2 > 2, round to +infinity
1193 tst.w (-4,%a0) | to -inf
1194 jne fp_e2i_doroundup\b | negative, round to infinity
1195 jra 9b | positive, round to zero
1196 3: tst.w (-4,%a0) | to +inf
1197 jeq fp_e2i_doroundup\b | positive, round to infinity
1198 jra 9b | negative, round to zero
1199 | we are only want -2**127 get correctly rounded here,
1200 | since the guard bit is in the lower lword.
1201 | everything else ends up anyway as overflow.
1203 5: move.w (FPD_RND,FPDATA),%d2 | rounding mode
1204 jne 2b | %d2 == 0, round to nearest
1205 move.l (4,%a0),%d1 | test guard bit
1206 jpl 9b | zero is closer
1207 lsl.l #1,%d1 | check low bits
1208 jne fp_e2i_doroundup\b
1218 fp_clr_sr FPSR_EXC_UNFL | fp_normalize_ext has set this bit
1220 fp_set_sr FPSR_EXC_INEX2
1222 move.w (FPD_RND,FPDATA),%d2 | rounding mode
1224 jcs 3f | %d2 < 2, round to nearest/zero
1225 jhi 2f | %d2 > 2, round to +infinity
1226 tst.w (-4,%a0) | to -inf
1230 2: tst.w (-4,%a0) | to +inf
1233 3: printf PCONV,"-> %p\n",1,%d0
1236 fp_set_sr FPSR_EXC_OPERR
1241 1: printf PCONV,"-> %p\n",1,%d0
1249 | fp_normalize_ext has set this bit already
1250 | and made the number nonsignaling
1251 1: fp_tst_sr FPSR_EXC_SNAN
1253 fp_set_sr FPSR_EXC_OPERR
1254 1: printf PCONV,"-> %p\n",1,%d0
1268 jsr fp_normalize_double
1269 printf PCONV,"e2d: %p(",1,%a0
1278 1: sub.w #0x3fff-0x3ff,%d2
1290 putuser.l %d0,(%a1)+,fp_err_ua2,%a1
1297 putuser.l %d0,(%a1),fp_err_ua2,%a1
1298 #ifdef FPU_EMU_DEBUG
1299 getuser.l %a1@(-4),%d0,fp_err_ua2,%a1
1300 getuser.l %a1@(0),%d1,fp_err_ua2,%a1
1301 printf PCONV,"%p(%08x%08x)\n",3,%a1,%d0,%d1
1306 jsr fp_normalize_single
1307 printf PCONV,"e2s: %p(",1,%a0
1316 1: sub.w #0x3fff-0x7f,%d1
1326 printf PCONV,"%08x\n",1,%d0
1329 | special return addresses for instr that
1330 | encode the rounding precision in the opcode
1331 | (e.g. fsmove,fdmove)
1333 fp_finalrounding_single:
1335 jsr fp_normalize_ext
1336 jsr fp_normalize_single
1339 fp_finalrounding_single_fast:
1341 jsr fp_normalize_ext
1342 jsr fp_normalize_single_fast
1345 fp_finalrounding_double:
1347 jsr fp_normalize_ext
1348 jsr fp_normalize_double
1352 | set the emulated status register based on the outcome of an
1353 | emulated instruction.
1357 | printf ,"f: %p\n",1,%a0
1358 jsr fp_normalize_ext
1359 move.w (FPD_PREC,FPDATA),%d0
1363 jsr fp_normalize_single
1365 1: jsr fp_normalize_double
1366 2:| printf ,"f: %p\n",1,%a0
1368 | First, we do some of the obvious tests for the exception
1369 | status byte and condition code bytes of fp_sr here, so that
1370 | they do not have to be handled individually by every
1371 | emulated instruction.
1376 bset #FPSR_CC_NEG-24,%d0 | N bit
1377 1: cmp.w #0x7fff,(%a0)+ | exponent
1380 moveq #FPSR_CC_Z-24,%d1
1387 2: moveq #FPSR_CC_NAN-24,%d1
1389 lsl.l #1,%d2 | ignore high bit
1393 moveq #FPSR_CC_INF-24,%d1
1395 9: move.b %d0,(FPD_FPSR+0,FPDATA) | set condition test result
1396 | move instructions enter here
1397 | Here, we test things in the exception status byte, and set
1398 | other things in the accrued exception byte accordingly.
1399 | Emulated instructions can set various things in the former,
1400 | as defined in fp_emu.h.
1402 move.l (FPD_FPSR,FPDATA),%d0
1404 btst #FPSR_EXC_SNAN,%d0 | EXC_SNAN
1406 btst #FPSR_EXC_OPERR,%d0 | EXC_OPERR
1408 1: bset #FPSR_AEXC_IOP,%d0 | set IOP bit
1409 2: btst #FPSR_EXC_OVFL,%d0 | EXC_OVFL
1411 bset #FPSR_AEXC_OVFL,%d0 | set OVFL bit
1412 1: btst #FPSR_EXC_UNFL,%d0 | EXC_UNFL
1414 btst #FPSR_EXC_INEX2,%d0 | EXC_INEX2
1416 bset #FPSR_AEXC_UNFL,%d0 | set UNFL bit
1417 1: btst #FPSR_EXC_DZ,%d0 | EXC_INEX1
1419 bset #FPSR_AEXC_DZ,%d0 | set DZ bit
1420 1: btst #FPSR_EXC_OVFL,%d0 | EXC_OVFL
1422 btst #FPSR_EXC_INEX2,%d0 | EXC_INEX2
1424 btst #FPSR_EXC_INEX1,%d0 | EXC_INEX1
1426 1: bset #FPSR_AEXC_INEX,%d0 | set INEX bit
1427 2: move.l %d0,(FPD_FPSR,FPDATA)
1429 | same as above, greatly optimized, but untested (yet)
1449 move.l %d2,(FPD_FPSR,FPDATA)
1451 move.b (FPD_FPSR+2,FPDATA),%d0
1452 and.b (FPD_FPCR+2,FPDATA),%d0
1454 printf ,"send signal!!!\n"