Squashed commit of 'sdl-hidd' branch
[cake.git] / rom / mathffp / spflt.c
blobdfd6e772a6a87f626dbbcf029be674b58ece3256
1 /*
2 Copyright © 1995-2004, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "mathffp_intern.h"
8 /*
9 FUNCTION
11 RESULT
12 FFP number
14 Flags:
15 zero : result is zero
16 negative : result is negative
17 overflow : ffp is not exactly the integer
19 NOTES
21 EXAMPLE
23 BUGS
25 SEE ALSO
27 INTERNALS
28 Return zero for x == 0.<br/>
29 If <code>x < 0</code> set the sign-bit and calculate the absolute value
30 of x.<br/>
31 Find out which bit is the highest-set bit. If the number
32 of that bit <code>> 24</code> then the result has the highest bit
33 of the mantisse set to one and the exponent equals the
34 number of the bit + 2. This is due to the fact that we only
35 have 24 bits for the mantisse.
36 Otherwise rotate the given integer by
37 <code>(32 - (number of highes set bit + 1))</code> bits to the left and
38 calculate the result from that.
40 HISTORY
44 AROS_LH1(float, SPFlt,
45 AROS_LHA(LONG, inum, D0),
46 struct LibHeader *, MathBase, 6, Mathffp
49 AROS_LIBFUNC_INIT
51 BYTE Exponent = 0;
52 LONG TestMask = 0xFFFFFFFF;
53 LONG Res = 0;
55 kprintf("SPFlt(%d)=",inum);
57 if (inum == 0)
59 SetSR(Zero_Bit, Zero_Bit | Negative_Bit | Overflow_Bit);
60 kprintf("0\n");
61 return 0;
64 if (inum < 0)
66 Res = FFPSign_Mask;
67 inum = -inum;
69 /* find out which is the number of the highes set bit */
70 while (TestMask & inum)
72 Exponent ++;
73 TestMask <<= 1;
76 /* Exponent = number of highest set bit + 1 */
78 inum <<= (32 - Exponent);
79 if ((char) inum < 0) inum +=0x100;
80 inum &= FFPMantisse_Mask;
82 /* adapt the exponent to the ffp format */
83 Exponent += 0x40;
84 Res |= inum | Exponent;
85 if ((char) Res < 0)
87 SetSR(Negative_Bit, Zero_Bit | Negative_Bit | Overflow_Bit);
90 if (Exponent > (25 + 0x40))
92 Res |= 0x80000000;
93 SetSR(Overflow_Bit, Zero_Bit | Negative_Bit | Overflow_Bit);
96 kprintf("%x\n",Res);
98 return Res;
100 AROS_LIBFUNC_EXIT