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