grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / libs / mathtrans / spcos.c
blob6eb8c23dd3e77031215a5b310fcc5e6c2f6c3a0a
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "mathtrans_intern.h"
8 /*****************************************************************************
10 NAME */
12 AROS_LH1(float, SPCos,
14 /* SYNOPSIS */
15 AROS_LHA(float, fnum1, D0),
17 /* LOCATION */
18 struct Library *, MathTransBase, 7, MathTrans)
20 /* FUNCTION
21 Calculate the cosine of a given ffp number in radians
23 INPUTS
25 RESULT
26 Motorola fast floating point number
28 flags:
29 zero : result is zero
30 negative : result is negative
31 overflow : 0
33 BUGS
35 INTERNALS
36 Algorithm for Calculation of cos(y):
38 z = floor ( |y| / pi );
39 y_1 = |y| - z * pi; => 0 <= y_1 < pi
41 if (y_1 > pi/2 ) then y_1 = pi - y_1;
43 => 0 <= y_1 < pi/2
45 Res = 1 - y^2/2! + y^4/4! - y^6/6! + y^8/8! - y^10/10! =
46 = 1 -(y^2(-1/2!+y^2(1/4!+y^2(-1/6!+y^2(1/8!-1/10!y^2)))));
48 if (z was an odd number)
49 Res = -Res;
51 if (y_1 was greater than pi/2 in the test above)
52 Res = -Res;
54 *****************************************************************************/
56 AROS_LIBFUNC_INIT
58 LONG z,Res,ysquared,yabs,tmp;
59 yabs = fnum1 & (FFPMantisse_Mask + FFPExponent_Mask);
61 if ((LONG)FFP_Pinfty == yabs)
63 SetSR(Overflow_Bit, Zero_Bit | Negative_Bit | Overflow_Bit);
64 return FFP_NAN;
67 z = SPFloor(SPDiv(pi, yabs));
68 tmp = SPMul(z,pi);
69 tmp |= FFPSign_Mask; /* tmp = -tmp; */
70 yabs = SPAdd(yabs, tmp);
72 if
74 (char)yabs > (char)pio2
75 && (yabs & FFPMantisse_Mask) > (pio2 & FFPMantisse_Mask)
78 yabs |= FFPSign_Mask;
79 yabs =SPAdd(pi, yabs);
80 tmp = TRUE;
82 else
84 tmp = FALSE;
87 ysquared = SPMul(yabs,yabs);
88 Res = SPAdd(cosf1,
89 SPMul(ysquared,
90 SPAdd(cosf2,
91 SPMul(ysquared,
92 SPAdd(cosf3,
93 SPMul(ysquared,
94 SPAdd(cosf4,
95 SPMul(ysquared,
96 SPAdd(cosf5,
97 SPMul(ysquared, cosf6))))))))));
99 if (0 == Res)
101 SetSR(Zero_Bit, Zero_Bit | Negative_Bit | Overflow_Bit);
102 return 0;
105 if (TRUE == intern_SPisodd(z)) Res ^= FFPSign_Mask;
107 if (TRUE == tmp) Res ^= FFPSign_Mask;
109 if ((char)Res < 0) SetSR(Negative_Bit, Zero_Bit | Negative_Bit | Overflow_Bit);
111 return Res;
113 AROS_LIBFUNC_EXIT