/* origin: FreeBSD /usr/src/lib/msun/src/s_cosf.c */1/*2* Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].3* Optimized by Bruce D. Evans.4*/5/*6* ====================================================7* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.8*9* Developed at SunPro, a Sun Microsystems, Inc. business.10* Permission to use, copy, modify, and distribute this11* software is freely granted, provided that this notice12* is preserved.13* ====================================================14*/1516#include "libm.h"1718/* Small multiples of pi/2 rounded to double precision. */19static const double20c1pio2 = 1*M_PI_2, /* 0x3FF921FB, 0x54442D18 */21c2pio2 = 2*M_PI_2, /* 0x400921FB, 0x54442D18 */22c3pio2 = 3*M_PI_2, /* 0x4012D97C, 0x7F3321D2 */23c4pio2 = 4*M_PI_2; /* 0x401921FB, 0x54442D18 */2425float __cdecl cosf(float x)26{27double y;28uint32_t ix;29unsigned n, sign;3031GET_FLOAT_WORD(ix, x);32sign = ix >> 31;33ix &= 0x7fffffff;3435if (ix <= 0x3f490fda) { /* |x| ~<= pi/4 */36if (ix < 0x39800000) { /* |x| < 2**-12 */37/* raise inexact if x != 0 */38FORCE_EVAL(x + 0x1p120f);39return 1.0f;40}41return __cosdf(x);42}43if (ix <= 0x407b53d1) { /* |x| ~<= 5*pi/4 */44if (ix > 0x4016cbe3) /* |x| ~> 3*pi/4 */45return -__cosdf(sign ? x+c2pio2 : x-c2pio2);46else {47if (sign)48return __sindf(x + c1pio2);49else50return __sindf(c1pio2 - x);51}52}53if (ix <= 0x40e231d5) { /* |x| ~<= 9*pi/4 */54if (ix > 0x40afeddf) /* |x| ~> 7*pi/4 */55return __cosdf(sign ? x+c4pio2 : x-c4pio2);56else {57if (sign)58return __sindf(-x - c3pio2);59else60return __sindf(x - c3pio2);61}62}6364/* cos(Inf or NaN) is NaN */65if (isinf(x))66return math_error(_DOMAIN, "cosf", x, 0, x - x);67if (ix >= 0x7f800000)68return x-x;6970/* general argument reduction needed */71n = __rem_pio2f(x,&y);72switch (n&3) {73case 0: return __cosdf(y);74case 1: return __sindf(-y);75case 2: return -__cosdf(y);76default:77return __sindf(y);78}79}808182