1 /* libgcc routines for 68000 w/o floating-point hardware.
2 Copyright (C) 1994, 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file with other programs, and to distribute
14 those programs without any restriction coming from the use of this
15 file. (The General Public License restrictions do apply in other
16 respects; for example, they cover modification of the file, and
17 distribution when not linked into another program.)
19 This file is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; see the file COPYING. If not, write to
26 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
27 Boston, MA 02110-1301, USA. */
29 /* As a special exception, if you link this library with files
30 compiled with GCC to produce an executable, this does not cause
31 the resulting executable to be covered by the GNU General Public License.
32 This exception does not however invalidate any other reasons why
33 the executable file might be covered by the GNU General Public License. */
35 /* Use this one for any 680x0; assumes no floating point hardware.
36 The trailing " '" appearing on some lines is for ANSI preprocessors. Yuk.
37 Some of this code comes from MINIX, via the folks at ericsson.
38 D. V. Henkel-Wallace (gumby@cygnus.com) Fete Bastille, 1992
41 /* These are predefined by new versions of GNU cpp. */
43 #ifndef __USER_LABEL_PREFIX__
44 #define __USER_LABEL_PREFIX__ _
47 #ifndef __REGISTER_PREFIX__
48 #define __REGISTER_PREFIX__
51 #ifndef __IMMEDIATE_PREFIX__
52 #define __IMMEDIATE_PREFIX__ #
55 /* ANSI concatenation macros. */
57 #define CONCAT1(a, b) CONCAT2(a, b)
58 #define CONCAT2(a, b) a ## b
60 /* Use the right prefix for global labels. */
62 #define SYM(x) CONCAT1 (__USER_LABEL_PREFIX__, x)
64 /* Note that X is a function. */
67 #define FUNC(x) .type SYM(x),function
69 /* The .proc pseudo-op is accepted, but ignored, by GAS. We could just
70 define this to the empty string for non-ELF systems, but defining it
71 to .proc means that the information is available to the assembler if
76 /* Use the right prefix for registers. */
78 #define REG(x) CONCAT1 (__REGISTER_PREFIX__, x)
80 /* Use the right prefix for immediate values. */
82 #define IMM(x) CONCAT1 (__IMMEDIATE_PREFIX__, x)
103 /* Provide a few macros to allow for PIC code support.
104 * With PIC, data is stored A5 relative so we've got to take a bit of special
105 * care to ensure that all loads of global data is via A5. PIC also requires
106 * jumps and subroutine calls to be PC relative rather than absolute. We cheat
107 * a little on this and in the PIC case, we use short offset branches and
108 * hope that the final object code is within range (which it should be).
112 /* Non PIC (absolute/relocatable) versions */
122 .macro PICLEA sym, reg
126 .macro PICPEA sym, areg
132 /* Common for Linux and uClinux, the latter with either
133 -mid-shared-library or -msep-data. */
136 #if defined (__mcoldfire__) && !defined (__mcfisab__) && !defined (__mcfisac__)
145 /* ISA C has no bra.l instruction, and since this assembly file
146 gets assembled into multiple object files, we avoid the
147 bra instruction entirely. */
148 #if defined (__mcoldfire__) && !defined (__mcfisab__)
156 # if defined (__uClinux__)
158 /* Versions for uClinux */
160 # if defined(__ID_SHARED_LIBRARY__)
162 /* -mid-shared-library versions */
164 .macro PICLEA sym, reg
165 movel a5@(_current_shared_library_a5_offset_), \reg
166 movel \sym@GOT(\reg), \reg
169 .macro PICPEA sym, areg
170 movel a5@(_current_shared_library_a5_offset_), \areg
171 movel \sym@GOT(\areg), sp@-
174 # else /* !__ID_SHARED_LIBRARY__ */
176 /* Versions for -msep-data */
178 .macro PICLEA sym, reg
179 movel \sym@GOT(a5), \reg
182 .macro PICPEA sym, areg
183 movel \sym@GOT(a5), sp@-
188 # else /* !__uClinux__ */
190 /* Versions for Linux */
192 .macro PICLEA sym, reg
193 movel #_GLOBAL_OFFSET_TABLE_@GOTPC, \reg
194 lea (-6, pc, \reg), \reg
195 movel \sym@GOT(\reg), \reg
198 .macro PICPEA sym, areg
199 movel #_GLOBAL_OFFSET_TABLE_@GOTPC, \areg
200 lea (-6, pc, \areg), \areg
201 movel \sym@GOT(\areg), sp@-
210 | This is an attempt at a decent floating point (single, double and
211 | extended double) code for the GNU C compiler. It should be easy to
212 | adapt to other compilers (but beware of the local labels!).
214 | Starting date: 21 October, 1990
216 | It is convenient to introduce the notation (s,e,f) for a floating point
217 | number, where s=sign, e=exponent, f=fraction. We will call a floating
218 | point number fpn to abbreviate, independently of the precision.
219 | Let MAX_EXP be in each case the maximum exponent (255 for floats, 1023
220 | for doubles and 16383 for long doubles). We then have the following
222 | 1. Normalized fpns have 0 < e < MAX_EXP. They correspond to
223 | (-1)^s x 1.f x 2^(e-bias-1).
224 | 2. Denormalized fpns have e=0. They correspond to numbers of the form
225 | (-1)^s x 0.f x 2^(-bias).
226 | 3. +/-INFINITY have e=MAX_EXP, f=0.
227 | 4. Quiet NaN (Not a Number) have all bits set.
228 | 5. Signaling NaN (Not a Number) have s=0, e=MAX_EXP, f=1.
230 |=============================================================================
232 |=============================================================================
234 | This is the floating point condition code register (_fpCCR):
237 | short _exception_bits;
238 | short _trap_enable_bits;
239 | short _sticky_bits;
240 | short _rounding_mode;
242 | short _last_operation;
266 .word ROUND_TO_NEAREST
279 EBITS = __exception_bits - SYM (_fpCCR)
280 TRAPE = __trap_enable_bits - SYM (_fpCCR)
281 STICK = __sticky_bits - SYM (_fpCCR)
282 ROUND = __rounding_mode - SYM (_fpCCR)
283 FORMT = __format - SYM (_fpCCR)
284 LASTO = __last_operation - SYM (_fpCCR)
285 OPER1 = __operand1 - SYM (_fpCCR)
286 OPER2 = __operand2 - SYM (_fpCCR)
288 | The following exception types are supported:
289 INEXACT_RESULT = 0x0001
292 DIVIDE_BY_ZERO = 0x0008
293 INVALID_OPERATION = 0x0010
295 | The allowed rounding modes are:
297 ROUND_TO_NEAREST = 0 | round result to nearest representable value
298 ROUND_TO_ZERO = 1 | round result towards zero
299 ROUND_TO_PLUS = 2 | round result towards plus infinity
300 ROUND_TO_MINUS = 3 | round result towards minus infinity
302 | The allowed values of format are:
308 | The allowed values for the last operation are:
318 |=============================================================================
319 | __clear_sticky_bits
320 |=============================================================================
322 | The sticky bits are normally not cleared (thus the name), whereas the
323 | exception type and exception value reflect the last computation.
324 | This routine is provided to clear them (you can also write to _fpCCR,
325 | since it is globally visible).
327 .globl SYM (__clear_sticky_bit)
332 | void __clear_sticky_bits(void);
333 SYM (__clear_sticky_bit):
334 PICLEA SYM (_fpCCR),a0
335 #ifndef __mcoldfire__
336 movew IMM (0),a0@(STICK)
342 |=============================================================================
343 | $_exception_handler
344 |=============================================================================
346 .globl $_exception_handler
351 | This is the common exit point if an exception occurs.
352 | NOTE: it is NOT callable from C!
353 | It expects the exception type in d7, the format (SINGLE_FLOAT,
354 | DOUBLE_FLOAT or LONG_FLOAT) in d6, and the last operation code in d5.
355 | It sets the corresponding exception and sticky bits, and the format.
356 | Depending on the format if fills the corresponding slots for the
357 | operands which produced the exception (all this information is provided
358 | so if you write your own exception handlers you have enough information
359 | to deal with the problem).
360 | Then checks to see if the corresponding exception is trap-enabled,
361 | in which case it pushes the address of _fpCCR and traps through
362 | trap FPTRAP (15 for the moment).
367 PICLEA SYM (_fpCCR),a0
368 movew d7,a0@(EBITS) | set __exception_bits
369 #ifndef __mcoldfire__
370 orw d7,a0@(STICK) | and __sticky_bits
376 movew d6,a0@(FORMT) | and __format
377 movew d5,a0@(LASTO) | and __last_operation
379 | Now put the operands in place:
380 #ifndef __mcoldfire__
381 cmpw IMM (SINGLE_FLOAT),d6
383 cmpl IMM (SINGLE_FLOAT),d6
386 movel a6@(8),a0@(OPER1)
387 movel a6@(12),a0@(OPER1+4)
388 movel a6@(16),a0@(OPER2)
389 movel a6@(20),a0@(OPER2+4)
391 1: movel a6@(8),a0@(OPER1)
392 movel a6@(12),a0@(OPER2)
394 | And check whether the exception is trap-enabled:
395 #ifndef __mcoldfire__
396 andw a0@(TRAPE),d7 | is exception trap-enabled?
403 PICPEA SYM (_fpCCR),a1 | yes, push address of _fpCCR
404 trap IMM (FPTRAP) | and trap
405 #ifndef __mcoldfire__
406 1: moveml sp@+,d2-d7 | restore data registers
409 | XXX if frame pointer is ever removed, stack pointer must
414 #endif /* L_floatex */
419 .globl SYM (__mulsi3)
421 movew sp@(4), d0 /* x0 -> d0 */
422 muluw sp@(10), d0 /* x0*y1 */
423 movew sp@(6), d1 /* x1 -> d1 */
424 muluw sp@(8), d1 /* x1*y0 */
425 #ifndef __mcoldfire__
432 movew sp@(6), d1 /* x1 -> d1 */
433 muluw sp@(10), d1 /* x1*y1 */
437 #endif /* L_mulsi3 */
442 .globl SYM (__udivsi3)
444 #ifndef __mcoldfire__
446 movel sp@(12), d1 /* d1 = divisor */
447 movel sp@(8), d0 /* d0 = dividend */
449 cmpl IMM (0x10000), d1 /* divisor >= 2 ^ 16 ? */
450 jcc L3 /* then try next algorithm */
454 divu d1, d2 /* high quotient in lower word */
455 movew d2, d0 /* save high quotient */
457 movew sp@(10), d2 /* get low dividend + high rest */
458 divu d1, d2 /* low quotient */
462 L3: movel d1, d2 /* use d2 as divisor backup */
463 L4: lsrl IMM (1), d1 /* shift divisor */
464 lsrl IMM (1), d0 /* shift dividend */
465 cmpl IMM (0x10000), d1 /* still divisor >= 2 ^ 16 ? */
467 divu d1, d0 /* now we have 16-bit divisor */
468 andl IMM (0xffff), d0 /* mask out divisor, ignore remainder */
470 /* Multiply the 16-bit tentative quotient with the 32-bit divisor. Because of
471 the operand ranges, this might give a 33-bit product. If this product is
472 greater than the dividend, the tentative quotient was too large. */
474 mulu d0, d1 /* low part, 32 bits */
476 mulu d0, d2 /* high part, at most 17 bits */
477 swap d2 /* align high part with low part */
478 tstw d2 /* high part 17 bits? */
479 jne L5 /* if 17 bits, quotient was too large */
480 addl d2, d1 /* add parts */
481 jcs L5 /* if sum is 33 bits, quotient was too large */
482 cmpl sp@(8), d1 /* compare the sum with the dividend */
483 jls L6 /* if sum > dividend, quotient was too large */
484 L5: subql IMM (1), d0 /* adjust quotient */
489 #else /* __mcoldfire__ */
491 /* ColdFire implementation of non-restoring division algorithm from
492 Hennessy & Patterson, Appendix A. */
499 L1: addl d0,d0 | shift reg pair (p,a) one bit left
501 movl d2,d3 | subtract b from p, store in tmp.
503 jcs L2 | if no carry,
504 bset IMM (0),d0 | set the low order bit of a to 1,
505 movl d3,d2 | and store tmp in p.
508 moveml sp@,d2-d4 | restore data registers
511 #endif /* __mcoldfire__ */
513 #endif /* L_udivsi3 */
518 .globl SYM (__divsi3)
522 moveq IMM (1), d2 /* sign of result stored in d2 (=1 or =-1) */
523 movel sp@(12), d1 /* d1 = divisor */
526 #ifndef __mcoldfire__
527 negb d2 /* change sign because divisor <0 */
529 negl d2 /* change sign because divisor <0 */
531 L1: movel sp@(8), d0 /* d0 = dividend */
534 #ifndef __mcoldfire__
542 PICCALL SYM (__udivsi3) /* divide abs(dividend) by abs(divisor) */
551 #endif /* L_divsi3 */
556 .globl SYM (__umodsi3)
558 movel sp@(8), d1 /* d1 = divisor */
559 movel sp@(4), d0 /* d0 = dividend */
562 PICCALL SYM (__udivsi3)
564 movel sp@(8), d1 /* d1 = divisor */
565 #ifndef __mcoldfire__
568 PICCALL SYM (__mulsi3) /* d0 = (a/b)*b */
573 movel sp@(4), d1 /* d1 = dividend */
574 subl d0, d1 /* d1 = a - (a/b)*b */
577 #endif /* L_umodsi3 */
582 .globl SYM (__modsi3)
584 movel sp@(8), d1 /* d1 = divisor */
585 movel sp@(4), d0 /* d0 = dividend */
588 PICCALL SYM (__divsi3)
590 movel sp@(8), d1 /* d1 = divisor */
591 #ifndef __mcoldfire__
594 PICCALL SYM (__mulsi3) /* d0 = (a/b)*b */
599 movel sp@(4), d1 /* d1 = dividend */
600 subl d0, d1 /* d1 = a - (a/b)*b */
603 #endif /* L_modsi3 */
609 .globl $_exception_handler
611 QUIET_NaN = 0xffffffff
615 DBL_MAX_EXP = D_MAX_EXP - D_BIAS
616 DBL_MIN_EXP = 1 - D_BIAS
619 INEXACT_RESULT = 0x0001
622 DIVIDE_BY_ZERO = 0x0008
623 INVALID_OPERATION = 0x0010
637 ROUND_TO_NEAREST = 0 | round result to nearest representable value
638 ROUND_TO_ZERO = 1 | round result towards zero
639 ROUND_TO_PLUS = 2 | round result towards plus infinity
640 ROUND_TO_MINUS = 3 | round result towards minus infinity
644 .globl SYM (__adddf3)
645 .globl SYM (__subdf3)
646 .globl SYM (__muldf3)
647 .globl SYM (__divdf3)
648 .globl SYM (__negdf2)
649 .globl SYM (__cmpdf2)
650 .globl SYM (__cmpdf2_internal)
655 | These are common routines to return and signal exceptions.
658 | Return and signal a denormalized number
660 movew IMM (INEXACT_RESULT+UNDERFLOW),d7
661 moveq IMM (DOUBLE_FLOAT),d6
662 PICJUMP $_exception_handler
666 | Return a properly signed INFINITY and set the exception flags
667 movel IMM (0x7ff00000),d0
670 movew IMM (INEXACT_RESULT+OVERFLOW),d7
671 moveq IMM (DOUBLE_FLOAT),d6
672 PICJUMP $_exception_handler
675 | Return 0 and set the exception flags
678 movew IMM (INEXACT_RESULT+UNDERFLOW),d7
679 moveq IMM (DOUBLE_FLOAT),d6
680 PICJUMP $_exception_handler
683 | Return a quiet NaN and set the exception flags
684 movel IMM (QUIET_NaN),d0
686 movew IMM (INEXACT_RESULT+INVALID_OPERATION),d7
687 moveq IMM (DOUBLE_FLOAT),d6
688 PICJUMP $_exception_handler
691 | Return a properly signed INFINITY and set the exception flags
692 movel IMM (0x7ff00000),d0
695 movew IMM (INEXACT_RESULT+DIVIDE_BY_ZERO),d7
696 moveq IMM (DOUBLE_FLOAT),d6
697 PICJUMP $_exception_handler
699 |=============================================================================
700 |=============================================================================
701 | double precision routines
702 |=============================================================================
703 |=============================================================================
705 | A double precision floating point number (double) has the format:
708 | unsigned int sign : 1; /* sign bit */
709 | unsigned int exponent : 11; /* exponent, shifted by 126 */
710 | unsigned int fraction : 52; /* fraction */
713 | Thus sizeof(double) = 8 (64 bits).
715 | All the routines are callable from C programs, and return the result
716 | in the register pair d0-d1. They also preserve all registers except
719 |=============================================================================
721 |=============================================================================
723 | double __subdf3(double, double);
726 bchg IMM (31),sp@(12) | change sign of second operand
727 | and fall through, so we always add
728 |=============================================================================
730 |=============================================================================
732 | double __adddf3(double, double);
735 #ifndef __mcoldfire__
736 link a6,IMM (0) | everything will be done in registers
737 moveml d2-d7,sp@- | save all data registers and a2 (but d0-d1)
742 movel a6@(8),d0 | get first operand
744 movel a6@(16),d2 | get second operand
747 movel d0,d7 | get d0's sign bit in d7 '
748 addl d1,d1 | check and clear sign bit of a, and gain one
749 addxl d0,d0 | bit of extra precision
750 beq Ladddf$b | if zero return second operand
752 movel d2,d6 | save sign in d6
753 addl d3,d3 | get rid of sign bit and gain one bit of
754 addxl d2,d2 | extra precision
755 beq Ladddf$a | if zero return first operand
757 andl IMM (0x80000000),d7 | isolate a's sign bit '
758 swap d6 | and also b's sign bit '
759 #ifndef __mcoldfire__
760 andw IMM (0x8000),d6 |
761 orw d6,d7 | and combine them into d7, so that a's sign '
762 | bit is in the high word and b's is in the '
763 | low word, so d6 is free to be used
768 movel d7,a0 | now save d7 into a0, so d7 is free to
771 | Get the exponents and check for denormalized and/or infinity.
773 movel IMM (0x001fffff),d6 | mask for the fraction
774 movel IMM (0x00200000),d7 | mask to put hidden bit back
777 andl d6,d0 | get fraction in d0
778 notl d6 | make d6 into mask for the exponent
779 andl d6,d4 | get exponent in d4
780 beq Ladddf$a$den | branch if a is denormalized
781 cmpl d6,d4 | check for INFINITY or NaN
783 orl d7,d0 | and put hidden bit back
785 swap d4 | shift right exponent so that it starts
786 #ifndef __mcoldfire__
787 lsrw IMM (5),d4 | in bit 0 and not bit 20
789 lsrl IMM (5),d4 | in bit 0 and not bit 20
791 | Now we have a's exponent in d4 and fraction in d0-d1 '
792 movel d2,d5 | save b to get exponent
793 andl d6,d5 | get exponent in d5
794 beq Ladddf$b$den | branch if b is denormalized
795 cmpl d6,d5 | check for INFINITY or NaN
797 notl d6 | make d6 into mask for the fraction again
798 andl d6,d2 | and get fraction in d2
799 orl d7,d2 | and put hidden bit back
801 swap d5 | shift right exponent so that it starts
802 #ifndef __mcoldfire__
803 lsrw IMM (5),d5 | in bit 0 and not bit 20
805 lsrl IMM (5),d5 | in bit 0 and not bit 20
808 | Now we have b's exponent in d5 and fraction in d2-d3. '
810 | The situation now is as follows: the signs are combined in a0, the
811 | numbers are in d0-d1 (a) and d2-d3 (b), and the exponents in d4 (a)
812 | and d5 (b). To do the rounding correctly we need to keep all the
813 | bits until the end, so we need to use d0-d1-d2-d3 for the first number
814 | and d4-d5-d6-d7 for the second. To do this we store (temporarily) the
815 | exponents in a2-a3.
817 #ifndef __mcoldfire__
818 moveml a2-a3,sp@- | save the address registers
825 movel d4,a2 | save the exponents
828 movel IMM (0),d7 | and move the numbers around
835 | Here we shift the numbers until the exponents are the same, and put
836 | the largest exponent in a2.
837 #ifndef __mcoldfire__
838 exg d4,a2 | get exponents back
840 cmpw d4,d5 | compare the exponents
842 movel d4,a4 | get exponents back
848 cmpl d4,d5 | compare the exponents
850 beq Ladddf$3 | if equal don't shift '
851 bhi 9f | branch if second exponent is higher
853 | Here we have a's exponent larger than b's, so we have to shift b. We do
854 | this by using as counter d2:
855 1: movew d4,d2 | move largest exponent to d2
856 #ifndef __mcoldfire__
857 subw d5,d2 | and subtract second exponent
858 exg d4,a2 | get back the longs we saved
861 subl d5,d2 | and subtract second exponent
862 movel d4,a4 | get back the longs we saved
869 | if difference is too large we don't shift (actually, we can just exit) '
870 #ifndef __mcoldfire__
871 cmpw IMM (DBL_MANT_DIG+2),d2
873 cmpl IMM (DBL_MANT_DIG+2),d2
876 #ifndef __mcoldfire__
877 cmpw IMM (32),d2 | if difference >= 32, shift by longs
879 cmpl IMM (32),d2 | if difference >= 32, shift by longs
883 #ifndef __mcoldfire__
884 cmpw IMM (16),d2 | if difference >= 16, shift by words
886 cmpl IMM (16),d2 | if difference >= 16, shift by words
889 bra 3f | enter dbra loop
892 #ifndef __mcoldfire__
913 #ifndef __mcoldfire__
927 #ifndef __mcoldfire__
942 #ifndef __mcoldfire__
950 #ifndef __mcoldfire__
953 subw d5,d6 | keep d5 (largest exponent) in d4
968 | if difference is too large we don't shift (actually, we can just exit) '
969 #ifndef __mcoldfire__
970 cmpw IMM (DBL_MANT_DIG+2),d6
972 cmpl IMM (DBL_MANT_DIG+2),d6
975 #ifndef __mcoldfire__
976 cmpw IMM (32),d6 | if difference >= 32, shift by longs
978 cmpl IMM (32),d6 | if difference >= 32, shift by longs
982 #ifndef __mcoldfire__
983 cmpw IMM (16),d6 | if difference >= 16, shift by words
985 cmpl IMM (16),d6 | if difference >= 16, shift by words
988 bra 3f | enter dbra loop
991 #ifndef __mcoldfire__
1012 #ifndef __mcoldfire__
1026 #ifndef __mcoldfire__
1041 #ifndef __mcoldfire__
1048 #ifndef __mcoldfire__
1060 | Now we have the numbers in d0--d3 and d4--d7, the exponent in a2, and
1063 | Here we have to decide whether to add or subtract the numbers:
1064 #ifndef __mcoldfire__
1065 exg d7,a0 | get the signs
1066 exg d6,a3 | a3 is free to be used
1076 movew IMM (0),d7 | get a's sign in d7 '
1078 movew IMM (0),d6 | and b's sign in d6 '
1079 eorl d7,d6 | compare the signs
1080 bmi Lsubdf$0 | if the signs are different we have
1082 #ifndef __mcoldfire__
1083 exg d7,a0 | else we add the numbers
1098 movel a2,d4 | return exponent to d4
1100 andl IMM (0x80000000),d7 | d7 now has the sign
1102 #ifndef __mcoldfire__
1110 | Before rounding normalize so bit #DBL_MANT_DIG is set (we will consider
1111 | the case of denormalized numbers in the rounding routine itself).
1112 | As in the addition (not in the subtraction!) we could have set
1113 | one more bit we check this:
1114 btst IMM (DBL_MANT_DIG+1),d0
1116 #ifndef __mcoldfire__
1139 lea pc@(Ladddf$5),a0 | to return from rounding routine
1140 PICLEA SYM (_fpCCR),a1 | check the rounding mode
1141 #ifdef __mcoldfire__
1144 movew a1@(6),d6 | rounding mode in d6
1145 beq Lround$to$nearest
1146 #ifndef __mcoldfire__
1147 cmpw IMM (ROUND_TO_PLUS),d6
1149 cmpl IMM (ROUND_TO_PLUS),d6
1155 | Put back the exponent and check for overflow
1156 #ifndef __mcoldfire__
1157 cmpw IMM (0x7ff),d4 | is the exponent big?
1159 cmpl IMM (0x7ff),d4 | is the exponent big?
1162 bclr IMM (DBL_MANT_DIG-1),d0
1163 #ifndef __mcoldfire__
1164 lslw IMM (4),d4 | put exponent back into position
1166 lsll IMM (4),d4 | put exponent back into position
1169 #ifndef __mcoldfire__
1181 | Here we do the subtraction.
1182 #ifndef __mcoldfire__
1183 exg d7,a0 | put sign back in a0
1197 beq Ladddf$ret$1 | if zero just exit
1198 bpl 1f | if positive skip the following
1200 bchg IMM (31),d7 | change sign bit in d7
1204 negxl d1 | and negate result
1207 movel a2,d4 | return exponent to d4
1209 andl IMM (0x80000000),d7 | isolate sign bit
1210 #ifndef __mcoldfire__
1218 | Before rounding normalize so bit #DBL_MANT_DIG is set (we will consider
1219 | the case of denormalized numbers in the rounding routine itself).
1220 | As in the addition (not in the subtraction!) we could have set
1221 | one more bit we check this:
1222 btst IMM (DBL_MANT_DIG+1),d0
1224 #ifndef __mcoldfire__
1247 lea pc@(Lsubdf$1),a0 | to return from rounding routine
1248 PICLEA SYM (_fpCCR),a1 | check the rounding mode
1249 #ifdef __mcoldfire__
1252 movew a1@(6),d6 | rounding mode in d6
1253 beq Lround$to$nearest
1254 #ifndef __mcoldfire__
1255 cmpw IMM (ROUND_TO_PLUS),d6
1257 cmpl IMM (ROUND_TO_PLUS),d6
1263 | Put back the exponent and sign (we don't have overflow). '
1264 bclr IMM (DBL_MANT_DIG-1),d0
1265 #ifndef __mcoldfire__
1266 lslw IMM (4),d4 | put exponent back into position
1268 lsll IMM (4),d4 | put exponent back into position
1271 #ifndef __mcoldfire__
1279 | If one of the numbers was too small (difference of exponents >=
1280 | DBL_MANT_DIG+1) we return the other (and now we don't have to '
1281 | check for finiteness or zero).
1283 #ifndef __mcoldfire__
1292 PICLEA SYM (_fpCCR),a0
1294 #ifndef __mcoldfire__
1295 moveml sp@+,d2-d7 | restore data registers
1298 | XXX if frame pointer is ever removed, stack pointer must
1301 unlk a6 | and return
1305 #ifndef __mcoldfire__
1314 PICLEA SYM (_fpCCR),a0
1316 #ifndef __mcoldfire__
1317 moveml sp@+,d2-d7 | restore data registers
1320 | XXX if frame pointer is ever removed, stack pointer must
1323 unlk a6 | and return
1327 movel d7,d4 | d7 contains 0x00200000
1331 movel d7,d5 | d7 contains 0x00200000
1336 | Return b (if a is zero)
1339 bne 1f | Check if b is -0
1340 cmpl IMM (0x80000000),d0
1342 andl IMM (0x80000000),d7 | Use the sign of a
1350 | Check for NaN and +/-INFINITY.
1352 andl IMM (0x80000000),d7 |
1354 cmpl IMM (0x7ff00000),d0 |
1356 movel d0,d0 | check for zero, since we don't '
1357 bne Ladddf$ret | want to return -0 by mistake
1361 andl IMM (0x000fffff),d0 | check for NaN (nonzero fraction)
1367 #ifndef __mcoldfire__
1368 moveml sp@+,a2-a3 | restore regs and exit
1377 PICLEA SYM (_fpCCR),a0
1379 orl d7,d0 | put sign bit back
1380 #ifndef __mcoldfire__
1384 | XXX if frame pointer is ever removed, stack pointer must
1391 | Return a denormalized number.
1392 #ifndef __mcoldfire__
1393 lsrl IMM (1),d0 | shift right once more
1406 | This could be faster but it is not worth the effort, since it is not
1407 | executed very often. We sacrifice speed for clarity here.
1408 movel a6@(8),d0 | get the numbers back (remember that we
1409 movel a6@(12),d1 | did some processing already)
1412 movel IMM (0x7ff00000),d4 | useful constant (INFINITY)
1413 movel d0,d7 | save sign bits
1415 bclr IMM (31),d0 | clear sign bits
1417 | We know that one of them is either NaN of +/-INFINITY
1418 | Check for NaN (if either one is NaN return NaN)
1419 cmpl d4,d0 | check first a (d0)
1420 bhi Ld$inop | if d0 > 0x7ff00000 or equal and
1422 tstl d1 | d1 > 0, a is NaN
1424 2: cmpl d4,d2 | check now b (d1)
1430 | Now comes the check for +/-INFINITY. We know that both are (maybe not
1431 | finite) numbers, but we have to check if both are infinite whether we
1432 | are adding or subtracting them.
1433 eorl d7,d6 | to check sign bits
1435 andl IMM (0x80000000),d7 | get (common) sign bit
1438 | We know one (or both) are infinite, so we test for equality between the
1439 | two numbers (if they are equal they have to be infinite both, so we
1441 cmpl d2,d0 | are both infinite?
1442 bne 1f | if d0 <> d2 they are not equal
1443 cmpl d3,d1 | if d0 == d2 test d3 and d1
1444 beq Ld$inop | if equal return NaN
1446 andl IMM (0x80000000),d7 | get a's sign bit '
1447 cmpl d4,d0 | test now for infinity
1448 beq Ld$infty | if a is INFINITY return with this sign
1449 bchg IMM (31),d7 | else we know b is INFINITY and has
1450 bra Ld$infty | the opposite sign
1452 |=============================================================================
1454 |=============================================================================
1456 | double __muldf3(double, double);
1459 #ifndef __mcoldfire__
1466 movel a6@(8),d0 | get a into d0-d1
1468 movel a6@(16),d2 | and b into d2-d3
1470 movel d0,d7 | d7 will hold the sign of the product
1472 andl IMM (0x80000000),d7 |
1473 movel d7,a0 | save sign bit into a0
1474 movel IMM (0x7ff00000),d7 | useful constant (+INFINITY)
1475 movel d7,d6 | another (mask for fraction)
1477 bclr IMM (31),d0 | get rid of a's sign bit '
1480 beq Lmuldf$a$0 | branch if a is zero
1482 bclr IMM (31),d2 | get rid of b's sign bit '
1485 beq Lmuldf$b$0 | branch if b is zero
1487 cmpl d7,d0 | is a big?
1488 bhi Lmuldf$inop | if a is NaN return NaN
1489 beq Lmuldf$a$nf | we still have to check d1 and b ...
1490 cmpl d7,d2 | now compare b with INFINITY
1491 bhi Lmuldf$inop | is b NaN?
1492 beq Lmuldf$b$nf | we still have to check d3 ...
1493 | Here we have both numbers finite and nonzero (and with no sign bit).
1494 | Now we get the exponents into d4 and d5.
1495 andl d7,d4 | isolate exponent in d4
1496 beq Lmuldf$a$den | if exponent zero, have denormalized
1497 andl d6,d0 | isolate fraction
1498 orl IMM (0x00100000),d0 | and put hidden bit back
1499 swap d4 | I like exponents in the first byte
1500 #ifndef __mcoldfire__
1509 orl IMM (0x00100000),d2 | and put hidden bit back
1511 #ifndef __mcoldfire__
1517 #ifndef __mcoldfire__
1518 addw d5,d4 | add exponents
1519 subw IMM (D_BIAS+1),d4 | and subtract bias (plus one)
1521 addl d5,d4 | add exponents
1522 subl IMM (D_BIAS+1),d4 | and subtract bias (plus one)
1525 | We are now ready to do the multiplication. The situation is as follows:
1526 | both a and b have bit 52 ( bit 20 of d0 and d2) set (even if they were
1527 | denormalized to start with!), which means that in the product bit 104
1528 | (which will correspond to bit 8 of the fourth long) is set.
1530 | Here we have to do the product.
1531 | To do it we have to juggle the registers back and forth, as there are not
1532 | enough to keep everything in them. So we use the address registers to keep
1533 | some intermediate data.
1535 #ifndef __mcoldfire__
1536 moveml a2-a3,sp@- | save a2 and a3 for temporary use
1542 movel IMM (0),a2 | a2 is a null register
1543 movel d4,a3 | and a3 will preserve the exponent
1545 | First, shift d2-d3 so bit 20 becomes bit 31:
1546 #ifndef __mcoldfire__
1547 rorl IMM (5),d2 | rotate d2 5 places right
1548 swap d2 | and swap it
1549 rorl IMM (5),d3 | do the same thing with d3
1551 movew d3,d6 | get the rightmost 11 bits of d3
1552 andw IMM (0x07ff),d6 |
1553 orw d6,d2 | and put them into d2
1554 andw IMM (0xf800),d3 | clear those bits in d3
1556 moveq IMM (11),d7 | left shift d2 11 bits
1558 movel d3,d6 | get a copy of d3
1559 lsll d7,d3 | left shift d3 11 bits
1560 andl IMM (0xffe00000),d6 | get the top 11 bits of d3
1561 moveq IMM (21),d7 | right shift them 21 bits
1563 orl d6,d2 | stick them at the end of d2
1566 movel d2,d6 | move b into d6-d7
1567 movel d3,d7 | move a into d4-d5
1568 movel d0,d4 | and clear d0-d1-d2-d3 (to put result)
1575 | We use a1 as counter:
1576 movel IMM (DBL_MANT_DIG-1),a1
1577 #ifndef __mcoldfire__
1586 #ifndef __mcoldfire__
1587 exg d7,a1 | put counter back in a1
1593 addl d3,d3 | shift sum once left
1599 bcc 2f | if bit clear skip the following
1600 #ifndef __mcoldfire__
1607 addl d5,d3 | else add a to the sum
1611 #ifndef __mcoldfire__
1619 #ifndef __mcoldfire__
1620 exg d7,a1 | put counter in d7
1621 dbf d7,1b | decrement and branch
1630 movel a3,d4 | restore exponent
1631 #ifndef __mcoldfire__
1639 | Now we have the product in d0-d1-d2-d3, with bit 8 of d0 set. The
1640 | first thing to do now is to normalize it so bit 8 becomes bit
1641 | DBL_MANT_DIG-32 (to do the rounding); later we will shift right.
1650 #ifndef __mcoldfire__
1680 | Now round, check for over- and underflow, and exit.
1681 movel a0,d7 | get sign bit back into d7
1682 moveq IMM (MULTIPLY),d5
1684 btst IMM (DBL_MANT_DIG+1-32),d0
1686 #ifndef __mcoldfire__
1701 moveq IMM (MULTIPLY),d5
1705 moveq IMM (MULTIPLY),d5
1706 movel a0,d7 | get sign bit back into d7
1707 tstl d3 | we know d2 == 0x7ff00000, so check d3
1708 bne Ld$inop | if d3 <> 0 b is NaN
1709 bra Ld$overflow | else we have overflow (since a is finite)
1712 moveq IMM (MULTIPLY),d5
1713 movel a0,d7 | get sign bit back into d7
1714 tstl d1 | we know d0 == 0x7ff00000, so check d1
1715 bne Ld$inop | if d1 <> 0 a is NaN
1716 bra Ld$overflow | else signal overflow
1718 | If either number is zero return zero, unless the other is +/-INFINITY or
1719 | NaN, in which case we return NaN.
1721 moveq IMM (MULTIPLY),d5
1722 #ifndef __mcoldfire__
1723 exg d2,d0 | put b (==0) into d0-d1
1724 exg d3,d1 | and a (with sign bit cleared) into d2-d3
1725 movel a0,d0 | set result sign
1727 movel d0,d2 | put a into d2-d3
1729 movel a0,d0 | put result zero into d0-d1
1734 movel a0,d0 | set result sign
1735 movel a6@(16),d2 | put b into d2-d3 again
1737 bclr IMM (31),d2 | clear sign bit
1738 1: cmpl IMM (0x7ff00000),d2 | check for non-finiteness
1739 bge Ld$inop | in case NaN or +/-INFINITY return NaN
1740 PICLEA SYM (_fpCCR),a0
1742 #ifndef __mcoldfire__
1746 | XXX if frame pointer is ever removed, stack pointer must
1752 | If a number is denormalized we put an exponent of 1 but do not put the
1753 | hidden bit back into the fraction; instead we shift left until bit 21
1754 | (the hidden bit) is set, adjusting the exponent accordingly. We do this
1755 | to ensure that the product of the fractions is close to 1.
1759 1: addl d1,d1 | shift a left until bit 20 is set
1761 #ifndef __mcoldfire__
1762 subw IMM (1),d4 | and adjust exponent
1764 subl IMM (1),d4 | and adjust exponent
1773 1: addl d3,d3 | shift b left until bit 20 is set
1775 #ifndef __mcoldfire__
1776 subw IMM (1),d5 | and adjust exponent
1778 subql IMM (1),d5 | and adjust exponent
1785 |=============================================================================
1787 |=============================================================================
1789 | double __divdf3(double, double);
1792 #ifndef __mcoldfire__
1799 movel a6@(8),d0 | get a into d0-d1
1801 movel a6@(16),d2 | and b into d2-d3
1803 movel d0,d7 | d7 will hold the sign of the result
1805 andl IMM (0x80000000),d7
1806 movel d7,a0 | save sign into a0
1807 movel IMM (0x7ff00000),d7 | useful constant (+INFINITY)
1808 movel d7,d6 | another (mask for fraction)
1810 bclr IMM (31),d0 | get rid of a's sign bit '
1813 beq Ldivdf$a$0 | branch if a is zero
1815 bclr IMM (31),d2 | get rid of b's sign bit '
1818 beq Ldivdf$b$0 | branch if b is zero
1820 cmpl d7,d0 | is a big?
1821 bhi Ldivdf$inop | if a is NaN return NaN
1822 beq Ldivdf$a$nf | if d0 == 0x7ff00000 we check d1
1823 cmpl d7,d2 | now compare b with INFINITY
1824 bhi Ldivdf$inop | if b is NaN return NaN
1825 beq Ldivdf$b$nf | if d2 == 0x7ff00000 we check d3
1826 | Here we have both numbers finite and nonzero (and with no sign bit).
1827 | Now we get the exponents into d4 and d5 and normalize the numbers to
1828 | ensure that the ratio of the fractions is around 1. We do this by
1829 | making sure that both numbers have bit #DBL_MANT_DIG-32-1 (hidden bit)
1830 | set, even if they were denormalized to start with.
1831 | Thus, the result will satisfy: 2 > result > 1/2.
1832 andl d7,d4 | and isolate exponent in d4
1833 beq Ldivdf$a$den | if exponent is zero we have a denormalized
1834 andl d6,d0 | and isolate fraction
1835 orl IMM (0x00100000),d0 | and put hidden bit back
1836 swap d4 | I like exponents in the first byte
1837 #ifndef __mcoldfire__
1846 orl IMM (0x00100000),d2
1848 #ifndef __mcoldfire__
1854 #ifndef __mcoldfire__
1855 subw d5,d4 | subtract exponents
1856 addw IMM (D_BIAS),d4 | and add bias
1858 subl d5,d4 | subtract exponents
1859 addl IMM (D_BIAS),d4 | and add bias
1862 | We are now ready to do the division. We have prepared things in such a way
1863 | that the ratio of the fractions will be less than 2 but greater than 1/2.
1864 | At this point the registers in use are:
1865 | d0-d1 hold a (first operand, bit DBL_MANT_DIG-32=0, bit
1866 | DBL_MANT_DIG-1-32=1)
1867 | d2-d3 hold b (second operand, bit DBL_MANT_DIG-32=1)
1868 | d4 holds the difference of the exponents, corrected by the bias
1869 | a0 holds the sign of the ratio
1871 | To do the rounding correctly we need to keep information about the
1872 | nonsignificant bits. One way to do this would be to do the division
1873 | using four registers; another is to use two registers (as originally
1874 | I did), but use a sticky bit to preserve information about the
1875 | fractional part. Note that we can keep that info in a1, which is not
1877 movel IMM (0),d6 | d6-d7 will hold the result
1879 movel IMM (0),a1 | and a1 will hold the sticky bit
1881 movel IMM (DBL_MANT_DIG-32+1),d5
1883 1: cmpl d0,d2 | is a < b?
1884 bhi 3f | if b > a skip the following
1885 beq 4f | if d0==d2 check d1 and d3
1887 subxl d2,d0 | a <-- a - b
1888 bset d5,d6 | set the corresponding bit in d6
1889 3: addl d1,d1 | shift a by 1
1891 #ifndef __mcoldfire__
1892 dbra d5,1b | and branch back
1898 4: cmpl d1,d3 | here d0==d2, so check d1 and d3
1899 bhi 3b | if d1 > d2 skip the subtraction
1900 bra 2b | else go do it
1902 | Here we have to start setting the bits in the second long.
1903 movel IMM (31),d5 | again d5 is counter
1905 1: cmpl d0,d2 | is a < b?
1906 bhi 3f | if b > a skip the following
1907 beq 4f | if d0==d2 check d1 and d3
1909 subxl d2,d0 | a <-- a - b
1910 bset d5,d7 | set the corresponding bit in d7
1911 3: addl d1,d1 | shift a by 1
1913 #ifndef __mcoldfire__
1914 dbra d5,1b | and branch back
1920 4: cmpl d1,d3 | here d0==d2, so check d1 and d3
1921 bhi 3b | if d1 > d2 skip the subtraction
1922 bra 2b | else go do it
1924 | Now go ahead checking until we hit a one, which we store in d2.
1925 movel IMM (DBL_MANT_DIG),d5
1926 1: cmpl d2,d0 | is a < b?
1927 bhi 4f | if b < a, exit
1928 beq 3f | if d0==d2 check d1 and d3
1929 2: addl d1,d1 | shift a by 1
1931 #ifndef __mcoldfire__
1932 dbra d5,1b | and branch back
1937 movel IMM (0),d2 | here no sticky bit was found
1940 3: cmpl d1,d3 | here d0==d2, so check d1 and d3
1941 bhi 2b | if d1 > d2 go back
1943 | Here put the sticky bit in d2-d3 (in the position which actually corresponds
1944 | to it; if you don't do this the algorithm loses in some cases). '
1947 #ifndef __mcoldfire__
1948 subw IMM (DBL_MANT_DIG),d5
1952 subl IMM (DBL_MANT_DIG),d5
1959 #ifndef __mcoldfire__
1966 | Finally we are finished! Move the longs in the address registers to
1967 | their final destination:
1972 | Here we have finished the division, with the result in d0-d1-d2-d3, with
1973 | 2^21 <= d6 < 2^23. Thus bit 23 is not set, but bit 22 could be set.
1974 | If it is not, then definitely bit 21 is set. Normalize so bit 22 is
1976 btst IMM (DBL_MANT_DIG-32+1),d0
1978 #ifndef __mcoldfire__
2001 | Now round, check for over- and underflow, and exit.
2002 movel a0,d7 | restore sign bit to d7
2003 moveq IMM (DIVIDE),d5
2007 moveq IMM (DIVIDE),d5
2011 | If a is zero check to see whether b is zero also. In that case return
2012 | NaN; then check if b is NaN, and return NaN also in that case. Else
2013 | return a properly signed zero.
2014 moveq IMM (DIVIDE),d5
2018 beq Ld$inop | if b is also zero return NaN
2019 cmpl IMM (0x7ff00000),d2 | check for NaN
2024 1: movel a0,d0 | else return signed zero
2026 PICLEA SYM (_fpCCR),a0 | clear exception flags
2028 #ifndef __mcoldfire__
2032 | XXX if frame pointer is ever removed, stack pointer must
2039 moveq IMM (DIVIDE),d5
2040 | If we got here a is not zero. Check if a is NaN; in that case return NaN,
2041 | else return +/-INFINITY. Remember that a is in d0 with the sign bit
2043 movel a0,d7 | put a's sign bit back in d7 '
2044 cmpl IMM (0x7ff00000),d0 | compare d0 with INFINITY
2045 bhi Ld$inop | if larger it is NaN
2048 bra Ld$div$0 | else signal DIVIDE_BY_ZERO
2051 moveq IMM (DIVIDE),d5
2052 | If d2 == 0x7ff00000 we have to check d3.
2054 bne Ld$inop | if d3 <> 0, b is NaN
2055 bra Ld$underflow | else b is +/-INFINITY, so signal underflow
2058 moveq IMM (DIVIDE),d5
2059 | If d0 == 0x7ff00000 we have to check d1.
2061 bne Ld$inop | if d1 <> 0, a is NaN
2062 | If a is INFINITY we have to check b
2063 cmpl d7,d2 | compare b with INFINITY
2064 bge Ld$inop | if b is NaN or INFINITY return NaN
2067 bra Ld$overflow | else return overflow
2069 | If a number is denormalized we put an exponent of 1 but do not put the
2070 | bit back into the fraction.
2074 1: addl d1,d1 | shift a left until bit 20 is set
2076 #ifndef __mcoldfire__
2077 subw IMM (1),d4 | and adjust exponent
2079 subl IMM (1),d4 | and adjust exponent
2081 btst IMM (DBL_MANT_DIG-32-1),d0
2088 1: addl d3,d3 | shift b left until bit 20 is set
2090 #ifndef __mcoldfire__
2091 subw IMM (1),d5 | and adjust exponent
2093 subql IMM (1),d5 | and adjust exponent
2095 btst IMM (DBL_MANT_DIG-32-1),d2
2100 | This is a common exit point for __muldf3 and __divdf3. When they enter
2101 | this point the sign of the result is in d7, the result in d0-d1, normalized
2102 | so that 2^21 <= d0 < 2^22, and the exponent is in the lower byte of d4.
2104 | First check for underlow in the exponent:
2105 #ifndef __mcoldfire__
2106 cmpw IMM (-DBL_MANT_DIG-1),d4
2108 cmpl IMM (-DBL_MANT_DIG-1),d4
2111 | It could happen that the exponent is less than 1, in which case the
2112 | number is denormalized. In this case we shift right and adjust the
2113 | exponent until it becomes 1 or the fraction is zero (in the latter case
2114 | we signal underflow and return zero).
2116 movel IMM (0),d6 | use d6-d7 to collect bits flushed right
2117 movel d6,d7 | use d6-d7 to collect bits flushed right
2118 #ifndef __mcoldfire__
2119 cmpw IMM (1),d4 | if the exponent is less than 1 we
2121 cmpl IMM (1),d4 | if the exponent is less than 1 we
2123 bge 2f | have to shift right (denormalize)
2125 #ifndef __mcoldfire__
2126 addw IMM (1),d4 | adjust the exponent
2127 lsrl IMM (1),d0 | shift right once
2133 cmpw IMM (1),d4 | is the exponent 1 already?
2135 addl IMM (1),d4 | adjust the exponent
2157 cmpl IMM (1),d4 | is the exponent 1 already?
2159 beq 2f | if not loop back
2161 bra Ld$underflow | safety check, shouldn't execute '
2162 2: orl d6,d2 | this is a trick so we don't lose '
2163 orl d7,d3 | the bits which were flushed right
2164 movel a0,d7 | get back sign bit into d7
2165 | Now call the rounding routine (which takes care of denormalized numbers):
2166 lea pc@(Lround$0),a0 | to return from rounding routine
2167 PICLEA SYM (_fpCCR),a1 | check the rounding mode
2168 #ifdef __mcoldfire__
2171 movew a1@(6),d6 | rounding mode in d6
2172 beq Lround$to$nearest
2173 #ifndef __mcoldfire__
2174 cmpw IMM (ROUND_TO_PLUS),d6
2176 cmpl IMM (ROUND_TO_PLUS),d6
2182 | Here we have a correctly rounded result (either normalized or denormalized).
2184 | Here we should have either a normalized number or a denormalized one, and
2185 | the exponent is necessarily larger or equal to 1 (so we don't have to '
2186 | check again for underflow!). We have to check for overflow or for a
2187 | denormalized number (which also signals underflow).
2188 | Check for overflow (i.e., exponent >= 0x7ff).
2189 #ifndef __mcoldfire__
2190 cmpw IMM (0x07ff),d4
2192 cmpl IMM (0x07ff),d4
2195 | Now check for a denormalized number (exponent==0):
2199 | Put back the exponents and sign and return.
2200 #ifndef __mcoldfire__
2201 lslw IMM (4),d4 | exponent back to fourth byte
2203 lsll IMM (4),d4 | exponent back to fourth byte
2205 bclr IMM (DBL_MANT_DIG-32-1),d0
2206 swap d0 | and put back exponent
2207 #ifndef __mcoldfire__
2213 orl d7,d0 | and sign also
2215 PICLEA SYM (_fpCCR),a0
2217 #ifndef __mcoldfire__
2221 | XXX if frame pointer is ever removed, stack pointer must
2227 |=============================================================================
2229 |=============================================================================
2231 | double __negdf2(double, double);
2234 #ifndef __mcoldfire__
2241 moveq IMM (NEGATE),d5
2242 movel a6@(8),d0 | get number to negate in d0-d1
2244 bchg IMM (31),d0 | negate
2245 movel d0,d2 | make a positive copy (for the tests)
2247 movel d2,d4 | check for zero
2249 beq 2f | if zero (either sign) return +zero
2250 cmpl IMM (0x7ff00000),d2 | compare to +INFINITY
2251 blt 1f | if finite, return
2252 bhi Ld$inop | if larger (fraction not zero) is NaN
2253 tstl d1 | if d2 == 0x7ff00000 check d1
2255 movel d0,d7 | else get sign and return INFINITY
2256 andl IMM (0x80000000),d7
2258 1: PICLEA SYM (_fpCCR),a0
2260 #ifndef __mcoldfire__
2264 | XXX if frame pointer is ever removed, stack pointer must
2272 |=============================================================================
2274 |=============================================================================
2280 | int __cmpdf2_internal(double, double, int);
2281 SYM (__cmpdf2_internal):
2282 #ifndef __mcoldfire__
2284 moveml d2-d7,sp@- | save registers
2289 moveq IMM (COMPARE),d5
2290 movel a6@(8),d0 | get first operand
2292 movel a6@(16),d2 | get second operand
2294 | First check if a and/or b are (+/-) zero and in that case clear
2296 movel d0,d6 | copy signs into d6 (a) and d7(b)
2297 bclr IMM (31),d0 | and clear signs in d0 and d2
2300 cmpl IMM (0x7ff00000),d0 | check for a == NaN
2301 bhi Lcmpd$inop | if d0 > 0x7ff00000, a is NaN
2302 beq Lcmpdf$a$nf | if equal can be INFINITY, so check d1
2303 movel d0,d4 | copy into d4 to test for zero
2307 cmpl IMM (0x7ff00000),d2 | check for b == NaN
2308 bhi Lcmpd$inop | if d2 > 0x7ff00000, b is NaN
2309 beq Lcmpdf$b$nf | if equal can be INFINITY, so check d3
2317 | If the signs are not equal check if a >= 0
2319 bpl Lcmpdf$a$gt$b | if (a >= 0 && b < 0) => a > b
2320 bmi Lcmpdf$b$gt$a | if (a < 0 && b >= 0) => a < b
2322 | If the signs are equal check for < 0
2325 | If both are negative exchange them
2326 #ifndef __mcoldfire__
2338 | Now that they are positive we just compare them as longs (does this also
2339 | work for denormalized numbers?).
2341 bhi Lcmpdf$b$gt$a | |b| > |a|
2342 bne Lcmpdf$a$gt$b | |b| < |a|
2343 | If we got here d0 == d2, so we compare d1 and d3.
2345 bhi Lcmpdf$b$gt$a | |b| > |a|
2346 bne Lcmpdf$a$gt$b | |b| < |a|
2347 | If we got here a == b.
2348 movel IMM (EQUAL),d0
2349 #ifndef __mcoldfire__
2350 moveml sp@+,d2-d7 | put back the registers
2353 | XXX if frame pointer is ever removed, stack pointer must
2359 movel IMM (GREATER),d0
2360 #ifndef __mcoldfire__
2361 moveml sp@+,d2-d7 | put back the registers
2364 | XXX if frame pointer is ever removed, stack pointer must
2371 #ifndef __mcoldfire__
2372 moveml sp@+,d2-d7 | put back the registers
2375 | XXX if frame pointer is ever removed, stack pointer must
2400 moveq IMM (INEXACT_RESULT+INVALID_OPERATION),d7
2401 moveq IMM (DOUBLE_FLOAT),d6
2402 PICJUMP $_exception_handler
2404 | int __cmpdf2(double, double);
2413 bsr SYM (__cmpdf2_internal)
2417 |=============================================================================
2419 |=============================================================================
2421 | The rounding routines expect the number to be normalized in registers
2422 | d0-d1-d2-d3, with the exponent in register d4. They assume that the
2423 | exponent is larger or equal to 1. They return a properly normalized number
2424 | if possible, and a denormalized number otherwise. The exponent is returned
2428 | We now normalize as suggested by D. Knuth ("Seminumerical Algorithms"):
2429 | Here we assume that the exponent is not too small (this should be checked
2430 | before entering the rounding routine), but the number could be denormalized.
2432 | Check for denormalized numbers:
2433 1: btst IMM (DBL_MANT_DIG-32),d0
2434 bne 2f | if set the number is normalized
2435 | Normalize shifting left until bit #DBL_MANT_DIG-32 is set or the exponent
2436 | is one (remember that a denormalized number corresponds to an
2437 | exponent of -D_BIAS+1).
2438 #ifndef __mcoldfire__
2439 cmpw IMM (1),d4 | remember that the exponent is at least one
2441 cmpl IMM (1),d4 | remember that the exponent is at least one
2443 beq 2f | an exponent of one means denormalized
2444 addl d3,d3 | else shift and adjust the exponent
2448 #ifndef __mcoldfire__
2455 | Now round: we do it as follows: after the shifting we can write the
2456 | fraction part as f + delta, where 1 < f < 2^25, and 0 <= delta <= 2.
2457 | If delta < 1, do nothing. If delta > 1, add 1 to f.
2458 | If delta == 1, we make sure the rounded number will be even (odd?)
2460 btst IMM (0),d1 | is delta < 1?
2461 beq 2f | if so, do not do anything
2462 orl d2,d3 | is delta == 1?
2463 bne 1f | if so round to even
2465 andl IMM (2),d3 | bit 1 is the last significant bit
2470 1: movel IMM (1),d3 | else add 1
2474 | Shift right once (because we used bit #DBL_MANT_DIG-32!).
2476 #ifndef __mcoldfire__
2487 | Now check again bit #DBL_MANT_DIG-32 (rounding could have produced a
2488 | 'fraction overflow' ...).
2489 btst IMM (DBL_MANT_DIG-32),d0
2491 #ifndef __mcoldfire__
2504 | If bit #DBL_MANT_DIG-32-1 is clear we have a denormalized number, so we
2505 | have to put the exponent to zero and return a denormalized number.
2506 btst IMM (DBL_MANT_DIG-32-1),d0
2516 #endif /* L_double */
2521 .globl $_exception_handler
2523 QUIET_NaN = 0xffffffff
2524 SIGNL_NaN = 0x7f800001
2525 INFINITY = 0x7f800000
2529 FLT_MAX_EXP = F_MAX_EXP - F_BIAS
2530 FLT_MIN_EXP = 1 - F_BIAS
2533 INEXACT_RESULT = 0x0001
2536 DIVIDE_BY_ZERO = 0x0008
2537 INVALID_OPERATION = 0x0010
2551 ROUND_TO_NEAREST = 0 | round result to nearest representable value
2552 ROUND_TO_ZERO = 1 | round result towards zero
2553 ROUND_TO_PLUS = 2 | round result towards plus infinity
2554 ROUND_TO_MINUS = 3 | round result towards minus infinity
2558 .globl SYM (__addsf3)
2559 .globl SYM (__subsf3)
2560 .globl SYM (__mulsf3)
2561 .globl SYM (__divsf3)
2562 .globl SYM (__negsf2)
2563 .globl SYM (__cmpsf2)
2564 .globl SYM (__cmpsf2_internal)
2566 | These are common routines to return and signal exceptions.
2572 | Return and signal a denormalized number
2574 moveq IMM (INEXACT_RESULT+UNDERFLOW),d7
2575 moveq IMM (SINGLE_FLOAT),d6
2576 PICJUMP $_exception_handler
2580 | Return a properly signed INFINITY and set the exception flags
2581 movel IMM (INFINITY),d0
2583 moveq IMM (INEXACT_RESULT+OVERFLOW),d7
2584 moveq IMM (SINGLE_FLOAT),d6
2585 PICJUMP $_exception_handler
2588 | Return 0 and set the exception flags
2590 moveq IMM (INEXACT_RESULT+UNDERFLOW),d7
2591 moveq IMM (SINGLE_FLOAT),d6
2592 PICJUMP $_exception_handler
2595 | Return a quiet NaN and set the exception flags
2596 movel IMM (QUIET_NaN),d0
2597 moveq IMM (INEXACT_RESULT+INVALID_OPERATION),d7
2598 moveq IMM (SINGLE_FLOAT),d6
2599 PICJUMP $_exception_handler
2602 | Return a properly signed INFINITY and set the exception flags
2603 movel IMM (INFINITY),d0
2605 moveq IMM (INEXACT_RESULT+DIVIDE_BY_ZERO),d7
2606 moveq IMM (SINGLE_FLOAT),d6
2607 PICJUMP $_exception_handler
2609 |=============================================================================
2610 |=============================================================================
2611 | single precision routines
2612 |=============================================================================
2613 |=============================================================================
2615 | A single precision floating point number (float) has the format:
2618 | unsigned int sign : 1; /* sign bit */
2619 | unsigned int exponent : 8; /* exponent, shifted by 126 */
2620 | unsigned int fraction : 23; /* fraction */
2623 | Thus sizeof(float) = 4 (32 bits).
2625 | All the routines are callable from C programs, and return the result
2626 | in the single register d0. They also preserve all registers except
2629 |=============================================================================
2631 |=============================================================================
2633 | float __subsf3(float, float);
2636 bchg IMM (31),sp@(8) | change sign of second operand
2638 |=============================================================================
2640 |=============================================================================
2642 | float __addsf3(float, float);
2645 #ifndef __mcoldfire__
2646 link a6,IMM (0) | everything will be done in registers
2647 moveml d2-d7,sp@- | save all data registers but d0-d1
2652 movel a6@(8),d0 | get first operand
2653 movel a6@(12),d1 | get second operand
2654 movel d0,a0 | get d0's sign bit '
2655 addl d0,d0 | check and clear sign bit of a
2656 beq Laddsf$b | if zero return second operand
2657 movel d1,a1 | save b's sign bit '
2658 addl d1,d1 | get rid of sign bit
2659 beq Laddsf$a | if zero return first operand
2661 | Get the exponents and check for denormalized and/or infinity.
2663 movel IMM (0x00ffffff),d4 | mask to get fraction
2664 movel IMM (0x01000000),d5 | mask to put hidden bit back
2666 movel d0,d6 | save a to get exponent
2667 andl d4,d0 | get fraction in d0
2668 notl d4 | make d4 into a mask for the exponent
2669 andl d4,d6 | get exponent in d6
2670 beq Laddsf$a$den | branch if a is denormalized
2671 cmpl d4,d6 | check for INFINITY or NaN
2673 swap d6 | put exponent into first word
2674 orl d5,d0 | and put hidden bit back
2676 | Now we have a's exponent in d6 (second byte) and the mantissa in d0. '
2677 movel d1,d7 | get exponent in d7
2679 beq Laddsf$b$den | branch if b is denormalized
2680 cmpl d4,d7 | check for INFINITY or NaN
2682 swap d7 | put exponent into first word
2683 notl d4 | make d4 into a mask for the fraction
2684 andl d4,d1 | get fraction in d1
2685 orl d5,d1 | and put hidden bit back
2687 | Now we have b's exponent in d7 (second byte) and the mantissa in d1. '
2689 | Note that the hidden bit corresponds to bit #FLT_MANT_DIG-1, and we
2690 | shifted right once, so bit #FLT_MANT_DIG is set (so we have one extra
2693 movel d1,d2 | move b to d2, since we want to use
2694 | two registers to do the sum
2695 movel IMM (0),d1 | and clear the new ones
2698 | Here we shift the numbers in registers d0 and d1 so the exponents are the
2699 | same, and put the largest exponent in d6. Note that we are using two
2700 | registers for each number (see the discussion by D. Knuth in "Seminumerical
2702 #ifndef __mcoldfire__
2703 cmpw d6,d7 | compare exponents
2705 cmpl d6,d7 | compare exponents
2707 beq Laddsf$3 | if equal don't shift '
2708 bhi 5f | branch if second exponent largest
2710 subl d6,d7 | keep the largest exponent
2712 #ifndef __mcoldfire__
2713 lsrw IMM (8),d7 | put difference in lower byte
2715 lsrl IMM (8),d7 | put difference in lower byte
2717 | if difference is too large we don't shift (actually, we can just exit) '
2718 #ifndef __mcoldfire__
2719 cmpw IMM (FLT_MANT_DIG+2),d7
2721 cmpl IMM (FLT_MANT_DIG+2),d7
2724 #ifndef __mcoldfire__
2725 cmpw IMM (16),d7 | if difference >= 16 swap
2727 cmpl IMM (16),d7 | if difference >= 16 swap
2731 #ifndef __mcoldfire__
2737 #ifndef __mcoldfire__
2738 lsrl IMM (1),d2 | shift right second operand
2756 #ifndef __mcoldfire__
2761 bne 2b | if still more bits, go back to normal case
2764 #ifndef __mcoldfire__
2765 exg d6,d7 | exchange the exponents
2771 subl d6,d7 | keep the largest exponent
2773 #ifndef __mcoldfire__
2774 lsrw IMM (8),d7 | put difference in lower byte
2776 lsrl IMM (8),d7 | put difference in lower byte
2778 | if difference is too large we don't shift (and exit!) '
2779 #ifndef __mcoldfire__
2780 cmpw IMM (FLT_MANT_DIG+2),d7
2782 cmpl IMM (FLT_MANT_DIG+2),d7
2785 #ifndef __mcoldfire__
2786 cmpw IMM (16),d7 | if difference >= 16 swap
2788 cmpl IMM (16),d7 | if difference >= 16 swap
2792 #ifndef __mcoldfire__
2798 #ifndef __mcoldfire__
2799 lsrl IMM (1),d0 | shift right first operand
2817 #ifndef __mcoldfire__
2822 bne 6b | if still more bits, go back to normal case
2823 | otherwise we fall through
2825 | Now we have a in d0-d1, b in d2-d3, and the largest exponent in d6 (the
2826 | signs are stored in a0 and a1).
2829 | Here we have to decide whether to add or subtract the numbers
2830 #ifndef __mcoldfire__
2831 exg d6,a0 | get signs back
2832 exg d7,a1 | and save the exponents
2841 eorl d6,d7 | combine sign bits
2842 bmi Lsubsf$0 | if negative a and b have opposite
2843 | sign so we actually subtract the
2846 | Here we have both positive or both negative
2847 #ifndef __mcoldfire__
2848 exg d6,a0 | now we have the exponent in d6
2854 movel a0,d7 | and sign in d7
2855 andl IMM (0x80000000),d7
2856 | Here we do the addition.
2859 | Note: now we have d2, d3, d4 and d5 to play with!
2861 | Put the exponent, in the first byte, in d2, to use the "standard" rounding
2864 #ifndef __mcoldfire__
2870 | Before rounding normalize so bit #FLT_MANT_DIG is set (we will consider
2871 | the case of denormalized numbers in the rounding routine itself).
2872 | As in the addition (not in the subtraction!) we could have set
2873 | one more bit we check this:
2874 btst IMM (FLT_MANT_DIG+1),d0
2876 #ifndef __mcoldfire__
2888 lea pc@(Laddsf$4),a0 | to return from rounding routine
2889 PICLEA SYM (_fpCCR),a1 | check the rounding mode
2890 #ifdef __mcoldfire__
2893 movew a1@(6),d6 | rounding mode in d6
2894 beq Lround$to$nearest
2895 #ifndef __mcoldfire__
2896 cmpw IMM (ROUND_TO_PLUS),d6
2898 cmpl IMM (ROUND_TO_PLUS),d6
2904 | Put back the exponent, but check for overflow.
2905 #ifndef __mcoldfire__
2911 bclr IMM (FLT_MANT_DIG-1),d0
2912 #ifndef __mcoldfire__
2925 | We are here if a > 0 and b < 0 (sign bits cleared).
2926 | Here we do the subtraction.
2927 movel d6,d7 | put sign in d7
2928 andl IMM (0x80000000),d7
2930 subl d3,d1 | result in d0-d1
2932 beq Laddsf$ret | if zero just exit
2933 bpl 1f | if positive skip the following
2934 bchg IMM (31),d7 | change sign bit in d7
2938 #ifndef __mcoldfire__
2939 exg d2,a0 | now we have the exponent in d2
2940 lsrw IMM (8),d2 | put it in the first byte
2945 lsrl IMM (8),d2 | put it in the first byte
2948 | Now d0-d1 is positive and the sign bit is in d7.
2950 | Note that we do not have to normalize, since in the subtraction bit
2951 | #FLT_MANT_DIG+1 is never set, and denormalized numbers are handled by
2952 | the rounding routines themselves.
2953 lea pc@(Lsubsf$1),a0 | to return from rounding routine
2954 PICLEA SYM (_fpCCR),a1 | check the rounding mode
2955 #ifdef __mcoldfire__
2958 movew a1@(6),d6 | rounding mode in d6
2959 beq Lround$to$nearest
2960 #ifndef __mcoldfire__
2961 cmpw IMM (ROUND_TO_PLUS),d6
2963 cmpl IMM (ROUND_TO_PLUS),d6
2969 | Put back the exponent (we can't have overflow!). '
2970 bclr IMM (FLT_MANT_DIG-1),d0
2971 #ifndef __mcoldfire__
2980 | If one of the numbers was too small (difference of exponents >=
2981 | FLT_MANT_DIG+2) we return the other (and now we don't have to '
2982 | check for finiteness or zero).
2985 PICLEA SYM (_fpCCR),a0
2987 #ifndef __mcoldfire__
2988 moveml sp@+,d2-d7 | restore data registers
2991 | XXX if frame pointer is ever removed, stack pointer must
2994 unlk a6 | and return
2999 PICLEA SYM (_fpCCR),a0
3001 #ifndef __mcoldfire__
3002 moveml sp@+,d2-d7 | restore data registers
3005 | XXX if frame pointer is ever removed, stack pointer must
3008 unlk a6 | and return
3011 | If the numbers are denormalized remember to put exponent equal to 1.
3014 movel d5,d6 | d5 contains 0x01000000
3021 notl d4 | make d4 into a mask for the fraction
3022 | (this was not executed after the jump)
3025 | The rest is mainly code for the different results which can be
3026 | returned (checking always for +/-INFINITY and NaN).
3029 | Return b (if a is zero).
3031 cmpl IMM (0x80000000),d0 | Check if b is -0
3034 andl IMM (0x80000000),d7 | Use the sign of a
3038 | Return a (if b is zero).
3042 | We have to check for NaN and +/-infty.
3044 andl IMM (0x80000000),d7 | put sign in d7
3045 bclr IMM (31),d0 | clear sign
3046 cmpl IMM (INFINITY),d0 | check for infty or NaN
3048 movel d0,d0 | check for zero (we do this because we don't '
3049 bne Laddsf$ret | want to return -0 by mistake
3050 bclr IMM (31),d7 | if zero be sure to clear sign
3051 bra Laddsf$ret | if everything OK just return
3053 | The value to be returned is either +/-infty or NaN
3054 andl IMM (0x007fffff),d0 | check for NaN
3055 bne Lf$inop | if mantissa not zero is NaN
3059 | Normal exit (a and b nonzero, result is not NaN nor +/-infty).
3060 | We have to clear the exception flags (just the exception type).
3061 PICLEA SYM (_fpCCR),a0
3063 orl d7,d0 | put sign bit
3064 #ifndef __mcoldfire__
3065 moveml sp@+,d2-d7 | restore data registers
3068 | XXX if frame pointer is ever removed, stack pointer must
3071 unlk a6 | and return
3075 | Return a denormalized number (for addition we don't signal underflow) '
3076 lsrl IMM (1),d0 | remember to shift right back once
3077 bra Laddsf$ret | and return
3079 | Note: when adding two floats of the same sign if either one is
3080 | NaN we return NaN without regard to whether the other is finite or
3081 | not. When subtracting them (i.e., when adding two numbers of
3082 | opposite signs) things are more complicated: if both are INFINITY
3083 | we return NaN, if only one is INFINITY and the other is NaN we return
3084 | NaN, but if it is finite we return INFINITY with the corresponding sign.
3088 | This could be faster but it is not worth the effort, since it is not
3089 | executed very often. We sacrifice speed for clarity here.
3090 movel a6@(8),d0 | get the numbers back (remember that we
3091 movel a6@(12),d1 | did some processing already)
3092 movel IMM (INFINITY),d4 | useful constant (INFINITY)
3093 movel d0,d2 | save sign bits
3095 bclr IMM (31),d0 | clear sign bits
3097 | We know that one of them is either NaN of +/-INFINITY
3098 | Check for NaN (if either one is NaN return NaN)
3099 cmpl d4,d0 | check first a (d0)
3101 cmpl d4,d1 | check now b (d1)
3103 | Now comes the check for +/-INFINITY. We know that both are (maybe not
3104 | finite) numbers, but we have to check if both are infinite whether we
3105 | are adding or subtracting them.
3106 eorl d3,d2 | to check sign bits
3109 andl IMM (0x80000000),d7 | get (common) sign bit
3112 | We know one (or both) are infinite, so we test for equality between the
3113 | two numbers (if they are equal they have to be infinite both, so we
3115 cmpl d1,d0 | are both infinite?
3116 beq Lf$inop | if so return NaN
3119 andl IMM (0x80000000),d7 | get a's sign bit '
3120 cmpl d4,d0 | test now for infinity
3121 beq Lf$infty | if a is INFINITY return with this sign
3122 bchg IMM (31),d7 | else we know b is INFINITY and has
3123 bra Lf$infty | the opposite sign
3125 |=============================================================================
3127 |=============================================================================
3129 | float __mulsf3(float, float);
3132 #ifndef __mcoldfire__
3139 movel a6@(8),d0 | get a into d0
3140 movel a6@(12),d1 | and b into d1
3141 movel d0,d7 | d7 will hold the sign of the product
3143 andl IMM (0x80000000),d7
3144 movel IMM (INFINITY),d6 | useful constant (+INFINITY)
3145 movel d6,d5 | another (mask for fraction)
3147 movel IMM (0x00800000),d4 | this is to put hidden bit back
3148 bclr IMM (31),d0 | get rid of a's sign bit '
3150 beq Lmulsf$a$0 | branch if a is zero
3151 bclr IMM (31),d1 | get rid of b's sign bit '
3153 beq Lmulsf$b$0 | branch if b is zero
3154 cmpl d6,d0 | is a big?
3155 bhi Lmulsf$inop | if a is NaN return NaN
3156 beq Lmulsf$inf | if a is INFINITY we have to check b
3157 cmpl d6,d1 | now compare b with INFINITY
3158 bhi Lmulsf$inop | is b NaN?
3159 beq Lmulsf$overflow | is b INFINITY?
3160 | Here we have both numbers finite and nonzero (and with no sign bit).
3161 | Now we get the exponents into d2 and d3.
3162 andl d6,d2 | and isolate exponent in d2
3163 beq Lmulsf$a$den | if exponent is zero we have a denormalized
3164 andl d5,d0 | and isolate fraction
3165 orl d4,d0 | and put hidden bit back
3166 swap d2 | I like exponents in the first byte
3167 #ifndef __mcoldfire__
3178 #ifndef __mcoldfire__
3184 #ifndef __mcoldfire__
3185 addw d3,d2 | add exponents
3186 subw IMM (F_BIAS+1),d2 | and subtract bias (plus one)
3188 addl d3,d2 | add exponents
3189 subl IMM (F_BIAS+1),d2 | and subtract bias (plus one)
3192 | We are now ready to do the multiplication. The situation is as follows:
3193 | both a and b have bit FLT_MANT_DIG-1 set (even if they were
3194 | denormalized to start with!), which means that in the product
3195 | bit 2*(FLT_MANT_DIG-1) (that is, bit 2*FLT_MANT_DIG-2-32 of the
3196 | high long) is set.
3198 | To do the multiplication let us move the number a little bit around ...
3199 movel d1,d6 | second operand in d6
3200 movel d0,d5 | first operand in d4-d5
3202 movel d4,d1 | the sums will go in d0-d1
3205 | now bit FLT_MANT_DIG-1 becomes bit 31:
3206 lsll IMM (31-FLT_MANT_DIG+1),d6
3208 | Start the loop (we loop #FLT_MANT_DIG times):
3209 moveq IMM (FLT_MANT_DIG-1),d3
3210 1: addl d1,d1 | shift sum
3212 lsll IMM (1),d6 | get bit bn
3213 bcc 2f | if not set skip sum
3217 #ifndef __mcoldfire__
3218 dbf d3,1b | loop back
3224 | Now we have the product in d0-d1, with bit (FLT_MANT_DIG - 1) + FLT_MANT_DIG
3225 | (mod 32) of d0 set. The first thing to do now is to normalize it so bit
3226 | FLT_MANT_DIG is set (to do the rounding).
3227 #ifndef __mcoldfire__
3231 andw IMM (0x03ff),d3
3232 andw IMM (0xfd00),d1
3241 andl IMM (0xfffffd00),d1
3246 #ifndef __mcoldfire__
3252 moveq IMM (MULTIPLY),d5
3254 btst IMM (FLT_MANT_DIG+1),d0
3256 #ifndef __mcoldfire__
3271 moveq IMM (MULTIPLY),d5
3275 moveq IMM (MULTIPLY),d5
3279 moveq IMM (MULTIPLY),d5
3280 | If either is NaN return NaN; else both are (maybe infinite) numbers, so
3281 | return INFINITY with the correct sign (which is in d7).
3282 cmpl d6,d1 | is b NaN?
3283 bhi Lf$inop | if so return NaN
3284 bra Lf$overflow | else return +/-INFINITY
3286 | If either number is zero return zero, unless the other is +/-INFINITY,
3287 | or NaN, in which case we return NaN.
3289 | Here d1 (==b) is zero.
3290 movel a6@(8),d1 | get a again to check for non-finiteness
3293 movel a6@(12),d1 | get b again to check for non-finiteness
3294 1: bclr IMM (31),d1 | clear sign bit
3295 cmpl IMM (INFINITY),d1 | and check for a large exponent
3296 bge Lf$inop | if b is +/-INFINITY or NaN return NaN
3297 movel d7,d0 | else return signed zero
3298 PICLEA SYM (_fpCCR),a0 |
3300 #ifndef __mcoldfire__
3304 | XXX if frame pointer is ever removed, stack pointer must
3310 | If a number is denormalized we put an exponent of 1 but do not put the
3311 | hidden bit back into the fraction; instead we shift left until bit 23
3312 | (the hidden bit) is set, adjusting the exponent accordingly. We do this
3313 | to ensure that the product of the fractions is close to 1.
3317 1: addl d0,d0 | shift a left (until bit 23 is set)
3318 #ifndef __mcoldfire__
3319 subw IMM (1),d2 | and adjust exponent
3321 subql IMM (1),d2 | and adjust exponent
3323 btst IMM (FLT_MANT_DIG-1),d0
3325 bra 1b | else loop back
3330 1: addl d1,d1 | shift b left until bit 23 is set
3331 #ifndef __mcoldfire__
3332 subw IMM (1),d3 | and adjust exponent
3334 subql IMM (1),d3 | and adjust exponent
3336 btst IMM (FLT_MANT_DIG-1),d1
3338 bra 1b | else loop back
3340 |=============================================================================
3342 |=============================================================================
3344 | float __divsf3(float, float);
3347 #ifndef __mcoldfire__
3354 movel a6@(8),d0 | get a into d0
3355 movel a6@(12),d1 | and b into d1
3356 movel d0,d7 | d7 will hold the sign of the result
3358 andl IMM (0x80000000),d7 |
3359 movel IMM (INFINITY),d6 | useful constant (+INFINITY)
3360 movel d6,d5 | another (mask for fraction)
3362 movel IMM (0x00800000),d4 | this is to put hidden bit back
3363 bclr IMM (31),d0 | get rid of a's sign bit '
3365 beq Ldivsf$a$0 | branch if a is zero
3366 bclr IMM (31),d1 | get rid of b's sign bit '
3368 beq Ldivsf$b$0 | branch if b is zero
3369 cmpl d6,d0 | is a big?
3370 bhi Ldivsf$inop | if a is NaN return NaN
3371 beq Ldivsf$inf | if a is INFINITY we have to check b
3372 cmpl d6,d1 | now compare b with INFINITY
3373 bhi Ldivsf$inop | if b is NaN return NaN
3374 beq Ldivsf$underflow
3375 | Here we have both numbers finite and nonzero (and with no sign bit).
3376 | Now we get the exponents into d2 and d3 and normalize the numbers to
3377 | ensure that the ratio of the fractions is close to 1. We do this by
3378 | making sure that bit #FLT_MANT_DIG-1 (hidden bit) is set.
3379 andl d6,d2 | and isolate exponent in d2
3380 beq Ldivsf$a$den | if exponent is zero we have a denormalized
3381 andl d5,d0 | and isolate fraction
3382 orl d4,d0 | and put hidden bit back
3383 swap d2 | I like exponents in the first byte
3384 #ifndef __mcoldfire__
3395 #ifndef __mcoldfire__
3401 #ifndef __mcoldfire__
3402 subw d3,d2 | subtract exponents
3403 addw IMM (F_BIAS),d2 | and add bias
3405 subl d3,d2 | subtract exponents
3406 addl IMM (F_BIAS),d2 | and add bias
3409 | We are now ready to do the division. We have prepared things in such a way
3410 | that the ratio of the fractions will be less than 2 but greater than 1/2.
3411 | At this point the registers in use are:
3412 | d0 holds a (first operand, bit FLT_MANT_DIG=0, bit FLT_MANT_DIG-1=1)
3413 | d1 holds b (second operand, bit FLT_MANT_DIG=1)
3414 | d2 holds the difference of the exponents, corrected by the bias
3415 | d7 holds the sign of the ratio
3416 | d4, d5, d6 hold some constants
3417 movel d7,a0 | d6-d7 will hold the ratio of the fractions
3421 moveq IMM (FLT_MANT_DIG+1),d3
3422 1: cmpl d0,d1 | is a < b?
3424 bset d3,d6 | set a bit in d6
3425 subl d1,d0 | if a >= b a <-- a-b
3426 beq 3f | if a is zero, exit
3427 2: addl d0,d0 | multiply a by 2
3428 #ifndef __mcoldfire__
3435 | Now we keep going to set the sticky bit ...
3436 moveq IMM (FLT_MANT_DIG),d3
3440 #ifndef __mcoldfire__
3449 #ifndef __mcoldfire__
3450 subw IMM (FLT_MANT_DIG),d3
3453 subl IMM (FLT_MANT_DIG),d3
3458 movel d6,d0 | put the ratio in d0-d1
3459 movel a0,d7 | get sign back
3461 | Because of the normalization we did before we are guaranteed that
3462 | d0 is smaller than 2^26 but larger than 2^24. Thus bit 26 is not set,
3463 | bit 25 could be set, and if it is not set then bit 24 is necessarily set.
3464 btst IMM (FLT_MANT_DIG+1),d0
3465 beq 1f | if it is not set, then bit 24 is set
3467 #ifndef __mcoldfire__
3473 | Now round, check for over- and underflow, and exit.
3474 moveq IMM (DIVIDE),d5
3478 moveq IMM (DIVIDE),d5
3482 moveq IMM (DIVIDE),d5
3486 moveq IMM (DIVIDE),d5
3490 moveq IMM (DIVIDE),d5
3491 | If a is zero check to see whether b is zero also. In that case return
3492 | NaN; then check if b is NaN, and return NaN also in that case. Else
3493 | return a properly signed zero.
3494 andl IMM (0x7fffffff),d1 | clear sign bit and test b
3495 beq Lf$inop | if b is also zero return NaN
3496 cmpl IMM (INFINITY),d1 | check for NaN
3498 movel d7,d0 | else return signed zero
3499 PICLEA SYM (_fpCCR),a0 |
3501 #ifndef __mcoldfire__
3505 | XXX if frame pointer is ever removed, stack pointer must
3512 moveq IMM (DIVIDE),d5
3513 | If we got here a is not zero. Check if a is NaN; in that case return NaN,
3514 | else return +/-INFINITY. Remember that a is in d0 with the sign bit
3516 cmpl IMM (INFINITY),d0 | compare d0 with INFINITY
3517 bhi Lf$inop | if larger it is NaN
3518 bra Lf$div$0 | else signal DIVIDE_BY_ZERO
3521 moveq IMM (DIVIDE),d5
3522 | If a is INFINITY we have to check b
3523 cmpl IMM (INFINITY),d1 | compare b with INFINITY
3524 bge Lf$inop | if b is NaN or INFINITY return NaN
3525 bra Lf$overflow | else return overflow
3527 | If a number is denormalized we put an exponent of 1 but do not put the
3528 | bit back into the fraction.
3532 1: addl d0,d0 | shift a left until bit FLT_MANT_DIG-1 is set
3533 #ifndef __mcoldfire__
3534 subw IMM (1),d2 | and adjust exponent
3536 subl IMM (1),d2 | and adjust exponent
3538 btst IMM (FLT_MANT_DIG-1),d0
3545 1: addl d1,d1 | shift b left until bit FLT_MANT_DIG is set
3546 #ifndef __mcoldfire__
3547 subw IMM (1),d3 | and adjust exponent
3549 subl IMM (1),d3 | and adjust exponent
3551 btst IMM (FLT_MANT_DIG-1),d1
3556 | This is a common exit point for __mulsf3 and __divsf3.
3558 | First check for underlow in the exponent:
3559 #ifndef __mcoldfire__
3560 cmpw IMM (-FLT_MANT_DIG-1),d2
3562 cmpl IMM (-FLT_MANT_DIG-1),d2
3565 | It could happen that the exponent is less than 1, in which case the
3566 | number is denormalized. In this case we shift right and adjust the
3567 | exponent until it becomes 1 or the fraction is zero (in the latter case
3568 | we signal underflow and return zero).
3569 movel IMM (0),d6 | d6 is used temporarily
3570 #ifndef __mcoldfire__
3571 cmpw IMM (1),d2 | if the exponent is less than 1 we
3573 cmpl IMM (1),d2 | if the exponent is less than 1 we
3575 bge 2f | have to shift right (denormalize)
3577 #ifndef __mcoldfire__
3578 addw IMM (1),d2 | adjust the exponent
3579 lsrl IMM (1),d0 | shift right once
3581 roxrl IMM (1),d6 | d6 collect bits we would lose otherwise
3582 cmpw IMM (1),d2 | is the exponent 1 already?
3584 addql IMM (1),d2 | adjust the exponent
3594 cmpl IMM (1),d2 | is the exponent 1 already?
3596 beq 2f | if not loop back
3598 bra Lf$underflow | safety check, shouldn't execute '
3599 2: orl d6,d1 | this is a trick so we don't lose '
3600 | the extra bits which were flushed right
3601 | Now call the rounding routine (which takes care of denormalized numbers):
3602 lea pc@(Lround$0),a0 | to return from rounding routine
3603 PICLEA SYM (_fpCCR),a1 | check the rounding mode
3604 #ifdef __mcoldfire__
3607 movew a1@(6),d6 | rounding mode in d6
3608 beq Lround$to$nearest
3609 #ifndef __mcoldfire__
3610 cmpw IMM (ROUND_TO_PLUS),d6
3612 cmpl IMM (ROUND_TO_PLUS),d6
3618 | Here we have a correctly rounded result (either normalized or denormalized).
3620 | Here we should have either a normalized number or a denormalized one, and
3621 | the exponent is necessarily larger or equal to 1 (so we don't have to '
3622 | check again for underflow!). We have to check for overflow or for a
3623 | denormalized number (which also signals underflow).
3624 | Check for overflow (i.e., exponent >= 255).
3625 #ifndef __mcoldfire__
3626 cmpw IMM (0x00ff),d2
3628 cmpl IMM (0x00ff),d2
3631 | Now check for a denormalized number (exponent==0).
3635 | Put back the exponents and sign and return.
3636 #ifndef __mcoldfire__
3637 lslw IMM (7),d2 | exponent back to fourth byte
3639 lsll IMM (7),d2 | exponent back to fourth byte
3641 bclr IMM (FLT_MANT_DIG-1),d0
3642 swap d0 | and put back exponent
3643 #ifndef __mcoldfire__
3649 orl d7,d0 | and sign also
3651 PICLEA SYM (_fpCCR),a0
3653 #ifndef __mcoldfire__
3657 | XXX if frame pointer is ever removed, stack pointer must
3663 |=============================================================================
3665 |=============================================================================
3667 | This is trivial and could be shorter if we didn't bother checking for NaN '
3670 | float __negsf2(float);
3673 #ifndef __mcoldfire__
3680 moveq IMM (NEGATE),d5
3681 movel a6@(8),d0 | get number to negate in d0
3682 bchg IMM (31),d0 | negate
3683 movel d0,d1 | make a positive copy
3685 tstl d1 | check for zero
3686 beq 2f | if zero (either sign) return +zero
3687 cmpl IMM (INFINITY),d1 | compare to +INFINITY
3689 bhi Lf$inop | if larger (fraction not zero) is NaN
3690 movel d0,d7 | else get sign and return INFINITY
3691 andl IMM (0x80000000),d7
3693 1: PICLEA SYM (_fpCCR),a0
3695 #ifndef __mcoldfire__
3699 | XXX if frame pointer is ever removed, stack pointer must
3707 |=============================================================================
3709 |=============================================================================
3715 | int __cmpsf2_internal(float, float, int);
3716 SYM (__cmpsf2_internal):
3717 #ifndef __mcoldfire__
3719 moveml d2-d7,sp@- | save registers
3724 moveq IMM (COMPARE),d5
3725 movel a6@(8),d0 | get first operand
3726 movel a6@(12),d1 | get second operand
3727 | Check if either is NaN, and in that case return garbage and signal
3728 | INVALID_OPERATION. Check also if either is zero, and clear the signs
3731 andl IMM (0x7fffffff),d0
3733 cmpl IMM (0x7f800000),d0
3737 andl IMM (0x7fffffff),d1
3739 cmpl IMM (0x7f800000),d1
3745 | If the signs are not equal check if a >= 0
3747 bpl Lcmpsf$a$gt$b | if (a >= 0 && b < 0) => a > b
3748 bmi Lcmpsf$b$gt$a | if (a < 0 && b >= 0) => a < b
3750 | If the signs are equal check for < 0
3753 | If both are negative exchange them
3754 #ifndef __mcoldfire__
3762 | Now that they are positive we just compare them as longs (does this also
3763 | work for denormalized numbers?).
3765 bhi Lcmpsf$b$gt$a | |b| > |a|
3766 bne Lcmpsf$a$gt$b | |b| < |a|
3767 | If we got here a == b.
3768 movel IMM (EQUAL),d0
3769 #ifndef __mcoldfire__
3770 moveml sp@+,d2-d7 | put back the registers
3777 movel IMM (GREATER),d0
3778 #ifndef __mcoldfire__
3779 moveml sp@+,d2-d7 | put back the registers
3782 | XXX if frame pointer is ever removed, stack pointer must
3789 #ifndef __mcoldfire__
3790 moveml sp@+,d2-d7 | put back the registers
3793 | XXX if frame pointer is ever removed, stack pointer must
3808 moveq IMM (INEXACT_RESULT+INVALID_OPERATION),d7
3809 moveq IMM (SINGLE_FLOAT),d6
3810 PICJUMP $_exception_handler
3812 | int __cmpsf2(float, float);
3819 bsr (__cmpsf2_internal)
3823 |=============================================================================
3825 |=============================================================================
3827 | The rounding routines expect the number to be normalized in registers
3828 | d0-d1, with the exponent in register d2. They assume that the
3829 | exponent is larger or equal to 1. They return a properly normalized number
3830 | if possible, and a denormalized number otherwise. The exponent is returned
3834 | We now normalize as suggested by D. Knuth ("Seminumerical Algorithms"):
3835 | Here we assume that the exponent is not too small (this should be checked
3836 | before entering the rounding routine), but the number could be denormalized.
3838 | Check for denormalized numbers:
3839 1: btst IMM (FLT_MANT_DIG),d0
3840 bne 2f | if set the number is normalized
3841 | Normalize shifting left until bit #FLT_MANT_DIG is set or the exponent
3842 | is one (remember that a denormalized number corresponds to an
3843 | exponent of -F_BIAS+1).
3844 #ifndef __mcoldfire__
3845 cmpw IMM (1),d2 | remember that the exponent is at least one
3847 cmpl IMM (1),d2 | remember that the exponent is at least one
3849 beq 2f | an exponent of one means denormalized
3850 addl d1,d1 | else shift and adjust the exponent
3852 #ifndef __mcoldfire__
3859 | Now round: we do it as follows: after the shifting we can write the
3860 | fraction part as f + delta, where 1 < f < 2^25, and 0 <= delta <= 2.
3861 | If delta < 1, do nothing. If delta > 1, add 1 to f.
3862 | If delta == 1, we make sure the rounded number will be even (odd?)
3864 btst IMM (0),d0 | is delta < 1?
3865 beq 2f | if so, do not do anything
3866 tstl d1 | is delta == 1?
3867 bne 1f | if so round to even
3869 andl IMM (2),d1 | bit 1 is the last significant bit
3872 1: movel IMM (1),d1 | else add 1
3874 | Shift right once (because we used bit #FLT_MANT_DIG!).
3876 | Now check again bit #FLT_MANT_DIG (rounding could have produced a
3877 | 'fraction overflow' ...).
3878 btst IMM (FLT_MANT_DIG),d0
3881 #ifndef __mcoldfire__
3887 | If bit #FLT_MANT_DIG-1 is clear we have a denormalized number, so we
3888 | have to put the exponent to zero and return a denormalized number.
3889 btst IMM (FLT_MANT_DIG-1),d0
3899 #endif /* L_float */
3901 | gcc expects the routines __eqdf2, __nedf2, __gtdf2, __gedf2,
3902 | __ledf2, __ltdf2 to all return the same value as a direct call to
3903 | __cmpdf2 would. In this implementation, each of these routines
3904 | simply calls __cmpdf2. It would be more efficient to give the
3905 | __cmpdf2 routine several names, but separating them out will make it
3906 | easier to write efficient versions of these routines someday.
3907 | If the operands recompare unordered unordered __gtdf2 and __gedf2 return -1.
3908 | The other routines return 1.
3913 .globl SYM (__eqdf2)
3921 PICCALL SYM (__cmpdf2_internal)
3924 #endif /* L_eqdf2 */
3929 .globl SYM (__nedf2)
3937 PICCALL SYM (__cmpdf2_internal)
3940 #endif /* L_nedf2 */
3945 .globl SYM (__gtdf2)
3953 PICCALL SYM (__cmpdf2_internal)
3956 #endif /* L_gtdf2 */
3961 .globl SYM (__gedf2)
3969 PICCALL SYM (__cmpdf2_internal)
3972 #endif /* L_gedf2 */
3977 .globl SYM (__ltdf2)
3985 PICCALL SYM (__cmpdf2_internal)
3988 #endif /* L_ltdf2 */
3993 .globl SYM (__ledf2)
4001 PICCALL SYM (__cmpdf2_internal)
4004 #endif /* L_ledf2 */
4006 | The comments above about __eqdf2, et. al., also apply to __eqsf2,
4007 | et. al., except that the latter call __cmpsf2 rather than __cmpdf2.
4012 .globl SYM (__eqsf2)
4018 PICCALL SYM (__cmpsf2_internal)
4021 #endif /* L_eqsf2 */
4026 .globl SYM (__nesf2)
4032 PICCALL SYM (__cmpsf2_internal)
4035 #endif /* L_nesf2 */
4040 .globl SYM (__gtsf2)
4046 PICCALL SYM (__cmpsf2_internal)
4049 #endif /* L_gtsf2 */
4054 .globl SYM (__gesf2)
4060 PICCALL SYM (__cmpsf2_internal)
4063 #endif /* L_gesf2 */
4068 .globl SYM (__ltsf2)
4074 PICCALL SYM (__cmpsf2_internal)
4077 #endif /* L_ltsf2 */
4082 .globl SYM (__lesf2)
4088 PICCALL SYM (__cmpsf2_internal)
4091 #endif /* L_lesf2 */
4093 #if defined (__ELF__) && defined (__linux__)
4094 /* Make stack non-executable for ELF linux targets. */
4095 .section .note.GNU-stack,"",@progbits