wf_lgamma.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /* wf_lgamma.c -- float version of w_lgamma.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. */
  23. #include "fdlibm.h"
  24. #define _IEEE_LIBM 1
  25. //#include <reent.h>
  26. //#include <errno.h>
  27. #ifdef __STDC__
  28. float lgammaf(float x)
  29. #else
  30. float lgammaf(x)
  31. float x;
  32. #endif
  33. {
  34. #ifdef _IEEE_LIBM
  35. int sign;
  36. return __ieee754_lgammaf_r(x,&sign);
  37. #else
  38. float y;
  39. struct exception exc;
  40. y = __ieee754_lgammaf_r(x,&(_REENT_SIGNGAM(_REENT)));
  41. if(_LIB_VERSION == _IEEE_) return y;
  42. if(!finitef(y)&&finitef(x)) {
  43. #ifndef HUGE_VAL
  44. #define HUGE_VAL inf
  45. double inf = 0.0;
  46. SET_HIGH_WORD(inf,0x7ff00000); /* set inf to infinite */
  47. #endif
  48. exc.name = "lgammaf";
  49. exc.err = 0;
  50. exc.arg1 = exc.arg2 = (double)x;
  51. if (_LIB_VERSION == _SVID_)
  52. exc.retval = HUGE;
  53. else
  54. exc.retval = HUGE_VAL;
  55. if(floorf(x)==x&&x<=(float)0.0) {
  56. /* lgammaf(-integer) */
  57. exc.type = SING;
  58. if (_LIB_VERSION == _POSIX_)
  59. errno = EDOM;
  60. else if (!matherr(&exc)) {
  61. errno = EDOM;
  62. }
  63. } else {
  64. /* lgammaf(finite) overflow */
  65. exc.type = OVERFLOW;
  66. if (_LIB_VERSION == _POSIX_)
  67. errno = ERANGE;
  68. else if (!matherr(&exc)) {
  69. errno = ERANGE;
  70. }
  71. }
  72. if (exc.err != 0)
  73. errno = exc.err;
  74. return (float)exc.retval;
  75. } else
  76. return y;
  77. #endif
  78. }
  79. #ifdef _DOUBLE_IS_32BITS
  80. #ifdef __STDC__
  81. double lgamma(double x)
  82. #else
  83. double lgamma(x)
  84. double x;
  85. #endif
  86. {
  87. return (double) lgammaf((float) x);
  88. }
  89. #endif /* defined(_DOUBLE_IS_32BITS) */