1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * These math functions are taken from newlib-nano-2, the newlib/libm/math
5  * directory, available from https://github.com/32bitmicro/newlib-nano-2.
6  *
7  * Appropriate copyright headers are reproduced below.
8  */
9 
10 /* sf_cos.c -- float version of s_cos.c.
11  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
12  */
13 
14 /*
15  * ====================================================
16  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
17  *
18  * Developed at SunPro, a Sun Microsystems, Inc. business.
19  * Permission to use, copy, modify, and distribute this
20  * software is freely granted, provided that this notice
21  * is preserved.
22  * ====================================================
23  */
24 
25 #include "fdlibm.h"
26 
27 #ifdef __STDC__
cosf(float x)28 	float cosf(float x)
29 #else
30 	float cosf(x)
31 	float x;
32 #endif
33 {
34 	float y[2],z=0.0;
35 	__int32_t n,ix;
36 
37 	GET_FLOAT_WORD(ix,x);
38 
39     /* |x| ~< pi/4 */
40 	ix &= 0x7fffffff;
41 	if(ix <= 0x3f490fd8) return __kernel_cosf(x,z);
42 
43     /* cos(Inf or NaN) is NaN */
44 	else if (!FLT_UWORD_IS_FINITE(ix)) return x-x;
45 
46     /* argument reduction needed */
47 	else {
48 	    n = __ieee754_rem_pio2f(x,y);
49 	    switch(n&3) {
50 		case 0: return  __kernel_cosf(y[0],y[1]);
51 		case 1: return -__kernel_sinf(y[0],y[1],1);
52 		case 2: return -__kernel_cosf(y[0],y[1]);
53 		default:
54 		        return  __kernel_sinf(y[0],y[1],1);
55 	    }
56 	}
57 }
58 
59 #ifdef _DOUBLE_IS_32BITS
60 
61 #ifdef __STDC__
cos(double x)62 	double cos(double x)
63 #else
64 	double cos(x)
65 	double x;
66 #endif
67 {
68 	return (double) cosf((float) x);
69 }
70 
71 #endif /* defined(_DOUBLE_IS_32BITS) */
72