?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*-------------------------------------------------------------------------
  2.    _fsadd.c - Floating point library in optimized assembly for 8051
  3.  
  4.    Copyright (c) 2004, Paul Stoffregen, paul@pjrc.com
  5.  
  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
  9.    later version.
  10.  
  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.
  15.  
  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,
  19.    MA 02110-1301, USA.
  20.  
  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. -------------------------------------------------------------------------*/
  28.  
  29.  
  30. #define __SDCC_FLOAT_LIB
  31. #include <float.h>
  32. #include <stdbool.h>
  33. #include <sdcc-lib.h>
  34.  
  35. #ifdef FLOAT_ASM_MCS51
  36.  
  37. // float __fsadd (float a, float b) __reentrant
  38. static void dummy(void) __naked
  39. {
  40.         __asm
  41.  
  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
  47.         //
  48.         // r1: used to extend precision of a's mantissa
  49.         // r0: general purpose loop counter
  50.  
  51.         .globl  ___fsadd
  52. ___fsadd:
  53.         lcall   fsgetargs
  54.  
  55.         .globl  fsadd_direct_entry
  56. fsadd_direct_entry:
  57.         // we're going to extend mantissa to 32 bits temporarily
  58.         mov     r1, #0
  59.  
  60.         // which exponent is greater?
  61.         mov     a, exp_b
  62.         cjne    a, exp_a, 00005$
  63.         sjmp    00011$
  64. 00005$: jnc     00010$
  65.  
  66.         // a's exponent was greater, so shift b's mantissa
  67.         lcall   fs_swap_a_b
  68.  
  69. 00010$:
  70.         // b's exponent was greater, so shift a's mantissa
  71.         mov     a, exp_b
  72.         clr     c
  73.         subb    a, exp_a
  74.         lcall   fs_rshift_a     // acc has # of shifts to do
  75.  
  76. 00011$:
  77.         // decide if we need to add or subtract
  78.         // sign_a and sign_b are stored in the flag bits of psw,
  79.         // so this little trick checks if the arguements have the
  80.         // same sign.
  81.         mov     a, psw
  82.         swap    a
  83.         xrl     a, psw
  84.         jb      acc.1, 00022$
  85.  
  86. 00020$:
  87.         // add the mantissas (both positive or both negative)
  88.         mov     a, r2
  89.         add     a, r5
  90.         mov     r2, a
  91.         mov     a, r3
  92.         addc    a, r6
  93.         mov     r3, a
  94.         mov     a, r4
  95.         addc    a, r7
  96.         mov     r4, a
  97.         // check for overflow past 24 bits
  98.         jnc     00021$
  99.         mov     a, #1
  100.         lcall   fs_rshift_a
  101.         mov     a, r4
  102.         orl     a, #0x80
  103.         mov     r4, a
  104. 00021$:
  105.         ljmp    fs_round_and_return
  106.  
  107.  
  108.  
  109. 00022$:
  110.         // subtract the mantissas (one of them is negative)
  111.         clr     c
  112.         mov     a, r2
  113.         subb    a, r5
  114.         mov     r2, a
  115.         mov     a, r3
  116.         subb    a, r6
  117.         mov     r3, a
  118.         mov     a, r4
  119.         subb    a, r7
  120.         mov     r4, a
  121.         jnc     00025$
  122.         // if we get a negative result, turn it positive and
  123.         // flip the sign bit
  124.         clr     c
  125.         clr     a
  126.         subb    a, r1
  127.         mov     r1, a
  128.         clr     a
  129.         subb    a, r2
  130.         mov     r2, a
  131.         clr     a
  132.         subb    a, r3
  133.         mov     r3, a
  134.         clr     a
  135.         subb    a, r4
  136.         mov     r4, a
  137.         cpl     sign_a
  138. 00025$:
  139.         lcall   fs_normalize_a
  140.         ljmp    fs_round_and_return
  141.  
  142.         __endasm;
  143. }
  144.  
  145. #else
  146.  
  147. /*
  148. ** libgcc support for software floating point.
  149. ** Copyright (C) 1991 by Pipeline Associates, Inc.  All rights reserved.
  150. ** Permission is granted to do *anything* you want with this file,
  151. ** commercial or otherwise, provided this message remains intact.  So there!
  152. ** I would appreciate receiving any updates/patches/changes that anyone
  153. ** makes, and am willing to be the repository for said changes (am I
  154. ** making a big mistake?).
  155. **
  156. ** Pat Wood
  157. ** Pipeline Associates, Inc.
  158. ** pipeline!phw@motown.com or
  159. ** sun!pipeline!phw or
  160. ** uunet!motown!pipeline!phw
  161. */
  162.  
  163. union float_long
  164.   {
  165.     float f;
  166.     unsigned long l;
  167.   };
  168.  
  169. /* add two floats */
  170. float __fsadd (float a1, float a2)
  171. {
  172.   long mant1, mant2;
  173.   long _AUTOMEM *pfl1;
  174.   long _AUTOMEM *pfl2;
  175.   int exp1, exp2, expd;
  176.   bool sign = false;
  177.  
  178.   pfl2 = (long _AUTOMEM *)&a2;
  179.   exp2 = EXP (*pfl2);
  180.   mant2 = MANT (*pfl2) << 4;
  181.   if (SIGN (*pfl2))
  182.     mant2 = -mant2;
  183.   /* check for zero args */
  184.   if (!*pfl2)
  185.     return (a1);
  186.  
  187.   pfl1 = (long _AUTOMEM *)&a1;
  188.   exp1 = EXP (*pfl1);
  189.   mant1 = MANT (*pfl1) << 4;
  190.   if (SIGN(*pfl1))
  191.   if (*pfl1 & 0x80000000)
  192.     mant1 = -mant1;
  193.   /* check for zero args */
  194.   if (!*pfl1)
  195.     return (a2);
  196.  
  197.   expd = exp1 - exp2;
  198.   if (expd > 25)
  199.     return (a1);
  200.   if (expd < -25)
  201.     return (a2);
  202.  
  203.   if (expd < 0)
  204.     {
  205.       expd = -expd;
  206.       exp1 += expd;
  207.       mant1 >>= expd;
  208.     }
  209.   else
  210.     {
  211.       mant2 >>= expd;
  212.     }
  213.   mant1 += mant2;
  214.  
  215.   sign = false;
  216.  
  217.   if (mant1 < 0)
  218.     {
  219.       mant1 = -mant1;
  220.       sign = true;
  221.     }
  222.   else if (!mant1)
  223.     return (0);
  224.  
  225.   /* normalize */
  226.   while (mant1 < (HIDDEN<<4)) {
  227.     mant1 <<= 1;
  228.     exp1--;
  229.   }
  230.  
  231.   /* round off */
  232.   while (mant1 & 0xf0000000) {
  233.     if (mant1&1)
  234.       mant1 += 2;
  235.     mant1 >>= 1;
  236.     exp1++;
  237.   }
  238.  
  239.   /* turn off hidden bit */
  240.   mant1 &= ~(HIDDEN<<4);
  241.  
  242.   /* pack up and go home */
  243.   if (exp1 >= 0x100)
  244.     *pfl1 = (sign ? (SIGNBIT | __INFINITY) : __INFINITY);
  245.   else if (exp1 < 0)
  246.     *pfl1 = 0;
  247.   else
  248.     *pfl1 = PACK (sign ? SIGNBIT : 0 , exp1, mant1>>4);
  249.   return (a1);
  250. }
  251.  
  252. #endif
  253.