1 /*-------------------------------------------------------------------------
2 _fsdiv.c - Floating point library in optimized assembly for 8051
4 Copyright (c) 2004, Paul Stoffregen, paul@pjrc.com
6 This library 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 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to the
18 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 As a special exception, if you link this library with other files,
22 some of which are compiled with SDCC, to produce an executable,
23 this library does not by itself cause the resulting executable to
24 be covered by the GNU General Public License. This exception does
25 not however invalidate any other reasons why the executable file
26 might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
30 #define __SDCC_FLOAT_LIB
34 #ifdef FLOAT_ASM_MCS51
36 // float __fsdiv (float a, float b) __reentrant
37 static void dummy(void) __naked
42 // extract the two inputs, placing them into:
43 // sign exponent mantissa
44 // ---- -------- --------
45 // a: sign_a exp_a r4/r3/r2
46 // b: sign_b exp_b r7/r6/r5
50 // compute final sign bit
55 // if divisor is zero, ...
57 // if dividend is also zero, return NaN
61 // but dividend is non-zero, return infinity
64 // if dividend is zero, return zero
68 // if divisor is infinity, ...
71 // and dividend is also infinity, return NaN
76 // but dividend is not infinity, return zero
79 // if dividend is infinity, return infinity
88 // if no carry then no underflow
103 // need extra bits on a's mantissa
104 #ifdef FLOAT_FULL_ACCURACY
137 // begin long division
139 #ifdef FLOAT_FULL_ACCURACY
154 subb a
, #0 // carry==0 if mant1 >= mant2
156 #ifdef FLOAT_FULL_ACCURACY
190 // shift partial remainder
205 #ifdef FLOAT_FULL_ACCURACY
212 // now we've got a division result, so all we need to do
213 // is round off properly, normalize and output a float
215 #ifdef FLOAT_FULL_ACCURACY
230 // incrementing exp_a without checking carry is dangerous
243 ljmp fs_zerocheck_return
250 ** libgcc support for software floating point.
251 ** Copyright (C) 1991 by Pipeline Associates, Inc. All rights reserved.
252 ** Permission is granted to do *anything* you want with this file,
253 ** commercial or otherwise, provided this message remains intact. So there!
254 ** I would appreciate receiving any updates/patches/changes that anyone
255 ** makes, and am willing to be the repository for said changes (am I
256 ** making a big mistake?).
259 ** Pipeline Associates, Inc.
260 ** pipeline!phw@motown.com or
261 ** sun!pipeline!phw or
262 ** uunet!motown!pipeline!phw
265 /* (c)2000/2001: hacked a little by johan.knol@iduna.nl for sdcc */
273 /* divide two floats */
274 static float __fsdiv_org (float a1
, float a2
) __SDCC_FLOAT_NONBANKED
276 volatile union float_long fl1
, fl2
;
279 unsigned long mant1
, mant2
;
286 /* numerator denormal??? */
291 /* subtract exponents */
296 sign
= SIGN (fl1
.l
) ^ SIGN (fl2
.l
);
298 /* now get mantissas */
299 mant1
= MANT (fl1
.l
);
300 mant2
= MANT (fl2
.l
);
302 /* this assures we have 24 bits of precision in the end */
313 if (exp
< 1) /* denormal */
318 fl1
.l
= sign
? SIGNBIT
| __INFINITY
: __INFINITY
;
322 /* now we perform repeated subtraction of fl2.l from fl1.l */
326 long diff
= mant1
- mant2
;
343 /* pack up and go home */
344 fl1
.l
= PACK (sign
? SIGNBIT
: 0 , exp
, result
);
349 float __fsdiv (float a1
, float a2
) __SDCC_FLOAT_NONBANKED
351 unsigned long _AUTOMEM
*p2
= (unsigned long *) &a2
;
353 if (EXP (*p2
) == 0) // a2 is denormal or zero, treat as zero
356 unsigned long _AUTOMEM
*p
= (unsigned long *) &f
;
358 *p
= __INFINITY
; // +inf
360 *p
= SIGNBIT
| __INFINITY
; // -inf
361 else // a1 is denormal, zero or nan
365 return __fsdiv_org (a1
, a2
);