sf_sin.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /* sf_sin.c -- float version of s_sin.c.
  10. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  11. */
  12. /*
  13. * ====================================================
  14. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  15. *
  16. * Developed at SunPro, a Sun Microsystems, Inc. business.
  17. * Permission to use, copy, modify, and distribute this
  18. * software is freely granted, provided that this notice
  19. * is preserved.
  20. * ====================================================
  21. */
  22. #include "fdlibm.h"
  23. #ifdef __STDC__
  24. float sinf(float x)
  25. #else
  26. float sinf(x)
  27. float x;
  28. #endif
  29. {
  30. float y[2],z=0.0;
  31. __int32_t n,ix;
  32. GET_FLOAT_WORD(ix,x);
  33. /* |x| ~< pi/4 */
  34. ix &= 0x7fffffff;
  35. if(ix <= 0x3f490fd8) return __kernel_sinf(x,z,0);
  36. /* sin(Inf or NaN) is NaN */
  37. else if (!FLT_UWORD_IS_FINITE(ix)) return x-x;
  38. /* argument reduction needed */
  39. else {
  40. n = __ieee754_rem_pio2f(x,y);
  41. switch(n&3) {
  42. case 0: return __kernel_sinf(y[0],y[1],1);
  43. case 1: return __kernel_cosf(y[0],y[1]);
  44. case 2: return -__kernel_sinf(y[0],y[1],1);
  45. default:
  46. return -__kernel_cosf(y[0],y[1]);
  47. }
  48. }
  49. }
  50. #ifdef _DOUBLE_IS_32BITS
  51. #ifdef __STDC__
  52. double sin(double x)
  53. #else
  54. double sin(x)
  55. double x;
  56. #endif
  57. {
  58. return (double) sinf((float) x);
  59. }
  60. #endif /* defined(_DOUBLE_IS_32BITS) */