From bacbb4255c9408e3ce669563f2fb1d4067947c1a Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 26 Jan 2015 18:52:28 -0600 Subject: Add math library support for copysign. From Brennan Ashton --- apps/interpreters/micropython/micropython_main.c | 10 ---- nuttx/include/nuttx/math.h | 8 ++++ nuttx/libc/math/Make.defs | 6 +-- nuttx/libc/math/lib_copysign.c | 59 ++++++++++++++++++++++++ nuttx/libc/math/lib_copysignf.c | 57 +++++++++++++++++++++++ nuttx/libc/math/lib_copysignl.c | 59 ++++++++++++++++++++++++ 6 files changed, 186 insertions(+), 13 deletions(-) create mode 100644 nuttx/libc/math/lib_copysign.c create mode 100644 nuttx/libc/math/lib_copysignf.c create mode 100644 nuttx/libc/math/lib_copysignl.c diff --git a/apps/interpreters/micropython/micropython_main.c b/apps/interpreters/micropython/micropython_main.c index d66402527..24bb12edf 100644 --- a/apps/interpreters/micropython/micropython_main.c +++ b/apps/interpreters/micropython/micropython_main.c @@ -149,16 +149,6 @@ float nanf(FAR const char *tagp) return 0; } -float copysignf(float x, float y) -{ - if (y < 0) - { - return -fabsf(x); - } - - return fabsf(x); -} - float truncf(float x) { union diff --git a/nuttx/include/nuttx/math.h b/nuttx/include/nuttx/math.h index 281b87ed5..b2ddcc4bb 100644 --- a/nuttx/include/nuttx/math.h +++ b/nuttx/include/nuttx/math.h @@ -369,6 +369,14 @@ long double erfl (long double x); #define erfcl(x) (1 - erfl(x)) #endif +float copysignf (float x, float y); +#if CONFIG_HAVE_DOUBLE +double copysign (double x, double y); +#endif +#ifdef CONFIG_HAVE_LONG_DOUBLE +long double copysignl (long double x, long double y); +#endif + #if defined(__cplusplus) } #endif diff --git a/nuttx/libc/math/Make.defs b/nuttx/libc/math/Make.defs index 0e40744a0..5f386f0ac 100644 --- a/nuttx/libc/math/Make.defs +++ b/nuttx/libc/math/Make.defs @@ -41,19 +41,19 @@ CSRCS += lib_acosf.c lib_asinf.c lib_atan2f.c lib_atanf.c lib_ceilf.c lib_cosf.c CSRCS += lib_coshf.c lib_expf.c lib_fabsf.c lib_floorf.c lib_fmodf.c lib_frexpf.c CSRCS += lib_ldexpf.c lib_logf.c lib_log10f.c lib_log2f.c lib_modff.c lib_powf.c CSRCS += lib_rintf.c lib_roundf.c lib_sinf.c lib_sinhf.c lib_sqrtf.c lib_tanf.c -CSRCS += lib_tanhf.c lib_asinhf.c lib_acoshf.c lib_atanhf.c lib_erff.c +CSRCS += lib_tanhf.c lib_asinhf.c lib_acoshf.c lib_atanhf.c lib_erff.c lib_copysignf.c CSRCS += lib_acos.c lib_asin.c lib_atan.c lib_atan2.c lib_ceil.c lib_cos.c CSRCS += lib_cosh.c lib_exp.c lib_fabs.c lib_floor.c lib_fmod.c lib_frexp.c CSRCS += lib_ldexp.c lib_log.c lib_log10.c lib_log2.c lib_modf.c lib_pow.c CSRCS += lib_rint.c lib_round.c lib_sin.c lib_sinh.c lib_sqrt.c lib_tan.c -CSRCS += lib_tanh.c lib_asinh.c lib_acosh.c lib_atanh.c lib_erf.c +CSRCS += lib_tanh.c lib_asinh.c lib_acosh.c lib_atanh.c lib_erf.c lib_copysign.c CSRCS += lib_acosl.c lib_asinl.c lib_atan2l.c lib_atanl.c lib_ceill.c lib_cosl.c CSRCS += lib_coshl.c lib_expl.c lib_fabsl.c lib_floorl.c lib_fmodl.c lib_frexpl.c CSRCS += lib_ldexpl.c lib_logl.c lib_log10l.c lib_log2l.c lib_modfl.c lib_powl.c CSRCS += lib_rintl.c lib_roundl.c lib_sinl.c lib_sinhl.c lib_sqrtl.c lib_tanl.c -CSRCS += lib_tanhl.c lib_asinhl.c lib_acoshl.c lib_atanhl.c lib_erfl.c +CSRCS += lib_tanhl.c lib_asinhl.c lib_acoshl.c lib_atanhl.c lib_erfl.c lib_copysignl.c CSRCS += lib_libexpi.c lib_libsqrtapprox.c diff --git a/nuttx/libc/math/lib_copysign.c b/nuttx/libc/math/lib_copysign.c new file mode 100644 index 000000000..dde9cb198 --- /dev/null +++ b/nuttx/libc/math/lib_copysign.c @@ -0,0 +1,59 @@ +/**************************************************************************** + * libc/math/lib_copysign.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Authors: Gregory Nutt + * Dave Marples + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include +#include + +#include + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#ifdef CONFIG_HAVE_DOUBLE +double copysign(double x, double y) +{ + if (y < 0) + { + return -fabs(x); + } + return fabs(x); +} +#endif diff --git a/nuttx/libc/math/lib_copysignf.c b/nuttx/libc/math/lib_copysignf.c new file mode 100644 index 000000000..6eb63e148 --- /dev/null +++ b/nuttx/libc/math/lib_copysignf.c @@ -0,0 +1,57 @@ +/**************************************************************************** + * libc/math/lib_copysignf.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Authors: Gregory Nutt + * Dave Marples + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include +#include + +#include + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float copysignf(float x, float y) +{ + if (y < 0) + { + return -fabsf(x); + } + return fabsf(x); +} diff --git a/nuttx/libc/math/lib_copysignl.c b/nuttx/libc/math/lib_copysignl.c new file mode 100644 index 000000000..29c145a72 --- /dev/null +++ b/nuttx/libc/math/lib_copysignl.c @@ -0,0 +1,59 @@ +/**************************************************************************** + * libc/math/lib_copysignl.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Authors: Gregory Nutt + * Dave Marples + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include +#include + +#include + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#ifdef CONFIG_HAVE_LONG_DOUBLE +long double copysignl(long double x, long double y) +{ + if (y < 0) + { + return -fabsl(x); + } + return fabsl(x); +} +#endif -- cgit v1.2.3 From 75c95457ede503b3f383c3e2df41239981221c06 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 26 Jan 2015 18:55:17 -0600 Subject: Cosmetic --- nuttx/libc/math/lib_copysign.c | 1 + nuttx/libc/math/lib_copysignf.c | 1 + nuttx/libc/math/lib_copysignl.c | 1 + 3 files changed, 3 insertions(+) diff --git a/nuttx/libc/math/lib_copysign.c b/nuttx/libc/math/lib_copysign.c index dde9cb198..ee62504db 100644 --- a/nuttx/libc/math/lib_copysign.c +++ b/nuttx/libc/math/lib_copysign.c @@ -54,6 +54,7 @@ double copysign(double x, double y) { return -fabs(x); } + return fabs(x); } #endif diff --git a/nuttx/libc/math/lib_copysignf.c b/nuttx/libc/math/lib_copysignf.c index 6eb63e148..0d12640cc 100644 --- a/nuttx/libc/math/lib_copysignf.c +++ b/nuttx/libc/math/lib_copysignf.c @@ -53,5 +53,6 @@ float copysignf(float x, float y) { return -fabsf(x); } + return fabsf(x); } diff --git a/nuttx/libc/math/lib_copysignl.c b/nuttx/libc/math/lib_copysignl.c index 29c145a72..3a49713b5 100644 --- a/nuttx/libc/math/lib_copysignl.c +++ b/nuttx/libc/math/lib_copysignl.c @@ -54,6 +54,7 @@ long double copysignl(long double x, long double y) { return -fabsl(x); } + return fabsl(x); } #endif -- cgit v1.2.3 From 6461f948f2f57e6d434c33ace44ea25e70d4c3fb Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 26 Jan 2015 19:00:35 -0600 Subject: Add math library support for trunc functions. From Brennan Ashton. --- apps/interpreters/micropython/micropython_main.c | 33 -------- nuttx/include/nuttx/math.h | 8 ++ nuttx/libc/math/Make.defs | 3 + nuttx/libc/math/lib_trunc.c | 75 ++++++++++++++++++ nuttx/libc/math/lib_truncf.c | 73 ++++++++++++++++++ nuttx/libc/math/lib_truncl.c | 97 ++++++++++++++++++++++++ 6 files changed, 256 insertions(+), 33 deletions(-) create mode 100644 nuttx/libc/math/lib_trunc.c create mode 100644 nuttx/libc/math/lib_truncf.c create mode 100644 nuttx/libc/math/lib_truncl.c diff --git a/apps/interpreters/micropython/micropython_main.c b/apps/interpreters/micropython/micropython_main.c index 24bb12edf..0d68e815b 100644 --- a/apps/interpreters/micropython/micropython_main.c +++ b/apps/interpreters/micropython/micropython_main.c @@ -149,39 +149,6 @@ float nanf(FAR const char *tagp) return 0; } -float truncf(float x) -{ - union - { - float f; - uint32_t i; - } u = - { - x}; - int e = (int)(u.i >> 23 & 0xff) - 0x7f + 9; - uint32_t m; - - if (e >= 23 + 9) - { - return x; - } - - if (e < 9) - { - e = 1; - } - - m = -1U >> e; - if ((u.i & m) == 0) - { - return x; - } - - FORCE_EVAL(x + 0x1p120f); - u.i &= ~m; - return u.f; -} - /**************************************************************************** * mp_import_stat ****************************************************************************/ diff --git a/nuttx/include/nuttx/math.h b/nuttx/include/nuttx/math.h index b2ddcc4bb..89223531e 100644 --- a/nuttx/include/nuttx/math.h +++ b/nuttx/include/nuttx/math.h @@ -377,6 +377,14 @@ double copysign (double x, double y); long double copysignl (long double x, long double y); #endif +float truncf (float x); +#if CONFIG_HAVE_DOUBLE +double trunc (double x); +#endif +#ifdef CONFIG_HAVE_LONG_DOUBLE +long double truncl (long double x); +#endif + #if defined(__cplusplus) } #endif diff --git a/nuttx/libc/math/Make.defs b/nuttx/libc/math/Make.defs index 5f386f0ac..cf25a1ec7 100644 --- a/nuttx/libc/math/Make.defs +++ b/nuttx/libc/math/Make.defs @@ -42,18 +42,21 @@ CSRCS += lib_coshf.c lib_expf.c lib_fabsf.c lib_floorf.c lib_fmodf.c lib_frexpf CSRCS += lib_ldexpf.c lib_logf.c lib_log10f.c lib_log2f.c lib_modff.c lib_powf.c CSRCS += lib_rintf.c lib_roundf.c lib_sinf.c lib_sinhf.c lib_sqrtf.c lib_tanf.c CSRCS += lib_tanhf.c lib_asinhf.c lib_acoshf.c lib_atanhf.c lib_erff.c lib_copysignf.c +CSRCS += lib_truncf.c CSRCS += lib_acos.c lib_asin.c lib_atan.c lib_atan2.c lib_ceil.c lib_cos.c CSRCS += lib_cosh.c lib_exp.c lib_fabs.c lib_floor.c lib_fmod.c lib_frexp.c CSRCS += lib_ldexp.c lib_log.c lib_log10.c lib_log2.c lib_modf.c lib_pow.c CSRCS += lib_rint.c lib_round.c lib_sin.c lib_sinh.c lib_sqrt.c lib_tan.c CSRCS += lib_tanh.c lib_asinh.c lib_acosh.c lib_atanh.c lib_erf.c lib_copysign.c +CSRCS += lib_trunc.c CSRCS += lib_acosl.c lib_asinl.c lib_atan2l.c lib_atanl.c lib_ceill.c lib_cosl.c CSRCS += lib_coshl.c lib_expl.c lib_fabsl.c lib_floorl.c lib_fmodl.c lib_frexpl.c CSRCS += lib_ldexpl.c lib_logl.c lib_log10l.c lib_log2l.c lib_modfl.c lib_powl.c CSRCS += lib_rintl.c lib_roundl.c lib_sinl.c lib_sinhl.c lib_sqrtl.c lib_tanl.c CSRCS += lib_tanhl.c lib_asinhl.c lib_acoshl.c lib_atanhl.c lib_erfl.c lib_copysignl.c +CSRCS += lib_truncl.c CSRCS += lib_libexpi.c lib_libsqrtapprox.c diff --git a/nuttx/libc/math/lib_trunc.c b/nuttx/libc/math/lib_trunc.c new file mode 100644 index 000000000..1de0f4b52 --- /dev/null +++ b/nuttx/libc/math/lib_trunc.c @@ -0,0 +1,75 @@ +/**************************************************************************** + * libc/math/lib_trunc.c + * + * This implementation is derived from the musl library under the MIT License + * + * Copyright © 2005-2014 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + ****************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include +#include + +#include +#include + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#ifdef CONFIG_HAVE_DOUBLE +double trunc(double x) +{ + union {double f; uint64_t i;} u = {x}; + int e = (int)(u.i >> 52 & 0x7ff) - 0x3ff + 12; + uint64_t m; + volatile float __x; + + if (e >= 52 + 12) + { + return x; + } + + if (e < 12) + { + e = 1; + } + + m = -1ull >> e; + if ((u.i & m) == 0) + { + return x; + } + + /* Force Evaluation */ + + __x = (x + 0x1p120f); + (void)__x; + + u.i &= ~m; + return u.f; +} +#endif diff --git a/nuttx/libc/math/lib_truncf.c b/nuttx/libc/math/lib_truncf.c new file mode 100644 index 000000000..6093de733 --- /dev/null +++ b/nuttx/libc/math/lib_truncf.c @@ -0,0 +1,73 @@ +/**************************************************************************** + * libc/math/lib_truncf.c + * + * This implementation is derived from the musl library under the MIT License + * + * Copyright © 2005-2014 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + ****************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include +#include + +#include +#include + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float truncf(float x) +{ + union {float f; uint32_t i;} u = {x}; + int e = (int)(u.i >> 23 & 0xff) - 0x7f + 9; + uint32_t m; + volatile float __x; + + if (e >= 23 + 9) + { + return x; + } + + if (e < 9) + { + e = 1; + } + + m = -1u >> e; + if ((u.i & m) == 0) + { + return x; + } + + /* Force Eval */ + + __x = (x + 0x1p120f); + (void)__x; + + u.i &= ~m; + return u.f; +} diff --git a/nuttx/libc/math/lib_truncl.c b/nuttx/libc/math/lib_truncl.c new file mode 100644 index 000000000..29f940206 --- /dev/null +++ b/nuttx/libc/math/lib_truncl.c @@ -0,0 +1,97 @@ +/**************************************************************************** + * libc/math/lib_truncl.c + * + * This implementation is derived from the musl library under the MIT License + * + * Copyright © 2005-2014 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + ****************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include +#include + +#include +#include +#include + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#ifdef CONFIG_HAVE_LONG_DOUBLE +static const long double toint = 1 / LDBL_EPSILON; + +/* FIXME This will only work if long double is 64 bit and little endian */ + +union ldshape +{ + long double f; + struct + { + uint64_t m; + uint16_t se; + } i; +}; + +long double truncl(long double x) +{ + union ldshape u = {x}; + int e = u.i.se & 0x7fff; + int s = u.i.se >> 15; + long double y; + volatile long double __x; + + if (e >= 0x3fff + LDBL_MANT_DIG - 1) + { + return x; + } + + if (e <= 0x3fff - 1) + { + /* Force Eval */ + + __x = (x + 0x1p120f); + (void)__x; + return x*0; + } + + /* y = int(|x|) - |x|, where int(|x|) is an integer neighbor of |x| */ + + if (s) + { + x = -x; + } + + y = x + toint - toint - x; + if (y > 0) + { + y -= 1; + } + + x += y; + return s ? -x : x; +} +#endif -- cgit v1.2.3 From 5066ce663316b83889afba55a5c81ae91f6a3500 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 26 Jan 2015 19:03:38 -0600 Subject: Add math library defines for nan functions. From Brennan Ashton. --- apps/interpreters/micropython/micropython_main.c | 6 ------ nuttx/include/nuttx/math.h | 8 ++++++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/interpreters/micropython/micropython_main.c b/apps/interpreters/micropython/micropython_main.c index 0d68e815b..f16c954e7 100644 --- a/apps/interpreters/micropython/micropython_main.c +++ b/apps/interpreters/micropython/micropython_main.c @@ -143,12 +143,6 @@ void do_str(FAR const char *src) * Public Functions ****************************************************************************/ -float nanf(FAR const char *tagp) -{ - (void)tagp; - return 0; -} - /**************************************************************************** * mp_import_stat ****************************************************************************/ diff --git a/nuttx/include/nuttx/math.h b/nuttx/include/nuttx/math.h index 89223531e..40990352a 100644 --- a/nuttx/include/nuttx/math.h +++ b/nuttx/include/nuttx/math.h @@ -385,6 +385,14 @@ double trunc (double x); long double truncl (long double x); #endif +#define nanf(x) ((float)(NAN)) +#ifdef CONFIG_HAVE_DOUBLE +#define nan(x) ((double)(NAN)) +#endif +#ifdef CONFIG_HAVE_LONG_DOUBLE +#define nanl(x) ((long double)(NAN)) +#endif + #if defined(__cplusplus) } #endif -- cgit v1.2.3 From 01ac044d75b9a927f51dc499697b19309761139e Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 26 Jan 2015 19:08:07 -0600 Subject: Update ChangeLogs --- apps/ChangeLog.txt | 3 +++ nuttx/ChangeLog | 3 +++ 2 files changed, 6 insertions(+) diff --git a/apps/ChangeLog.txt b/apps/ChangeLog.txt index a6761c905..9be1899a3 100644 --- a/apps/ChangeLog.txt +++ b/apps/ChangeLog.txt @@ -1196,3 +1196,6 @@ * apps/nsh: Clean up network status presentation for IPv6 (2015-01-24). 7.8 2015-xx-xx Gregory Nutt + + * apps/interpreters/micropython: Add math library defines for nan(), + copysign(), and trunc() functions. From Brennan Ashton (2015-01-26). diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index 9ae410449..52d1466b4 100755 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -9521,3 +9521,6 @@ From Brennan Ashton (2015-01-26). * include/nuttx/math.h and libc/math/lib_erf*.c: Add error function to math library. From Brennan Ashton (2015-01-26). + * include/nuttx/math.h and libc/math: Add math library defines for + nan(), copysign(), and trunc() functions. From Brennan Ashton + (2015-01-26). -- cgit v1.2.3