1f3841c578d575c23aaa89a45aa87fc1e35f57f7
[fw/sdcc] / device / lib / pic / libsdcc / ulong2fs.c
1 /*
2 ** libgcc support for software floating point.
3 ** Copyright (C) 1991 by Pipeline Associates, Inc.  All rights reserved.
4 ** Permission is granted to do *anything* you want with this file,
5 ** commercial or otherwise, provided this message remains intact.  So there!
6 ** I would appreciate receiving any updates/patches/changes that anyone
7 ** makes, and am willing to be the repository for said changes (am I
8 ** making a big mistake?).
9 **
10 ** Pat Wood
11 ** Pipeline Associates, Inc.
12 ** pipeline!phw@motown.com or
13 ** sun!pipeline!phw or
14 ** uunet!motown!pipeline!phw
15 */
16
17 /*
18 ** $Id$
19 */
20
21 /* (c)2000/2001: hacked a little by johan.knol@iduna.nl for sdcc */
22
23 #include <float.h>
24
25 union float_long
26   {
27     float f;
28     long l;
29   };
30
31 float __ulong2fs (unsigned long a ) _FS_REENTRANT
32 {
33   int exp = 24 + EXCESS;
34   FS_STATIC volatile union float_long fl;
35
36   if (!a)
37     {
38       return 0.0;
39     }
40
41   while (a & NORM) 
42     {
43       // we lose accuracy here
44       a >>= 1;
45       exp++;
46     }
47   
48
49   if(a < HIDDEN) {
50         do {
51                 a<<=1;
52                 exp--;
53         } while (a < HIDDEN);
54   }
55
56 #if 0
57   while (a < HIDDEN) {
58       a <<= 1;
59       exp--;
60     }
61 #endif
62
63 #if 1
64   if ((a&0x7fffff)==0x7fffff) {
65     a=0;
66     exp++;
67   }
68 #endif
69
70   a &= ~HIDDEN ;
71   /* pack up and go home */
72   fl.l = PACK(0,(unsigned long)exp, a);
73
74   return (fl.f);
75 }