1 /* Floating point library in optimized assembly for 8051
2 * Copyright (c) 2004, Paul Stoffregen, paul@pjrc.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #define SDCC_FLOAT_LIB
24 #ifdef FLOAT_ASM_MCS51
26 // float __fsdiv (float a, float b) __reentrant
27 static void dummy(void) __naked
32 // extract the two inputs, placing them into:
33 // sign exponent mantiassa
34 // ---- -------- ---------
35 // a: sign_a exp_a r4/r3/r2
36 // b: sign_b exp_b r7/r6/r5
40 // compute final sign bit
45 // if divisor is zero, ...
47 // if dividend is also zero, return NaN
51 // but dividend is non-zero, return infinity
54 // if dividend is zero, return zero
58 // if divisor is infinity, ...
61 // and dividend is also infinity, return NaN
66 // but dividend is not infinity, return zero
69 // if dividend is infinity, return infinity
78 // if no carry then no underflow
93 // need extra bits on a's mantissa
94 #ifdef FLOAT_FULL_ACCURACY
127 // begin long division
129 #ifdef FLOAT_FULL_ACCURACY
144 subb a, #0 // carry==0 if mant1 >= mant2
146 #ifdef FLOAT_FULL_ACCURACY
180 // shift partial remainder
195 #ifdef FLOAT_FULL_ACCURACY
202 // now we've got a division result, so all we need to do
203 // is round off properly, normalize and output a float
205 #ifdef FLOAT_FULL_ACCURACY
220 // incrementing exp_a without checking carry is dangerous
233 ljmp fs_zerocheck_return
240 ** libgcc support for software floating point.
241 ** Copyright (C) 1991 by Pipeline Associates, Inc. All rights reserved.
242 ** Permission is granted to do *anything* you want with this file,
243 ** commercial or otherwise, provided this message remains intact. So there!
244 ** I would appreciate receiving any updates/patches/changes that anyone
245 ** makes, and am willing to be the repository for said changes (am I
246 ** making a big mistake?).
249 ** Pipeline Associates, Inc.
250 ** pipeline!phw@motown.com or
251 ** sun!pipeline!phw or
252 ** uunet!motown!pipeline!phw
255 /* (c)2000/2001: hacked a little by johan.knol@iduna.nl for sdcc */
263 /* divide two floats */
264 float __fsdiv (float a1, float a2)
266 volatile union float_long fl1, fl2;
267 volatile long result;
268 volatile unsigned long mask;
269 volatile long mant1, mant2;
276 /* subtract exponents */
282 sign = SIGN (fl1.l) ^ SIGN (fl2.l);
284 /* divide by zero??? */
286 {/* return NaN or -NaN */
291 /* numerator zero??? */
295 /* now get mantissas */
296 mant1 = MANT (fl1.l);
297 mant2 = MANT (fl2.l);
299 /* this assures we have 25 bits of precision in the end */
306 /* now we perform repeated subtraction of fl2.l from fl1.l */
329 /* pack up and go home */
331 fl1.l = (sign ? SIGNBIT : 0) | __INFINITY;
335 fl1.l = PACK (sign ? SIGNBIT : 0 , exp, result);