sf_frexp.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_frexp.c -- float version of s_frexp.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. static const float
  25. #else
  26. static float
  27. #endif
  28. two25 = 3.3554432000e+07; /* 0x4c000000 */
  29. #ifdef __STDC__
  30. float frexpf(float x, int *eptr)
  31. #else
  32. float frexpf(x, eptr)
  33. float x; int *eptr;
  34. #endif
  35. {
  36. __int32_t hx, ix;
  37. GET_FLOAT_WORD(hx,x);
  38. ix = 0x7fffffff&hx;
  39. *eptr = 0;
  40. if(!FLT_UWORD_IS_FINITE(ix)||FLT_UWORD_IS_ZERO(ix)) return x; /* 0,inf,nan */
  41. if (FLT_UWORD_IS_SUBNORMAL(ix)) { /* subnormal */
  42. x *= two25;
  43. GET_FLOAT_WORD(hx,x);
  44. ix = hx&0x7fffffff;
  45. *eptr = -25;
  46. }
  47. *eptr += (ix>>23)-126;
  48. hx = (hx&0x807fffff)|0x3f000000;
  49. SET_FLOAT_WORD(x,hx);
  50. return x;
  51. }
  52. #ifdef _DOUBLE_IS_32BITS
  53. #ifdef __STDC__
  54. double frexp(double x, int *eptr)
  55. #else
  56. double frexp(x, eptr)
  57. double x; int *eptr;
  58. #endif
  59. {
  60. return (double) frexpf((float) x, eptr);
  61. }
  62. #endif /* defined(_DOUBLE_IS_32BITS) */