23123339a3a80f41db5be05724ae356e78d9e24b
[fw/sdcc] / device / lib / pic16 / libsdcc / float / 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 )
32 // reentrant
33 {
34   int exp = 24 + EXCESS;
35   volatile union float_long fl;
36
37   if (!a)
38     {
39       return 0.0;
40     }
41
42   while (a & NORM) 
43     {
44       // we lose accuracy here
45       a >>= 1;
46       exp++;
47     }
48   
49
50   if(a < HIDDEN) {
51         do {
52                 a<<=1;
53                 exp--;
54         } while (a > HIDDEN);
55   }
56
57 #if 0
58   while (a < HIDDEN) {
59       a <<= 1;
60       exp--;
61     }
62 #endif
63
64 #if 1
65   if ((a&0x7fffff)==0x7fffff) {
66     a=0;
67     exp++;
68   }
69 #endif
70
71   a &= ~HIDDEN ;
72   /* pack up and go home */
73   fl.l = PACK(0,(unsigned long)exp, a);
74
75   return (fl.f);
76 }