diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-10-28 18:42:09 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-10-28 18:42:09 +0000 |
commit | 0e190d4cf7f3d151592414d8bc3d3eaf77e0b0e8 (patch) | |
tree | 73b8411e1d00ef6df3dd5fa2ed717241174ae14b /nuttx/lib | |
parent | 5b7a650b16980bb2a395fa76df170d8948da72a9 (diff) | |
download | px4-firmware-0e190d4cf7f3d151592414d8bc3d3eaf77e0b0e8.tar.gz px4-firmware-0e190d4cf7f3d151592414d8bc3d3eaf77e0b0e8.tar.bz2 px4-firmware-0e190d4cf7f3d151592414d8bc3d3eaf77e0b0e8.zip |
lib/math files not follow coding standard; float, double, and long double versions in separate files to reduce size of dumb link
git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5270 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/lib')
77 files changed, 3859 insertions, 1185 deletions
diff --git a/nuttx/lib/Kconfig b/nuttx/lib/Kconfig index 77cf62665..440e9c3e2 100644 --- a/nuttx/lib/Kconfig +++ b/nuttx/lib/Kconfig @@ -30,27 +30,7 @@ config LIB_HOMEDIR ---help--- The home directory to use with operations like such as 'cd ~' -config LIBM - bool "Math library" - default n - depends on !ARCH_MATH_H - ---help--- - By default, no math library will be provided by NuttX. In this this case, it - is assumed that (1) no math library is required, or (2) you will be using the - math.h header file and the libm library provided by your toolchain. - - This is may be a very good choice is possible because your toolchain may have - have a highly optimized version of libm. - - Another possibility is that you have a custom, architecture-specific math - libary and that the corresponding math.h file resides at arch/<architecture>/include/math.h. - The option is selected via ARCH_MATH_H. If ARCH_MATH_H is selected,then the include/nuttx/math.h - header file will be copied to include/math.h where it can be used by your applications. - - If ARCH_MATH_H is not defined, then this option can be selected to build a generic, - math library built into NuttX. This math library comes from the Rhombus OS and - was written by Nick Johnson. The Rhombus OS math library port was contributed by - Darcy Gong. +source lib/math/Kconfig config NOPRINTF_FIELDWIDTH bool "Disable sprintf support fieldwidth" diff --git a/nuttx/lib/Makefile b/nuttx/lib/Makefile index 5c62967ce..406c2276e 100644 --- a/nuttx/lib/Makefile +++ b/nuttx/lib/Makefile @@ -50,6 +50,7 @@ include pthread/Make.defs include semaphore/Make.defs include signal/Make.defs include mqueue/Make.defs +include math/Make.defs include fixedmath/Make.defs include net/Make.defs include time/Make.defs diff --git a/nuttx/lib/lib_internal.h b/nuttx/lib/lib_internal.h index c3d9bfd18..5da0ac924 100644 --- a/nuttx/lib/lib_internal.h +++ b/nuttx/lib/lib_internal.h @@ -117,56 +117,56 @@ extern bool g_dbgenable; /* Defined in lib_streamsem.c */ #if CONFIG_NFILE_STREAMS > 0 -extern void stream_semtake(FAR struct streamlist *list); -extern void stream_semgive(FAR struct streamlist *list); +void stream_semtake(FAR struct streamlist *list); +void stream_semgive(FAR struct streamlist *list); #endif /* Defined in lib_libnoflush.c */ #ifdef CONFIG_STDIO_LINEBUFFER -extern int lib_noflush(FAR struct lib_outstream_s *this); +int lib_noflush(FAR struct lib_outstream_s *this); #endif /* Defined in lib_libsprintf.c */ -extern int lib_sprintf(FAR struct lib_outstream_s *obj, +int lib_sprintf(FAR struct lib_outstream_s *obj, const char *fmt, ...); /* Defined lib_libvsprintf.c */ -extern int lib_vsprintf(FAR struct lib_outstream_s *obj, - FAR const char *src, va_list ap); +int lib_vsprintf(FAR struct lib_outstream_s *obj, + FAR const char *src, va_list ap); /* Defined lib_rawprintf.c */ -extern int lib_rawvprintf(const char *src, va_list ap); +int lib_rawvprintf(const char *src, va_list ap); /* Defined lib_lowprintf.c */ -extern int lib_lowvprintf(const char *src, va_list ap); +int lib_lowvprintf(const char *src, va_list ap); /* Defined in lib_dtoa.c */ #ifdef CONFIG_LIBC_FLOATINGPOINT -extern char *__dtoa(double d, int mode, int ndigits, - int *decpt, int *sign, char **rve); +char *__dtoa(double d, int mode, int ndigits, int *decpt, int *sign, + char **rve); #endif /* Defined in lib_libwrite.c */ -extern ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream); +ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream); /* Defined in lib_libfread.c */ -extern ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream); +ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream); /* Defined in lib_libfflush.c */ -extern ssize_t lib_fflush(FAR FILE *stream, bool bforce); +ssize_t lib_fflush(FAR FILE *stream, bool bforce); /* Defined in lib_rdflush.c */ -extern int lib_rdflush(FAR FILE *stream); +int lib_rdflush(FAR FILE *stream); /* Defined in lib_wrflush.c */ @@ -175,25 +175,37 @@ int lib_wrflush(FAR FILE *stream); /* Defined in lib_sem.c */ #if CONFIG_STDIO_BUFFER_SIZE > 0 -extern void lib_sem_initialize(FAR struct file_struct *stream); -extern void lib_take_semaphore(FAR struct file_struct *stream); -extern void lib_give_semaphore(FAR struct file_struct *stream); +void lib_sem_initialize(FAR struct file_struct *stream); +void lib_take_semaphore(FAR struct file_struct *stream); +void lib_give_semaphore(FAR struct file_struct *stream); #endif /* Defined in lib_libgetbase.c */ -extern int lib_getbase(const char *nptr, const char **endptr); +int lib_getbase(const char *nptr, const char **endptr); /* Defined in lib_skipspace.c */ -extern void lib_skipspace(const char **pptr); +void lib_skipspace(const char **pptr); /* Defined in lib_isbasedigit.c */ -extern bool lib_isbasedigit(int ch, int base, int *value); +bool lib_isbasedigit(int ch, int base, int *value); /* Defined in lib_checkbase.c */ -extern int lib_checkbase(int base, const char **pptr); +int lib_checkbase(int base, const char **pptr); + +/* Defined in lib_expi.c */ + +#ifdef CONFIG_LIBM +double lib_expi(size_t n); +#endif + +/* Defined in lib_libsqrtapprox.c */ + +#ifdef CONFIG_LIBM +float lib_sqrtapprox(float x); +#endif #endif /* __LIB_LIB_INTERNAL_H */ diff --git a/nuttx/lib/math/Kconfig b/nuttx/lib/math/Kconfig index ae2bf3130..c24bfd53f 100644 --- a/nuttx/lib/math/Kconfig +++ b/nuttx/lib/math/Kconfig @@ -2,3 +2,25 @@ # For a description of the syntax of this configuration file, # see misc/tools/kconfig-language.txt. # + +config LIBM + bool "Math library" + default n + depends on !ARCH_MATH_H + ---help--- + By default, no math library will be provided by NuttX. In this this case, it + is assumed that (1) no math library is required, or (2) you will be using the + math.h header file and the libm library provided by your toolchain. + + This is may be a very good choice is possible because your toolchain may have + have a highly optimized version of libm. + + Another possibility is that you have a custom, architecture-specific math + libary and that the corresponding math.h file resides at arch/<architecture>/include/math.h. + The option is selected via ARCH_MATH_H. If ARCH_MATH_H is selected,then the include/nuttx/math.h + header file will be copied to include/math.h where it can be used by your applications. + + If ARCH_MATH_H is not defined, then this option can be selected to build a generic, + math library built into NuttX. This math library comes from the Rhombus OS and + was written by Nick Johnson. The Rhombus OS math library port was contributed by + Darcy Gong. diff --git a/nuttx/lib/math/Make.defs b/nuttx/lib/math/Make.defs index f656e1a73..8e2e60f12 100644 --- a/nuttx/lib/math/Make.defs +++ b/nuttx/lib/math/Make.defs @@ -33,17 +33,30 @@ # ############################################################################ -if ($(CONFIG_LIBM),y) +ifeq ($(CONFIG_LIBM),y) # Add the floating point math C files to the build +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_sinf.c lib_sinhf.c lib_sqrtf.c lib_tanf.c lib_tanhf.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 CRRCS += lib_ldexp.c lib_log.c lib_log10.c lib_log2.c lib_modf.c lib_pow.c CSRCS += lib_sin.c lib_sinh.c lib_sqrt.c lib_tan.c lib_tanh.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_sinl.c lib_sinhl.c lib_sqrtl.c lib_tanl.c lib_tanhl.c + +CSRCS += lib_libexpi.c lib_libsqrtapprox.c + # Add the floating point math directory to the build DEPPATH += --dep-path math VPATH += :math + endif diff --git a/nuttx/lib/math/Makefile b/nuttx/lib/math/Makefile deleted file mode 100644 index 56be0f0ab..000000000 --- a/nuttx/lib/math/Makefile +++ /dev/null @@ -1,100 +0,0 @@ -############################################################################ -# nuttx/lib/math/Makefile -# -# Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. -# Author: Gregory Nutt <gnutt@nuttx.org> -# -# 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. -# -############################################################################ - --include $(TOPDIR)/.config --include $(TOPDIR)/Make.defs -include $(APPDIR)/Make.defs - -# NSH Library - -ASRCS = -CSRCS = - -ifeq ($(CONFIG_MATHEXT_LIBRARY),y) -CSRCS += m_cos.c m_log2.c m_fmod.c m_sin.c m_fabs.c m_exp.c m_asin.c m_tan.c \ - m_modf.c m_ldexp.c m_acos.c m_frexp.c m_atan.c m_log.c m_tanh.c m_ceil.c \ - m_sinh.c m_sqrt.c m_pow.c m_cosh.c m_floor.c m_atan2.c m_log10.c -endif - -AOBJS = $(ASRCS:.S=$(OBJEXT)) -COBJS = $(CSRCS:.c=$(OBJEXT)) - -SRCS = $(ASRCS) $(CSRCS) -OBJS = $(AOBJS) $(COBJS) - -ifeq ($(WINTOOL),y) - BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}" -else - BIN = "$(APPDIR)/libapps$(LIBEXT)" -endif - -ROOTDEPPATH = --dep-path . -VPATH = - -# Build targets - -all: .built -.PHONY: context .depend depend clean distclean - -$(AOBJS): %$(OBJEXT): %.S - $(call ASSEMBLE, $<, $@) - -$(COBJS): %$(OBJEXT): %.c - $(call COMPILE, $<, $@) - -.built: $(OBJS) - @( for obj in $(OBJS) ; do \ - $(call ARCHIVE, $(BIN), $${obj}); \ - done ; ) - @touch .built - -context: - -.depend: Makefile $(SRCS) - @$(MKDEP) $(ROOTDEPPATH) \ - $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep - @touch $@ - -depend: .depend - -clean: - @rm -f *.o *~ .*.swp .built - $(call CLEAN) - -distclean: clean - @rm -f Make.dep .depend - --include Make.dep - diff --git a/nuttx/lib/math/lib_acos.c b/nuttx/lib/math/lib_acos.c index 83650d132..4c5aa67c0 100644 --- a/nuttx/lib/math/lib_acos.c +++ b/nuttx/lib/math/lib_acos.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_acos.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,19 +22,25 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float acosf(float x) { - return (M_PI_2 - asinf(x)); -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double acos(double x) { - return (M_PI_2 - asin(x)); -} +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ -long double acosl(long double x) { - return (M_PI_2 - asinl(x)); +#if CONFIG_HAVE_DOUBLE +double acos(double x) +{ + return (M_PI_2 - asin(x)); } +#endif diff --git a/nuttx/lib/math/lib_acosf.c b/nuttx/lib/math/lib_acosf.c new file mode 100644 index 000000000..e14a73a6e --- /dev/null +++ b/nuttx/lib/math/lib_acosf.c @@ -0,0 +1,41 @@ +/************************************************************************ + * lib/math/lib_acosf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float acosf(float x) +{ + return (M_PI_2 - asinf(x)); +} diff --git a/nuttx/lib/math/lib_acosl.c b/nuttx/lib/math/lib_acosl.c new file mode 100644 index 000000000..3b6fa8aa9 --- /dev/null +++ b/nuttx/lib/math/lib_acosl.c @@ -0,0 +1,46 @@ +/************************************************************************ + * lib/math/lib_acos.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double acosl(long double x) +{ + return (M_PI_2 - asinl(x)); +} +#endif diff --git a/nuttx/lib/math/lib_asin.c b/nuttx/lib/math/lib_asin.c index 9d72e8042..1469d1ed8 100644 --- a/nuttx/lib/math/lib_asin.c +++ b/nuttx/lib/math/lib_asin.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_sin.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,77 +22,48 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> - -float asinf(float x) { - long double y, y_sin, y_cos; - - y = 0; - - while (1) { - y_sin = sinf(y); - y_cos = cosf(y); - - if (y > M_PI_2 || y < -M_PI_2) { - y = fmodf(y, M_PI); - } - - if (y_sin + DBL_EPSILON >= x && y_sin - DBL_EPSILON <= x) { - break; - } - - y = y - (y_sin - x) / y_cos; - } - - return y; -} - -double asin(double x) { - long double y, y_sin, y_cos; - - y = 0; - - while (1) { - y_sin = sin(y); - y_cos = cos(y); +/************************************************************************ + * Included Files + ************************************************************************/ - if (y > M_PI_2 || y < -M_PI_2) { - y = fmod(y, M_PI); - } +#include <nuttx/config.h> +#include <nuttx/compiler.h> - if (y_sin + DBL_EPSILON >= x && y_sin - DBL_EPSILON <= x) { - break; - } - - y = y - (y_sin - x) / y_cos; - } +#include <math.h> +#include <float.h> - return y; -} +/************************************************************************ + * Public Functions + ************************************************************************/ -long double asinl(long double x) { - long double y, y_sin, y_cos; +#if CONFIG_HAVE_DOUBLE +double asin(double x) +{ + long double y, y_sin, y_cos; - y = 0; + y = 0; - while (1) { - y_sin = sinl(y); - y_cos = cosl(y); + while (1) + { + y_sin = sin(y); + y_cos = cos(y); - if (y > M_PI_2 || y < -M_PI_2) { - y = fmodl(y, M_PI); - } + if (y > M_PI_2 || y < -M_PI_2) + { + y = fmod(y, M_PI); + } - if (y_sin + LDBL_EPSILON >= x && y_sin - LDBL_EPSILON <= x) { - break; - } + if (y_sin + DBL_EPSILON >= x && y_sin - DBL_EPSILON <= x) + { + break; + } - y = y - (y_sin - x) / y_cos; - } + y = y - (y_sin - x) / y_cos; + } - return y; + return y; } - +#endif diff --git a/nuttx/lib/math/lib_asinf.c b/nuttx/lib/math/lib_asinf.c new file mode 100644 index 000000000..17669a934 --- /dev/null +++ b/nuttx/lib/math/lib_asinf.c @@ -0,0 +1,65 @@ +/************************************************************************ + * lib/math/lib_sinf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> +#include <float.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float asinf(float x) +{ + long double y, y_sin, y_cos; + + y = 0; + + while (1) + { + y_sin = sinf(y); + y_cos = cosf(y); + + if (y > M_PI_2 || y < -M_PI_2) + { + y = fmodf(y, M_PI); + } + + if (y_sin + FLT_EPSILON >= x && y_sin - FLT_EPSILON <= x) + { + break; + } + + y = y - (y_sin - x) / y_cos; + } + + return y; +} + diff --git a/nuttx/lib/math/lib_asinl.c b/nuttx/lib/math/lib_asinl.c new file mode 100644 index 000000000..bd1cb70ee --- /dev/null +++ b/nuttx/lib/math/lib_asinl.c @@ -0,0 +1,69 @@ +/************************************************************************ + * lib/math/lib_sinl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> +#include <float.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double asinl(long double x) +{ + long double y, y_sin, y_cos; + + y = 0; + + while (1) + { + y_sin = sinl(y); + y_cos = cosl(y); + + if (y > M_PI_2 || y < -M_PI_2) + { + y = fmodl(y, M_PI); + } + + if (y_sin + LDBL_EPSILON >= x && y_sin - LDBL_EPSILON <= x) + { + break; + } + + y = y - (y_sin - x) / y_cos; + } + + return y; +} +#endif diff --git a/nuttx/lib/math/lib_atan.c b/nuttx/lib/math/lib_atan.c index e909e5a8e..2b7f6e3a6 100644 --- a/nuttx/lib/math/lib_atan.c +++ b/nuttx/lib/math/lib_atan.c @@ -1,5 +1,15 @@ -/* - * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> +/************************************************************************ + * lib/math/lib_atan.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -12,21 +22,27 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> #include <stddef.h> #include <stdint.h> -float atanf(float x) { - return asinf(x / sqrtf(x * x + 1)); -} - -double atan(double x) { - return asin(x / sqrt(x * x + 1)); -} +/************************************************************************ + * Public Functions + ************************************************************************/ -long double atanl(long double x) { - return asinl(x / sqrtl(x * x + 1)); +#if CONFIG_HAVE_DOUBLE +double atan(double x) +{ + return asin(x / sqrt(x * x + 1)); } +#endif diff --git a/nuttx/lib/math/lib_atan2.c b/nuttx/lib/math/lib_atan2.c index b4ded0c16..bfb33657c 100644 --- a/nuttx/lib/math/lib_atan2.c +++ b/nuttx/lib/math/lib_atan2.c @@ -1,5 +1,15 @@ -/* - * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> +/************************************************************************ + * lib/math/lib_atan2.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -12,109 +22,65 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float atan2f(float y, float x) { - - if (y == 0.0) { - if (x >= 0.0) { - return 0.0; - } - else { - return M_PI; - } - } - else if (y > 0.0) { - if (x == 0.0) { - return M_PI_2; - } - else if (x > 0.0) { - return atanf(y / x); - } - else { - return M_PI - atanf(y / x); - } - } - else { - if (x == 0.0) { - return M_PI + M_PI_2; - } - else if (x > 0.0) { - return 2 * M_PI - atanf(y / x); - } - else { - return M_PI + atanf(y / x); - } - } -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double atan2(double y, double x) { - - if (y == 0.0) { - if (x >= 0.0) { - return 0.0; - } - else { - return M_PI; - } - } - else if (y > 0.0) { - if (x == 0.0) { - return M_PI_2; - } - else if (x > 0.0) { - return atan(y / x); - } - else { - return M_PI - atan(y / x); - } - } - else { - if (x == 0.0) { - return M_PI + M_PI_2; - } - else if (x > 0.0) { - return 2 * M_PI - atan(y / x); - } - else { - return M_PI + atan(y / x); - } - } -} +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ -long double atan2l(long double y, long double x) { - - if (y == 0.0) { - if (x >= 0.0) { - return 0.0; - } - else { - return M_PI; - } - } - else if (y > 0.0) { - if (x == 0.0) { - return M_PI_2; - } - else if (x > 0.0) { - return atanl(y / x); - } - else { - return M_PI - atanl(y / x); - } - } - else { - if (x == 0.0) { - return M_PI + M_PI_2; - } - else if (x > 0.0) { - return 2 * M_PI - atanl(y / x); - } - else { - return M_PI + atanl(y / x); - } - } +#if CONFIG_HAVE_DOUBLE +double atan2(double y, double x) +{ + if (y == 0.0) + { + if (x >= 0.0) + { + return 0.0; + } + else + { + return M_PI; + } + } + else if (y > 0.0) + { + if (x == 0.0) + { + return M_PI_2; + } + else if (x > 0.0) + { + return atan(y / x); + } + else + { + return M_PI - atan(y / x); + } + } + else + { + if (x == 0.0) + { + return M_PI + M_PI_2; + } + else if (x > 0.0) + { + return 2 * M_PI - atan(y / x); + } + else + { + return M_PI + atan(y / x); + } + } } +#endif diff --git a/nuttx/lib/math/lib_atan2f.c b/nuttx/lib/math/lib_atan2f.c new file mode 100644 index 000000000..a60d32c65 --- /dev/null +++ b/nuttx/lib/math/lib_atan2f.c @@ -0,0 +1,81 @@ +/************************************************************************ + * lib/math/lib_atan2f.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float atan2f(float y, float x) +{ + if (y == 0.0) + { + if (x >= 0.0) + { + return 0.0; + } + else + { + return M_PI; + } + } + else if (y > 0.0) + { + if (x == 0.0) + { + return M_PI_2; + } + else if (x > 0.0) + { + return atanf(y / x); + } + else + { + return M_PI - atanf(y / x); + } + } + else + { + if (x == 0.0) + { + return M_PI + M_PI_2; + } + else if (x > 0.0) + { + return 2 * M_PI - atanf(y / x); + } + else + { + return M_PI + atanf(y / x); + } + } +} diff --git a/nuttx/lib/math/lib_atan2l.c b/nuttx/lib/math/lib_atan2l.c new file mode 100644 index 000000000..d7891b628 --- /dev/null +++ b/nuttx/lib/math/lib_atan2l.c @@ -0,0 +1,87 @@ +/************************************************************************ + * lib/math/lib_atan2l.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double atan2l(long double y, long double x) +{ + + if (y == 0.0) + { + if (x >= 0.0) + { + return 0.0; + } + else + { + return M_PI; + } + } + else if (y > 0.0) + { + if (x == 0.0) + { + return M_PI_2; + } + else if (x > 0.0) + { + return atanl(y / x); + } + else + { + return M_PI - atanl(y / x); + } + } + else + { + if (x == 0.0) + { + return M_PI + M_PI_2; + } + else if (x > 0.0) + { + return 2 * M_PI - atanl(y / x); + } + else + { + return M_PI + atanl(y / x); + } + } +} +#endif diff --git a/nuttx/lib/math/lib_atanf.c b/nuttx/lib/math/lib_atanf.c new file mode 100644 index 000000000..7c540dd16 --- /dev/null +++ b/nuttx/lib/math/lib_atanf.c @@ -0,0 +1,43 @@ +/************************************************************************ + * lib/math/lib_atanf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> +#include <stddef.h> +#include <stdint.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float atanf(float x) +{ + return asinf(x / sqrtf(x * x + 1)); +} diff --git a/nuttx/lib/math/lib_atanl.c b/nuttx/lib/math/lib_atanl.c new file mode 100644 index 000000000..c8fa239f9 --- /dev/null +++ b/nuttx/lib/math/lib_atanl.c @@ -0,0 +1,48 @@ +/************************************************************************ + * lib/math/lib_atanl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> +#include <stddef.h> +#include <stdint.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double atanl(long double x) +{ + return asinl(x / sqrtl(x * x + 1)); +} +#endif diff --git a/nuttx/lib/math/lib_ceil.c b/nuttx/lib/math/lib_ceil.c index c2447c0b8..12e223128 100644 --- a/nuttx/lib/math/lib_ceil.c +++ b/nuttx/lib/math/lib_ceil.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_ceil.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,26 +22,31 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float ceilf(float x) { - modff(x, &x); - if (x > 0.0) x += 1.0; - return x; -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double ceil(double x) { - modf(x, &x); - if (x > 0.0) x += 1.0; - return x; -} +#include <math.h> -long double ceill(long double x) { - modfl(x, &x); - if (x > 0.0) x += 1.0; - return x; -} +/************************************************************************ + * Public Functions + ************************************************************************/ +#if CONFIG_HAVE_DOUBLE +double ceil(double x) +{ + modf(x, &x); + if (x > 0.0) + { + x += 1.0; + } + + return x; +} +#endif diff --git a/nuttx/lib/math/lib_ceilf.c b/nuttx/lib/math/lib_ceilf.c new file mode 100644 index 000000000..0721ffc22 --- /dev/null +++ b/nuttx/lib/math/lib_ceilf.c @@ -0,0 +1,47 @@ +/************************************************************************ + * lib/math/lib_ceilf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float ceilf(float x) +{ + modff(x, &x); + if (x > 0.0) + { + x += 1.0; + } + + return x; +} diff --git a/nuttx/lib/math/lib_ceill.c b/nuttx/lib/math/lib_ceill.c new file mode 100644 index 000000000..5a0010aa2 --- /dev/null +++ b/nuttx/lib/math/lib_ceill.c @@ -0,0 +1,52 @@ +/************************************************************************ + * lib/math/lib_ceil;.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double ceill(long double x) +{ + modfl(x, &x); + if (x > 0.0) + { + x += 1.0; + } + + return x; +} +#endif diff --git a/nuttx/lib/math/lib_cos.c b/nuttx/lib/math/lib_cos.c index f8732b041..09808bd9f 100644 --- a/nuttx/lib/math/lib_cos.c +++ b/nuttx/lib/math/lib_cos.c @@ -1,5 +1,15 @@ -/* - * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> +/************************************************************************ + * lib/math/lib_cos.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -12,20 +22,25 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <stdint.h> -#include <float.h> -#include <apps/math.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float cosf(float x) { - return sinf(x + M_PI_2); -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double cos(double x) { - return sin(x + M_PI_2); -} +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ -long double cosl(long double x) { - return sinl(x + M_PI_2); +#if CONFIG_HAVE_DOUBLE +double cos(double x) +{ + return sin(x + M_PI_2); } +#endif diff --git a/nuttx/lib/math/lib_cosf.c b/nuttx/lib/math/lib_cosf.c new file mode 100644 index 000000000..093a8a002 --- /dev/null +++ b/nuttx/lib/math/lib_cosf.c @@ -0,0 +1,41 @@ +/************************************************************************ + * lib/math/lib_cosf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float cosf(float x) +{ + return sinf(x + M_PI_2); +} diff --git a/nuttx/lib/math/lib_cosh.c b/nuttx/lib/math/lib_cosh.c index 106990311..de4f42d7a 100644 --- a/nuttx/lib/math/lib_cosh.c +++ b/nuttx/lib/math/lib_cosh.c @@ -1,5 +1,15 @@ -/* - * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> +/************************************************************************ + * lib/math/lib_cosh.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -12,22 +22,26 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float coshf(float x) { - x = expf(x); - return ((x + (1.0 / x)) / 2.0); -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double cosh(double x) { - x = exp(x); - return ((x + (1.0 / x)) / 2.0); -} +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ -long double coshl(long double x) { - x = expl(x); - return ((x + (1.0 / x)) / 2.0); +#if CONFIG_HAVE_DOUBLE +double cosh(double x) +{ + x = exp(x); + return ((x + (1.0 / x)) / 2.0); } +#endif diff --git a/nuttx/lib/math/lib_coshf.c b/nuttx/lib/math/lib_coshf.c new file mode 100644 index 000000000..d5e5ea14d --- /dev/null +++ b/nuttx/lib/math/lib_coshf.c @@ -0,0 +1,42 @@ +/************************************************************************ + * lib/math/lib_coshf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float coshf(float x) +{ + x = expf(x); + return ((x + (1.0 / x)) / 2.0); +} diff --git a/nuttx/lib/math/lib_coshl.c b/nuttx/lib/math/lib_coshl.c new file mode 100644 index 000000000..d43912745 --- /dev/null +++ b/nuttx/lib/math/lib_coshl.c @@ -0,0 +1,47 @@ +/************************************************************************ + * lib/math/lib_coshl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double coshl(long double x) +{ + x = expl(x); + return ((x + (1.0 / x)) / 2.0); +} +#endif diff --git a/nuttx/lib/math/lib_cosl.c b/nuttx/lib/math/lib_cosl.c new file mode 100644 index 000000000..5b7321812 --- /dev/null +++ b/nuttx/lib/math/lib_cosl.c @@ -0,0 +1,46 @@ +/************************************************************************ + * lib/math/lib_cosl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double cosl(long double x) +{ + return sinl(x + M_PI_2); +} +#endif diff --git a/nuttx/lib/math/lib_exp.c b/nuttx/lib/math/lib_exp.c index a70cb10e7..5b54dbaac 100644 --- a/nuttx/lib/math/lib_exp.c +++ b/nuttx/lib/math/lib_exp.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_exp.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,242 +22,104 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <apps/math.h> -#include <float.h> -#include <stdint.h> -#include <stdbool.h> -#include <unistd.h> - -#define M_E2 (M_E * M_E) -#define M_E4 (M_E2 * M_E2) -#define M_E8 (M_E4 * M_E4) -#define M_E16 (M_E8 * M_E8) -#define M_E32 (M_E16 * M_E16) -#define M_E64 (M_E32 * M_E32) -#define M_E128 (M_E64 * M_E64) -#define M_E256 (M_E128 * M_E128) -#define M_E512 (M_E256 * M_E256) -#define M_E1024 (M_E512 * M_E512) - -static double _expi_square_tbl[11] = { - M_E, // e^1 - M_E2, // e^2 - M_E4, // e^4 - M_E8, // e^8 - M_E16, // e^16 - M_E32, // e^32 - M_E64, // e^64 - M_E128, // e^128 - M_E256, // e^256 - M_E512, // e^512 - M_E1024, // e^1024 + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +#include "lib_internal.h" + +#if CONFIG_HAVE_DOUBLE + +/************************************************************************ + * Private Data + ************************************************************************/ + +static double _dbl_inv_fact[] = +{ + 1.0 / 1.0, // 1 / 0! + 1.0 / 1.0, // 1 / 1! + 1.0 / 2.0, // 1 / 2! + 1.0 / 6.0, // 1 / 3! + 1.0 / 24.0, // 1 / 4! + 1.0 / 120.0, // 1 / 5! + 1.0 / 720.0, // 1 / 6! + 1.0 / 5040.0, // 1 / 7! + 1.0 / 40320.0, // 1 / 8! + 1.0 / 362880.0, // 1 / 9! + 1.0 / 3628800.0, // 1 / 10! + 1.0 / 39916800.0, // 1 / 11! + 1.0 / 479001600.0, // 1 / 12! + 1.0 / 6227020800.0, // 1 / 13! + 1.0 / 87178291200.0, // 1 / 14! + 1.0 / 1307674368000.0, // 1 / 15! + 1.0 / 20922789888000.0, // 1 / 16! + 1.0 / 355687428096000.0, // 1 / 17! + 1.0 / 6402373705728000.0, // 1 / 18! }; -static double _expi(size_t n) { - size_t i; - double val; - - if (n > 1024) { - return INFINITY; - } - - val = 1.0; - - for (i = 0; n; i++) { - if (n & (1 << i)) { - n &= ~(1 << i); - val *= _expi_square_tbl[i]; - } - } - - return val; -} - -static float _flt_inv_fact[] = { - 1.0 / 1.0, // 1/0! - 1.0 / 1.0, // 1/1! - 1.0 / 2.0, // 1/2! - 1.0 / 6.0, // 1/3! - 1.0 / 24.0, // 1/4! - 1.0 / 120.0, // 1/5! - 1.0 / 720.0, // 1/6! - 1.0 / 5040.0, // 1/7! - 1.0 / 40320.0, // 1/8! - 1.0 / 362880.0, // 1/9! - 1.0 / 3628800.0, // 1/10! -}; - -float expf(float x) { - size_t int_part; - bool invert; - float value; - float x0; - size_t i; - - if (x == 0) { - return 1; - } - else if (x < 0) { - invert = true; - x = -x; - } - else { - invert = false; - } - - /* extract integer component */ - int_part = (size_t) x; - - /* set x to fractional component */ - x -= (float) int_part; - - /* perform Taylor series approximation with eleven terms */ - value = 0.0; - x0 = 1.0; - for (i = 0; i < 10; i++) { - value += x0 * _flt_inv_fact[i]; - x0 *= x; - } - - /* multiply by exp of the integer component */ - value *= _expi(int_part); - - if (invert) { - return (1.0 / value); - } - else { - return value; - } -} - -static double _dbl_inv_fact[] = { - 1.0 / 1.0, // 1 / 0! - 1.0 / 1.0, // 1 / 1! - 1.0 / 2.0, // 1 / 2! - 1.0 / 6.0, // 1 / 3! - 1.0 / 24.0, // 1 / 4! - 1.0 / 120.0, // 1 / 5! - 1.0 / 720.0, // 1 / 6! - 1.0 / 5040.0, // 1 / 7! - 1.0 / 40320.0, // 1 / 8! - 1.0 / 362880.0, // 1 / 9! - 1.0 / 3628800.0, // 1 / 10! - 1.0 / 39916800.0, // 1 / 11! - 1.0 / 479001600.0, // 1 / 12! - 1.0 / 6227020800.0, // 1 / 13! - 1.0 / 87178291200.0, // 1 / 14! - 1.0 / 1307674368000.0, // 1 / 15! - 1.0 / 20922789888000.0, // 1 / 16! - 1.0 / 355687428096000.0, // 1 / 17! - 1.0 / 6402373705728000.0, // 1 / 18! -}; - -double exp(double x) { - size_t int_part; - bool invert; - double value; - double x0; - size_t i; - - if (x == 0) { - return 1; - } - else if (x < 0) { - invert = true; - x = -x; - } - else { - invert = false; - } - - /* extract integer component */ - int_part = (size_t) x; - - /* set x to fractional component */ - x -= (double) int_part; - - /* perform Taylor series approximation with nineteen terms */ - value = 0.0; - x0 = 1.0; - for (i = 0; i < 19; i++) { - value += x0 * _dbl_inv_fact[i]; - x0 *= x; - } - - /* multiply by exp of the integer component */ - value *= _expi(int_part); - - if (invert) { - return (1.0 / value); - } - else { - return value; - } -} - -static long double _ldbl_inv_fact[] = { - 1.0 / 1.0, // 1 / 0! - 1.0 / 1.0, // 1 / 1! - 1.0 / 2.0, // 1 / 2! - 1.0 / 6.0, // 1 / 3! - 1.0 / 24.0, // 1 / 4! - 1.0 / 120.0, // 1 / 5! - 1.0 / 720.0, // 1 / 6! - 1.0 / 5040.0, // 1 / 7! - 1.0 / 40320.0, // 1 / 8! - 1.0 / 362880.0, // 1 / 9! - 1.0 / 3628800.0, // 1 / 10! - 1.0 / 39916800.0, // 1 / 11! - 1.0 / 479001600.0, // 1 / 12! - 1.0 / 6227020800.0, // 1 / 13! - 1.0 / 87178291200.0, // 1 / 14! - 1.0 / 1307674368000.0, // 1 / 15! - 1.0 / 20922789888000.0, // 1 / 16! - 1.0 / 355687428096000.0, // 1 / 17! - 1.0 / 6402373705728000.0, // 1 / 18! -}; - -long double expl(long double x) { - size_t int_part; - bool invert; - long double value; - long double x0; - size_t i; - - if (x == 0) { - return 1; - } - else if (x < 0) { - invert = true; - x = -x; - } - else { - invert = false; - } - - /* extract integer component */ - int_part = (size_t) x; - - /* set x to fractional component */ - x -= (long double) int_part; - - /* perform Taylor series approximation with nineteen terms */ - value = 0.0; - x0 = 1.0; - for (i = 0; i < 19; i++) { - value += x0 * _ldbl_inv_fact[i]; - x0 *= x; - } - - /* multiply by exp of the integer component */ - value *= _expi(int_part); - - if (invert) { - return (1.0 / value); - } - else { - return value; - } +/************************************************************************ + * Public Functions + ************************************************************************/ + +double exp(double x) +{ + size_t int_part; + bool invert; + double value; + double x0; + size_t i; + + if (x == 0) + { + return 1; + } + else if (x < 0) + { + invert = true; + x = -x; + } + else + { + invert = false; + } + + /* Extract integer component */ + + int_part = (size_t) x; + + /* Set x to fractional component */ + + x -= (double)int_part; + + /* Perform Taylor series approximation with nineteen terms */ + + value = 0.0; + x0 = 1.0; + for (i = 0; i < 19; i++) + { + value += x0 * _dbl_inv_fact[i]; + x0 *= x; + } + + /* Multiply by exp of the integer component */ + + value *= lib_expi(int_part); + + if (invert) + { + return (1.0 / value); + } + else + { + return value; + } } +#endif diff --git a/nuttx/lib/math/lib_expf.c b/nuttx/lib/math/lib_expf.c new file mode 100644 index 000000000..eac4641c6 --- /dev/null +++ b/nuttx/lib/math/lib_expf.c @@ -0,0 +1,112 @@ +/************************************************************************ + * lib/math/lib_expf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <sys/types.h> +#include <math.h> + +#include "lib_internal.h" + +/************************************************************************ + * Private Data + ************************************************************************/ + +static float _flt_inv_fact[] = +{ + 1.0 / 1.0, // 1/0! + 1.0 / 1.0, // 1/1! + 1.0 / 2.0, // 1/2! + 1.0 / 6.0, // 1/3! + 1.0 / 24.0, // 1/4! + 1.0 / 120.0, // 1/5! + 1.0 / 720.0, // 1/6! + 1.0 / 5040.0, // 1/7! + 1.0 / 40320.0, // 1/8! + 1.0 / 362880.0, // 1/9! + 1.0 / 3628800.0, // 1/10! +}; + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float expf(float x) +{ + size_t int_part; + bool invert; + float value; + float x0; + size_t i; + + if (x == 0) + { + return 1; + } + else if (x < 0) + { + invert = true; + x = -x; + } + else + { + invert = false; + } + + /* Extract integer component */ + + int_part = (size_t) x; + + /* set x to fractional component */ + + x -= (float)int_part; + + /* Perform Taylor series approximation with eleven terms */ + + value = 0.0; + x0 = 1.0; + for (i = 0; i < 10; i++) + { + value += x0 * _flt_inv_fact[i]; + x0 *= x; + } + + /* Multiply by exp of the integer component */ + + value *= lib_expi(int_part); + + if (invert) + { + return (1.0 / value); + } + else + { + return value; + } +} diff --git a/nuttx/lib/math/lib_expl.c b/nuttx/lib/math/lib_expl.c new file mode 100644 index 000000000..cd5e6a500 --- /dev/null +++ b/nuttx/lib/math/lib_expl.c @@ -0,0 +1,126 @@ +/************************************************************************ + * lib/math/lib_expl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <sys/types.h> +#include <math.h> + +#include "lib_internal.h" + +#if CONFIG_HAVE_LONG_DOUBLE + +/************************************************************************ + * Private Data + ************************************************************************/ + +static long double _ldbl_inv_fact[] = +{ + 1.0 / 1.0, // 1 / 0! + 1.0 / 1.0, // 1 / 1! + 1.0 / 2.0, // 1 / 2! + 1.0 / 6.0, // 1 / 3! + 1.0 / 24.0, // 1 / 4! + 1.0 / 120.0, // 1 / 5! + 1.0 / 720.0, // 1 / 6! + 1.0 / 5040.0, // 1 / 7! + 1.0 / 40320.0, // 1 / 8! + 1.0 / 362880.0, // 1 / 9! + 1.0 / 3628800.0, // 1 / 10! + 1.0 / 39916800.0, // 1 / 11! + 1.0 / 479001600.0, // 1 / 12! + 1.0 / 6227020800.0, // 1 / 13! + 1.0 / 87178291200.0, // 1 / 14! + 1.0 / 1307674368000.0, // 1 / 15! + 1.0 / 20922789888000.0, // 1 / 16! + 1.0 / 355687428096000.0, // 1 / 17! + 1.0 / 6402373705728000.0, // 1 / 18! +}; + +/************************************************************************ + * Public Functions + ************************************************************************/ + +long double expl(long double x) +{ + size_t int_part; + bool invert; + long double value; + long double x0; + size_t i; + + if (x == 0) + { + return 1; + } + else if (x < 0) + { + invert = true; + x = -x; + } + else + { + invert = false; + } + + /* Extract integer component */ + + int_part = (size_t) x; + + /* Set x to fractional component */ + + x -= (long double)int_part; + + /* Perform Taylor series approximation with nineteen terms */ + + value = 0.0; + x0 = 1.0; + for (i = 0; i < 19; i++) + { + value += x0 * _ldbl_inv_fact[i]; + x0 *= x; + } + + /* Multiply by exp of the integer component */ + + value *= lib_expi(int_part); + + if (invert) + { + return (1.0 / value); + } + else + { + return value; + } +} +#endif diff --git a/nuttx/lib/math/lib_fabs.c b/nuttx/lib/math/lib_fabs.c index f479aa123..a635b0b41 100644 --- a/nuttx/lib/math/lib_fabs.c +++ b/nuttx/lib/math/lib_fabs.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_fabs.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,19 +22,25 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float fabsf(float x) { - return ((x < 0) ? -x : x); -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double fabs(double x) { - return ((x < 0) ? -x : x); -} +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ -long double fabsl(long double x) { - return ((x < 0) ? -x : x); +#if CONFIG_HAVE_DOUBLE +double fabs(double x) +{ + return ((x < 0) ? -x : x); } +#endif diff --git a/nuttx/lib/math/lib_fabsf.c b/nuttx/lib/math/lib_fabsf.c new file mode 100644 index 000000000..0ea186ca0 --- /dev/null +++ b/nuttx/lib/math/lib_fabsf.c @@ -0,0 +1,41 @@ +/************************************************************************ + * lib/math/lib_fabsf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float fabsf(float x) +{ + return ((x < 0) ? -x : x); +} diff --git a/nuttx/lib/math/lib_fabsl.c b/nuttx/lib/math/lib_fabsl.c new file mode 100644 index 000000000..f1e37e3f1 --- /dev/null +++ b/nuttx/lib/math/lib_fabsl.c @@ -0,0 +1,46 @@ +/************************************************************************ + * lib/math/lib_fabsl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double fabsl(long double x) +{ + return ((x < 0) ? -x : x); +} +#endif diff --git a/nuttx/lib/math/lib_floor.c b/nuttx/lib/math/lib_floor.c index a6979ac8a..895b9a503 100644 --- a/nuttx/lib/math/lib_floor.c +++ b/nuttx/lib/math/lib_floor.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_floor.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,26 +22,31 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float floorf(float x) { - modff(x, &x); - if (x < 0.0) x -= 1.0; - return x; -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double floor(double x) { - modf(x, &x); - if (x < 0.0) x -= 1.0; - return x; -} +#include <math.h> -long double floorl(long double x) { - modfl(x, &x); - if (x < 0.0) x -= 1.0; - return x; -} +/************************************************************************ + * Public Functions + ************************************************************************/ +#if CONFIG_HAVE_DOUBLE +double floor(double x) +{ + modf(x, &x); + if (x < 0.0) + { + x -= 1.0; + } + + return x; +} +#endif diff --git a/nuttx/lib/math/lib_floorf.c b/nuttx/lib/math/lib_floorf.c new file mode 100644 index 000000000..2becb5fac --- /dev/null +++ b/nuttx/lib/math/lib_floorf.c @@ -0,0 +1,47 @@ +/************************************************************************ + * lib/math/lib_floorf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float floorf(float x) +{ + modff(x, &x); + if (x < 0.0) + { + x -= 1.0; + } + + return x; +} diff --git a/nuttx/lib/math/lib_floorl.c b/nuttx/lib/math/lib_floorl.c new file mode 100644 index 000000000..2c7011297 --- /dev/null +++ b/nuttx/lib/math/lib_floorl.c @@ -0,0 +1,52 @@ +/************************************************************************ + * lib/math/lib_floorl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double floorl(long double x) +{ + modfl(x, &x); + if (x < 0.0) + { + x -= 1.0; + } + + return x; +} +#endif diff --git a/nuttx/lib/math/lib_fmod.c b/nuttx/lib/math/lib_fmod.c index 532fa3360..c55151d75 100644 --- a/nuttx/lib/math/lib_fmod.c +++ b/nuttx/lib/math/lib_fmod.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_fmod.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,56 +22,31 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -/* If GCC/CLang builtins are available, use them */ -#ifdef __GNUC__ +#include <nuttx/config.h> +#include <nuttx/compiler.h> -float fmodf(float x, float div) { - return __builtin_fmodf(x, div); -} - -double fmod(double x, double div) { - return __builtin_fmod(x, div); -} - -long double fmodl(long double x, long double div) { - return __builtin_fmodl(x, div); -} - -#else - -float fmodf(float x, float div) { - float n0; +#include <math.h> - x /= div; - x = modff(x, &n0); - x *= div; +/************************************************************************ + * Public Functions + ************************************************************************/ - return x; -} - -double fmod(double x, double div) { - double n0; +#if CONFIG_HAVE_DOUBLE +double fmod(double x, double div) +{ + double n0; - x /= div; - x = modf(x, &n0); - x *= div; + x /= div; + x = modf(x, &n0); + x *= div; - return x; + return x; } - -long double fmodl(long double x, long double div) { - long double n0; - - x /= div; - x = modfl(x, &n0); - x *= div; - - return x; -} - #endif diff --git a/nuttx/lib/math/lib_fmodf.c b/nuttx/lib/math/lib_fmodf.c new file mode 100644 index 000000000..085786f17 --- /dev/null +++ b/nuttx/lib/math/lib_fmodf.c @@ -0,0 +1,47 @@ +/************************************************************************ + * lib/math/lib_fmodf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float fmodf(float x, float div) +{ + float n0; + + x /= div; + x = modff(x, &n0); + x *= div; + + return x; +} diff --git a/nuttx/lib/math/lib_fmodl.c b/nuttx/lib/math/lib_fmodl.c new file mode 100644 index 000000000..c9b3ca784 --- /dev/null +++ b/nuttx/lib/math/lib_fmodl.c @@ -0,0 +1,52 @@ +/************************************************************************ + * lib/math/lib_fmodl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double fmodl(long double x, long double div) +{ + long double n0; + + x /= div; + x = modfl(x, &n0); + x *= div; + + return x; +} +#endif diff --git a/nuttx/lib/math/lib_frexp.c b/nuttx/lib/math/lib_frexp.c index 102609dbe..6de67f8b5 100644 --- a/nuttx/lib/math/lib_frexp.c +++ b/nuttx/lib/math/lib_frexp.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_frexp.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,23 +22,26 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float frexpf(float x, int *exp) { - *exp = (int) ceilf(log2f(x)); - return x / ldexpf(1.0, *exp); -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double frexp(double x, int *exp) { - *exp = (int) ceil(log2(x)); - return x / ldexp(1.0, *exp); -} +#include <math.h> -long double frexpl(long double x, int *exp) { - *exp = (int) ceill(log2(x)); - return x / ldexpl(1.0, *exp); -} +/************************************************************************ + * Public Functions + ************************************************************************/ +#if CONFIG_HAVE_DOUBLE +double frexp(double x, int *exponent) +{ + *exponent = (int)ceil(log2(x)); + return x / ldexp(1.0, *exponent); +} +#endif diff --git a/nuttx/lib/math/lib_frexpf.c b/nuttx/lib/math/lib_frexpf.c new file mode 100644 index 000000000..1fb0df3d8 --- /dev/null +++ b/nuttx/lib/math/lib_frexpf.c @@ -0,0 +1,42 @@ +/************************************************************************ + * lib/math/lib_frexpf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float frexpf(float x, int *exponent) +{ + *exponent = (int)ceilf(log2f(x)); + return x / ldexpf(1.0, *exponent); +} diff --git a/nuttx/lib/math/lib_frexpl.c b/nuttx/lib/math/lib_frexpl.c new file mode 100644 index 000000000..7a0d3c408 --- /dev/null +++ b/nuttx/lib/math/lib_frexpl.c @@ -0,0 +1,47 @@ +/************************************************************************ + * lib/math/lib_frexpl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double frexpl(long double x, int *exponent) +{ + *exponent = (int)ceill(log2(x)); + return x / ldexpl(1.0, *exponent); +} +#endif diff --git a/nuttx/lib/math/lib_ldexp.c b/nuttx/lib/math/lib_ldexp.c index 2ca79221a..ca428dc07 100644 --- a/nuttx/lib/math/lib_ldexp.c +++ b/nuttx/lib/math/lib_ldexp.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_ldexp.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,20 +22,25 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float ldexpf(float x, int n) { - return (x * powf(2.0, (float) n)); -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double ldexp(double x, int n) { - return (x * pow(2.0, (double) n)); -} +#include <math.h> -long double ldexpl(long double x, int n) { - return (x * powl(2.0, (long double) n)); -} +/************************************************************************ + * Public Functions + ************************************************************************/ +#if CONFIG_HAVE_DOUBLE +double ldexp(double x, int n) +{ + return (x * pow(2.0, (double)n)); +} +#endif diff --git a/nuttx/lib/math/lib_ldexpf.c b/nuttx/lib/math/lib_ldexpf.c new file mode 100644 index 000000000..c61d633d5 --- /dev/null +++ b/nuttx/lib/math/lib_ldexpf.c @@ -0,0 +1,41 @@ +/************************************************************************ + * lib/math/lib_ldexpf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float ldexpf(float x, int n) +{ + return (x * powf(2.0, (float)n)); +} diff --git a/nuttx/lib/math/lib_ldexpl.c b/nuttx/lib/math/lib_ldexpl.c new file mode 100644 index 000000000..0f77bff11 --- /dev/null +++ b/nuttx/lib/math/lib_ldexpl.c @@ -0,0 +1,46 @@ +/************************************************************************ + * lib/math/lib_ldexpl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double ldexpl(long double x, int n) +{ + return (x * powl(2.0, (long double)n)); +} +#endif diff --git a/nuttx/lib/math/lib_libexpi.c b/nuttx/lib/math/lib_libexpi.c new file mode 100644 index 000000000..d2bb971a2 --- /dev/null +++ b/nuttx/lib/math/lib_libexpi.c @@ -0,0 +1,103 @@ +/************************************************************************ + * lib/math/lib_libexpi.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <sys/types.h> +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +/************************************************************************ + * Pre-processor Definitions + ************************************************************************/ + +#define M_E2 (M_E * M_E) +#define M_E4 (M_E2 * M_E2) +#define M_E8 (M_E4 * M_E4) +#define M_E16 (M_E8 * M_E8) +#define M_E32 (M_E16 * M_E16) +#define M_E64 (M_E32 * M_E32) +#define M_E128 (M_E64 * M_E64) +#define M_E256 (M_E128 * M_E128) +#define M_E512 (M_E256 * M_E256) +#define M_E1024 (M_E512 * M_E512) + +/************************************************************************ + * Private Data + ************************************************************************/ + +static double _expi_square_tbl[11] = +{ + M_E, // e^1 + M_E2, // e^2 + M_E4, // e^4 + M_E8, // e^8 + M_E16, // e^16 + M_E32, // e^32 + M_E64, // e^64 + M_E128, // e^128 + M_E256, // e^256 + M_E512, // e^512 + M_E1024, // e^1024 +}; + +/************************************************************************ + * Public Functions + ************************************************************************/ + +static double lib_expi(size_t n) +{ + size_t i; + double val; + + if (n > 1024) + { + return INFINITY; + } + + val = 1.0; + + for (i = 0; n; i++) + { + if (n & (1 << i)) + { + n &= ~(1 << i); + val *= _expi_square_tbl[i]; + } + } + + return val; +} + diff --git a/nuttx/lib/math/lib_libsqrtapprox.c b/nuttx/lib/math/lib_libsqrtapprox.c new file mode 100644 index 000000000..5c556c3a0 --- /dev/null +++ b/nuttx/lib/math/lib_libsqrtapprox.c @@ -0,0 +1,50 @@ +/************************************************************************ + * lib/math/lib_libsqrtapprox.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <stdint.h> +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float lib_sqrtapprox(float x) +{ + int32_t i; + + /* Floats + bit manipulation = +inf fun! */ + + i = *((int32_t *) & x); + i = 0x1fc00000 + (i >> 1); + x = *((float *)&i); + + return x; +} diff --git a/nuttx/lib/math/lib_log.c b/nuttx/lib/math/lib_log.c index 62af0368a..4dd51e87c 100644 --- a/nuttx/lib/math/lib_log.c +++ b/nuttx/lib/math/lib_log.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_log.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,102 +22,61 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> - -float logf(float x) { - float y, y_old, ey, epsilon; - - y = 0.0; - y_old = 1.0; - epsilon = FLT_EPSILON; - - while (y > y_old + epsilon || y < y_old - epsilon) { - y_old = y; - ey = exp(y); - y -= (ey - x) / ey; - - if (y > 700.0) { - y = 700.0; - } - if (y < -700.0) { - y = -700.0; - } - - epsilon = (fabs(y) > 1.0) ? fabs(y) * FLT_EPSILON : FLT_EPSILON; - } - - if (y == 700.0) { - return INFINITY; - } - if (y == -700.0) { - return INFINITY; - } - - return y; -} - -double log(double x) { - double y, y_old, ey, epsilon; - - y = 0.0; - y_old = 1.0; - epsilon = DBL_EPSILON; +/************************************************************************ + * Included Files + ************************************************************************/ - while (y > y_old + epsilon || y < y_old - epsilon) { - y_old = y; - ey = exp(y); - y -= (ey - x) / ey; +#include <nuttx/config.h> +#include <nuttx/compiler.h> - if (y > 700.0) { - y = 700.0; - } - if (y < -700.0) { - y = -700.0; - } - - epsilon = (fabs(y) > 1.0) ? fabs(y) * DBL_EPSILON : DBL_EPSILON; - } - - if (y == 700.0) { - return INFINITY; - } - if (y == -700.0) { - return INFINITY; - } - - return y; -} - -long double logl(long double x) { - long double y, y_old, ey, epsilon; - - y = 0.0; - y_old = 1.0; - epsilon = 1e-6; //fixme - - while (y > y_old + epsilon || y < y_old - epsilon) { - y_old = y; - ey = expl(y); - y -= (ey - x) / ey; - - if (y > 700.0) { - y = 700.0; - } - if (y < -700.0) { - y = -700.0; - } - } - - if (y == 700.0) { - return INFINITY; - } - if (y == -700.0) { - return INFINITY; - } +#include <math.h> +#include <float.h> - return y; +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_DOUBLE +double log(double x) +{ + double y, y_old, ey, epsilon; + + y = 0.0; + y_old = 1.0; + epsilon = DBL_EPSILON; + + while (y > y_old + epsilon || y < y_old - epsilon) + { + y_old = y; + ey = exp(y); + y -= (ey - x) / ey; + + if (y > 700.0) + { + y = 700.0; + } + + if (y < -700.0) + { + y = -700.0; + } + + epsilon = (fabs(y) > 1.0) ? fabs(y) * DBL_EPSILON : DBL_EPSILON; + } + + if (y == 700.0) + { + return INFINITY; + } + + if (y == -700.0) + { + return INFINITY; + } + + return y; } - +#endif diff --git a/nuttx/lib/math/lib_log10.c b/nuttx/lib/math/lib_log10.c index 855480ad6..a6534407e 100644 --- a/nuttx/lib/math/lib_log10.c +++ b/nuttx/lib/math/lib_log10.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_log10.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,19 +22,25 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float log10f(float x) { - return (logf(x) / M_LN10); -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double log10(double x) { - return (log(x) / M_LN10); -} +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ -long double log10l(long double x) { - return (logl(x) / M_LN10); +#if CONFIG_HAVE_DOUBLE +double log10(double x) +{ + return (log(x) / M_LN10); } +#endif diff --git a/nuttx/lib/math/lib_log10f.c b/nuttx/lib/math/lib_log10f.c new file mode 100644 index 000000000..651071920 --- /dev/null +++ b/nuttx/lib/math/lib_log10f.c @@ -0,0 +1,41 @@ +/************************************************************************ + * lib/math/lib_log10f.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float log10f(float x) +{ + return (logf(x) / M_LN10); +} diff --git a/nuttx/lib/math/lib_log10l.c b/nuttx/lib/math/lib_log10l.c new file mode 100644 index 000000000..8702ed5a5 --- /dev/null +++ b/nuttx/lib/math/lib_log10l.c @@ -0,0 +1,46 @@ +/************************************************************************ + * lib/math/lib_log10l.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double log10l(long double x) +{ + return (logl(x) / M_LN10); +} +#endif diff --git a/nuttx/lib/math/lib_log2.c b/nuttx/lib/math/lib_log2.c index f8eb541b2..377d78e29 100644 --- a/nuttx/lib/math/lib_log2.c +++ b/nuttx/lib/math/lib_log2.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_log2.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,19 +22,25 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float log2f(float x) { - return (logf(x) / M_LN2); -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double log2(double x) { - return (log(x) / M_LN2); -} +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ -long double log2l(long double x) { - return (logl(x) / M_LN2); +#if CONFIG_HAVE_DOUBLE +double log2(double x) +{ + return (log(x) / M_LN2); } +#endif diff --git a/nuttx/lib/math/lib_log2f.c b/nuttx/lib/math/lib_log2f.c new file mode 100644 index 000000000..e160ca59e --- /dev/null +++ b/nuttx/lib/math/lib_log2f.c @@ -0,0 +1,41 @@ +/************************************************************************ + * lib/math/lib_log2f.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float log2f(float x) +{ + return (logf(x) / M_LN2); +} diff --git a/nuttx/lib/math/lib_log2l.c b/nuttx/lib/math/lib_log2l.c new file mode 100644 index 000000000..cd25c32f1 --- /dev/null +++ b/nuttx/lib/math/lib_log2l.c @@ -0,0 +1,46 @@ +/************************************************************************ + * lib/math/lib_log2l.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double log2l(long double x) +{ + return (logl(x) / M_LN2); +} +#endif diff --git a/nuttx/lib/math/lib_logf.c b/nuttx/lib/math/lib_logf.c new file mode 100644 index 000000000..1d31aa0c0 --- /dev/null +++ b/nuttx/lib/math/lib_logf.c @@ -0,0 +1,77 @@ +/************************************************************************ + * lib/math/lib_logf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> +#include <float.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float logf(float x) +{ + float y, y_old, ey, epsilon; + + y = 0.0; + y_old = 1.0; + epsilon = FLT_EPSILON; + + while (y > y_old + epsilon || y < y_old - epsilon) + { + y_old = y; + ey = exp(y); + y -= (ey - x) / ey; + + if (y > 700.0) + { + y = 700.0; + } + + if (y < -700.0) + { + y = -700.0; + } + + epsilon = (fabs(y) > 1.0) ? fabs(y) * FLT_EPSILON : FLT_EPSILON; + } + + if (y == 700.0) + { + return INFINITY; + } + + if (y == -700.0) + { + return INFINITY; + } + + return y; +} diff --git a/nuttx/lib/math/lib_logl.c b/nuttx/lib/math/lib_logl.c new file mode 100644 index 000000000..df0031943 --- /dev/null +++ b/nuttx/lib/math/lib_logl.c @@ -0,0 +1,80 @@ +/************************************************************************ + * lib/math/lib_lol.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> +#include <float.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double logl(long double x) +{ + long double y, y_old, ey, epsilon; + + y = 0.0; + y_old = 1.0; + epsilon = LDBL_EPSILON; + + while (y > y_old + epsilon || y < y_old - epsilon) + { + y_old = y; + ey = expl(y); + y -= (ey - x) / ey; + + if (y > 700.0) + { + y = 700.0; + } + + if (y < -700.0) + { + y = -700.0; + } + } + + if (y == 700.0) + { + return INFINITY; + } + + if (y == -700.0) + { + return INFINITY; + } + + return y; +} +#endif diff --git a/nuttx/lib/math/lib_modf.c b/nuttx/lib/math/lib_modf.c index 1e6fadc11..3b33cde42 100644 --- a/nuttx/lib/math/lib_modf.c +++ b/nuttx/lib/math/lib_modf.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_modf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,54 +22,37 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ -#include <apps/math.h> -#include <float.h> #include <stdint.h> +#include <math.h> -float modff(float x, float *iptr) { - if (fabsf(x) >= 8388608.0) { - *iptr = x; - return 0.0; - } - else if (fabs(x) < 1.0) { - *iptr = 0.0; - return x; - } - else { - *iptr = (float) (int) x; - return (x - *iptr); - } -} +/************************************************************************ + * Public Functions + ************************************************************************/ -double modf(double x, double *iptr) { - if (fabs(x) >= 4503599627370496.0) { - *iptr = x; - return 0.0; - } - else if (fabs(x) < 1.0) { - *iptr = 0.0; - return x; - } - else { - *iptr = (double) (int64_t) x; - return (x - *iptr); - } +#if CONFIG_HAVE_DOUBLE +double modf(double x, double *iptr) +{ + if (fabs(x) >= 4503599627370496.0) + { + *iptr = x; + return 0.0; + } + else if (fabs(x) < 1.0) + { + *iptr = 0.0; + return x; + } + else + { + *iptr = (double)(int64_t) x; + return (x - *iptr); + } } - -long double modfl(long double x, long double *iptr) { - if (fabs(x) >= 4503599627370496.0) { - *iptr = x; - return 0.0; - } - else if (fabs(x) < 1.0) { - *iptr = 0.0; - return x; - } - else { - *iptr = (long double) (int64_t) x; - return (x - *iptr); - } -} - +#endif diff --git a/nuttx/lib/math/lib_modff.c b/nuttx/lib/math/lib_modff.c new file mode 100644 index 000000000..4eec2ae17 --- /dev/null +++ b/nuttx/lib/math/lib_modff.c @@ -0,0 +1,55 @@ +/************************************************************************ + * lib/math/lib_modff.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float modff(float x, float *iptr) +{ + if (fabsf(x) >= 8388608.0) + { + *iptr = x; + return 0.0; + } + else if (fabs(x) < 1.0) + { + *iptr = 0.0; + return x; + } + else + { + *iptr = (float)(int)x; + return (x - *iptr); + } +} diff --git a/nuttx/lib/math/lib_modfl.c b/nuttx/lib/math/lib_modfl.c new file mode 100644 index 000000000..55b17eae6 --- /dev/null +++ b/nuttx/lib/math/lib_modfl.c @@ -0,0 +1,61 @@ +/************************************************************************ + * lib/math/lib_modfl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <stdint.h> +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double modfl(long double x, long double *iptr) +{ + if (fabs(x) >= 4503599627370496.0) + { + *iptr = x; + return 0.0; + } + else if (fabs(x) < 1.0) + { + *iptr = 0.0; + return x; + } + else + { + *iptr = (long double)(int64_t) x; + return (x - *iptr); + } +} +#endif diff --git a/nuttx/lib/math/lib_pow.c b/nuttx/lib/math/lib_pow.c index b353359eb..66f6a2d2e 100644 --- a/nuttx/lib/math/lib_pow.c +++ b/nuttx/lib/math/lib_pow.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_pow.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,19 +22,25 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float powf(float b, float e) { - return expf(e * logf(b)); -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double pow(double b, double e) { - return exp(e * log(b)); -} +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ -long double powl(long double b, long double e) { - return expl(e * logl(b)); +#if CONFIG_HAVE_DOUBLE +double pow(double b, double e) +{ + return exp(e * log(b)); } +#endif diff --git a/nuttx/lib/math/lib_powf.c b/nuttx/lib/math/lib_powf.c new file mode 100644 index 000000000..a43f9cf82 --- /dev/null +++ b/nuttx/lib/math/lib_powf.c @@ -0,0 +1,41 @@ +/************************************************************************ + * lib/math/lib_powf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float powf(float b, float e) +{ + return expf(e * logf(b)); +} diff --git a/nuttx/lib/math/lib_powl.c b/nuttx/lib/math/lib_powl.c new file mode 100644 index 000000000..6a820df26 --- /dev/null +++ b/nuttx/lib/math/lib_powl.c @@ -0,0 +1,46 @@ +/************************************************************************ + * lib/math/lib_powl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double powl(long double b, long double e) +{ + return expl(e * logl(b)); +} +#endif diff --git a/nuttx/lib/math/lib_sin.c b/nuttx/lib/math/lib_sin.c index e862104fd..2d21d2309 100644 --- a/nuttx/lib/math/lib_sin.c +++ b/nuttx/lib/math/lib_sin.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_sin.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,141 +22,93 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <stdint.h> -#include <unistd.h> -#include <float.h> -#include <apps/math.h> - -static float _flt_inv_fact[] = { - 1.0 / 1.0, // 1 / 1! - 1.0 / 6.0, // 1 / 3! - 1.0 / 120.0, // 1 / 5! - 1.0 / 5040.0, // 1 / 7! - 1.0 / 362880.0, // 1 / 9! - 1.0 / 39916800.0, // 1 / 11! + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <sys/types.h> +#include <math.h> + +#if CONFIG_HAVE_DOUBLE + +/************************************************************************ + * Private Data + ************************************************************************/ + +static double _dbl_inv_fact[] = +{ + 1.0 / 1.0, // 1 / 1! + 1.0 / 6.0, // 1 / 3! + 1.0 / 120.0, // 1 / 5! + 1.0 / 5040.0, // 1 / 7! + 1.0 / 362880.0, // 1 / 9! + 1.0 / 39916800.0, // 1 / 11! + 1.0 / 6227020800.0, // 1 / 13! + 1.0 / 1307674368000.0, // 1 / 15! + 1.0 / 355687428096000.0, // 1 / 17! + 1.0 / 121645100408832000.0, // 1 / 19! }; -float sinf(float x) { - float x_squared; - float sin_x; - size_t i; - - /* move x to [-pi, pi) */ - x = fmodf(x, 2 * M_PI); - if (x >= M_PI) x -= 2 * M_PI; - if (x < -M_PI) x += 2 * M_PI; - - /* move x to [-pi/2, pi/2) */ - if (x >= M_PI_2) x = M_PI - x; - if (x < -M_PI_2) x = -M_PI - x; - - x_squared = x * x; - sin_x = 0.0; - - /* perform Taylor series approximation for sin(x) with six terms */ - for (i = 0; i < 6; i++) { - if (i % 2 == 0) { - sin_x += x * _flt_inv_fact[i]; - } - else { - sin_x -= x * _flt_inv_fact[i]; - } - - x *= x_squared; - } - - return sin_x; +/************************************************************************ + * Public Functions + ************************************************************************/ + +double sin(double x) +{ + double x_squared; + double sin_x; + size_t i; + + /* Move x to [-pi, pi) */ + + x = fmod(x, 2 * M_PI); + if (x >= M_PI) + { + x -= 2 * M_PI; + } + + if (x < -M_PI) + { + x += 2 * M_PI; + } + + /* Move x to [-pi/2, pi/2) */ + + if (x >= M_PI_2) + { + x = M_PI - x; + } + + if (x < -M_PI_2) + { + x = -M_PI - x; + } + + x_squared = x * x; + sin_x = 0.0; + + /* Perform Taylor series approximation for sin(x) with ten terms */ + + for (i = 0; i < 10; i++) + { + if (i % 2 == 0) + { + sin_x += x * _dbl_inv_fact[i]; + } + else + { + sin_x -= x * _dbl_inv_fact[i]; + } + + x *= x_squared; + } + + return sin_x; } - -static double _dbl_inv_fact[] = { - 1.0 / 1.0, // 1 / 1! - 1.0 / 6.0, // 1 / 3! - 1.0 / 120.0, // 1 / 5! - 1.0 / 5040.0, // 1 / 7! - 1.0 / 362880.0, // 1 / 9! - 1.0 / 39916800.0, // 1 / 11! - 1.0 / 6227020800.0, // 1 / 13! - 1.0 / 1307674368000.0, // 1 / 15! - 1.0 / 355687428096000.0, // 1 / 17! - 1.0 / 121645100408832000.0, // 1 / 19! -}; - -double sin(double x) { - double x_squared; - double sin_x; - size_t i; - - /* move x to [-pi, pi) */ - x = fmod(x, 2 * M_PI); - if (x >= M_PI) x -= 2 * M_PI; - if (x < -M_PI) x += 2 * M_PI; - - /* move x to [-pi/2, pi/2) */ - if (x >= M_PI_2) x = M_PI - x; - if (x < -M_PI_2) x = -M_PI - x; - - x_squared = x * x; - sin_x = 0.0; - - /* perform Taylor series approximation for sin(x) with ten terms */ - for (i = 0; i < 10; i++) { - if (i % 2 == 0) { - sin_x += x * _dbl_inv_fact[i]; - } - else { - sin_x -= x * _dbl_inv_fact[i]; - } - - x *= x_squared; - } - - return sin_x; -} - -static long double _ldbl_inv_fact[] = { - 1.0 / 1.0, // 1 / 1! - 1.0 / 6.0, // 1 / 3! - 1.0 / 120.0, // 1 / 5! - 1.0 / 5040.0, // 1 / 7! - 1.0 / 362880.0, // 1 / 9! - 1.0 / 39916800.0, // 1 / 11! - 1.0 / 6227020800.0, // 1 / 13! - 1.0 / 1307674368000.0, // 1 / 15! - 1.0 / 355687428096000.0, // 1 / 17! - 1.0 / 121645100408832000.0, // 1 / 19! -}; - -long double sinl(long double x) { - long double x_squared; - long double sin_x; - size_t i; - - /* move x to [-pi, pi) */ - x = fmodl(x, 2 * M_PI); - if (x >= M_PI) x -= 2 * M_PI; - if (x < -M_PI) x += 2 * M_PI; - - /* move x to [-pi/2, pi/2) */ - if (x >= M_PI_2) x = M_PI - x; - if (x < -M_PI_2) x = -M_PI - x; - - x_squared = x * x; - sin_x = 0.0; - - /* perform Taylor series approximation for sin(x) with ten terms */ - for (i = 0; i < 10; i++) { - if (i % 2 == 0) { - sin_x += x * _ldbl_inv_fact[i]; - } - else { - sin_x -= x * _ldbl_inv_fact[i]; - } - - x *= x_squared; - } - - return sin_x; -} - +#endif diff --git a/nuttx/lib/math/lib_sinf.c b/nuttx/lib/math/lib_sinf.c new file mode 100644 index 000000000..e298bbba4 --- /dev/null +++ b/nuttx/lib/math/lib_sinf.c @@ -0,0 +1,104 @@ +/************************************************************************ + * lib/math/lib_sinf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <sys/types.h> +#include <math.h> + +/************************************************************************ + * Private Data + ************************************************************************/ + +static float _flt_inv_fact[] = +{ + 1.0 / 1.0, // 1 / 1! + 1.0 / 6.0, // 1 / 3! + 1.0 / 120.0, // 1 / 5! + 1.0 / 5040.0, // 1 / 7! + 1.0 / 362880.0, // 1 / 9! + 1.0 / 39916800.0, // 1 / 11! +}; + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float sinf(float x) +{ + float x_squared; + float sin_x; + size_t i; + + /* Move x to [-pi, pi) */ + + x = fmodf(x, 2 * M_PI); + if (x >= M_PI) + { + x -= 2 * M_PI; + } + + if (x < -M_PI) + { + x += 2 * M_PI; + } + + /* Move x to [-pi/2, pi/2) */ + + if (x >= M_PI_2) + { + x = M_PI - x; + } + + if (x < -M_PI_2) + { + x = -M_PI - x; + } + + x_squared = x * x; + sin_x = 0.0; + + /* Perform Taylor series approximation for sin(x) with six terms */ + + for (i = 0; i < 6; i++) + { + if (i % 2 == 0) + { + sin_x += x * _flt_inv_fact[i]; + } + else + { + sin_x -= x * _flt_inv_fact[i]; + } + + x *= x_squared; + } + + return sin_x; +} diff --git a/nuttx/lib/math/lib_sinh.c b/nuttx/lib/math/lib_sinh.c index 10276d821..a22652302 100644 --- a/nuttx/lib/math/lib_sinh.c +++ b/nuttx/lib/math/lib_sinh.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_sinh.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,22 +22,26 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float sinhf(float x) { - x = expf(x); - return ((x - (1.0 / x)) / 2.0); -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double sinh(double x) { - x = exp(x); - return ((x - (1.0 / x)) / 2.0); -} +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ -long double sinhl(long double x) { - x = expl(x); - return ((x - (1.0 / x)) / 2.0); +#if CONFIG_HAVE_DOUBLE +double sinh(double x) +{ + x = exp(x); + return ((x - (1.0 / x)) / 2.0); } +#endif diff --git a/nuttx/lib/math/lib_sinhf.c b/nuttx/lib/math/lib_sinhf.c new file mode 100644 index 000000000..e15cb14dc --- /dev/null +++ b/nuttx/lib/math/lib_sinhf.c @@ -0,0 +1,42 @@ +/************************************************************************ + * lib/math/lib_sinhf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float sinhf(float x) +{ + x = expf(x); + return ((x - (1.0 / x)) / 2.0); +} diff --git a/nuttx/lib/math/lib_sinhl.c b/nuttx/lib/math/lib_sinhl.c new file mode 100644 index 000000000..442586ffb --- /dev/null +++ b/nuttx/lib/math/lib_sinhl.c @@ -0,0 +1,47 @@ +/************************************************************************ + * lib/math/lib_sinhl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double sinhl(long double x) +{ + x = expl(x); + return ((x - (1.0 / x)) / 2.0); +} +#endif diff --git a/nuttx/lib/math/lib_sinl.c b/nuttx/lib/math/lib_sinl.c new file mode 100644 index 000000000..f8d9e00ed --- /dev/null +++ b/nuttx/lib/math/lib_sinl.c @@ -0,0 +1,114 @@ +/************************************************************************ + * lib/math/lib_sinl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <sys/types.h> +#include <math.h> + +#if CONFIG_HAVE_LONG_DOUBLE + +/************************************************************************ + * Private Data + ************************************************************************/ + +static long double _ldbl_inv_fact[] = +{ + 1.0 / 1.0, // 1 / 1! + 1.0 / 6.0, // 1 / 3! + 1.0 / 120.0, // 1 / 5! + 1.0 / 5040.0, // 1 / 7! + 1.0 / 362880.0, // 1 / 9! + 1.0 / 39916800.0, // 1 / 11! + 1.0 / 6227020800.0, // 1 / 13! + 1.0 / 1307674368000.0, // 1 / 15! + 1.0 / 355687428096000.0, // 1 / 17! + 1.0 / 121645100408832000.0, // 1 / 19! +}; + +/************************************************************************ + * Public Functions + ************************************************************************/ + +long double sinl(long double x) +{ + long double x_squared; + long double sin_x; + size_t i; + + /* Move x to [-pi, pi) */ + + x = fmodl(x, 2 * M_PI); + if (x >= M_PI) + { + x -= 2 * M_PI; + } + + if (x < -M_PI) + { + x += 2 * M_PI; + } + + /* Move x to [-pi/2, pi/2) */ + + if (x >= M_PI_2) + { + x = M_PI - x; + } + + if (x < -M_PI_2) + { + x = -M_PI - x; + } + + x_squared = x * x; + sin_x = 0.0; + + /* Perform Taylor series approximation for sin(x) with ten terms */ + + for (i = 0; i < 10; i++) + { + if (i % 2 == 0) + { + sin_x += x * _ldbl_inv_fact[i]; + } + else + { + sin_x -= x * _ldbl_inv_fact[i]; + } + + x *= x_squared; + } + + return sin_x; +} +#endif diff --git a/nuttx/lib/math/lib_sqrt.c b/nuttx/lib/math/lib_sqrt.c index 206a7fe82..96f0d5409 100644 --- a/nuttx/lib/math/lib_sqrt.c +++ b/nuttx/lib/math/lib_sqrt.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_sqrt.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,105 +22,78 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <stdint.h> -#include <float.h> -#include <errno.h> -#include <apps/math.h> - -static float __sqrt_approx(float x) { - int32_t i; - - // floats + bit manipulation = +inf fun! - i = *((int32_t*) &x); - i = 0x1FC00000 + (i >> 1); - x = *((float*) &i); - - return x; -} - -float sqrtf(float x) { - float y; +/************************************************************************ + * Included Files + ************************************************************************/ - // filter out invalid/trivial inputs - if (x < 0.0) { errno = EDOM; return NAN; } - if (isnan(x)) return NAN; - if (isinf(x)) return INFINITY; - if (x == 0.0) return 0.0; +#include <nuttx/config.h> +#include <nuttx/compiler.h> - // guess square root (using bit manipulation) - y = __sqrt_approx(x); - - // perform three iterations of approximation - // this number (3) is definitely optimal - y = 0.5 * (y + x / y); - y = 0.5 * (y + x / y); - y = 0.5 * (y + x / y); - - return y; -} - -double sqrt(double x) { - long double y, y1; - - // filter out invalid/trivial inputs - if (x < 0.0) { errno = EDOM; return NAN; } - if (isnan(x)) return NAN; - if (isinf(x)) return INFINITY; - if (x == 0.0) return 0.0; - - // guess square root (using bit manipulation) - y = __sqrt_approx(x); - - // perform four iterations of approximation - // this number (4) is definitely optimal - y = 0.5 * (y + x / y); - y = 0.5 * (y + x / y); - y = 0.5 * (y + x / y); - y = 0.5 * (y + x / y); - - // if guess was terribe (out of range of float) - // repeat approximation until convergence - if (y * y < x - 1.0 || y * y > x + 1.0) { - y1 = -1.0; - while (y != y1) { - y1 = y; - y = 0.5 * (y + x / y); - } - } - - return y; -} +#include <math.h> +#include <errno.h> -long double sqrtl(long double x) { - long double y, y1; - - // filter out invalid/trivial inputs - if (x < 0.0) { errno = EDOM; return NAN; } - if (isnan(x)) return NAN; - if (isinf(x)) return INFINITY; - if (x == 0.0) return 0.0; - - // guess square root (using bit manipulation) - y = __sqrt_approx(x); - - // perform four iterations of approximation - // this number (4) is definitely optimal - y = 0.5 * (y + x / y); - y = 0.5 * (y + x / y); - y = 0.5 * (y + x / y); - y = 0.5 * (y + x / y); - - // if guess was terribe (out of range of float) - // repeat approximation until convergence - if (y * y < x - 1.0 || y * y > x + 1.0) { - y1 = -1.0; - while (y != y1) { - y1 = y; - y = 0.5 * (y + x / y); - } - } - - return y; +#include "lib_internal.h" + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_DOUBLE +double sqrt(double x) +{ + long double y, y1; + + if (x < 0.0) + { + errno = EDOM; + return NAN; + } + + if (isnan(x)) + { + return NAN; + } + + if (isinf(x)) + { + return INFINITY; + } + + if (x == 0.0) + { + return 0.0; + } + + /* Guess square root (using bit manipulation) */ + + y = lib_sqrtapprox(x); + + /* Perform four iterations of approximation. This number (4) is + * definitely optimal + */ + + y = 0.5 * (y + x / y); + y = 0.5 * (y + x / y); + y = 0.5 * (y + x / y); + y = 0.5 * (y + x / y); + + /* If guess was terribe (out of range of float). Repeat approximation + * until convergence. + */ + + if (y * y < x - 1.0 || y * y > x + 1.0) + { + y1 = -1.0; + while (y != y1) + { + y1 = y; + y = 0.5 * (y + x / y); + } + } + + return y; } +#endif diff --git a/nuttx/lib/math/lib_sqrtf.c b/nuttx/lib/math/lib_sqrtf.c new file mode 100644 index 000000000..81817a040 --- /dev/null +++ b/nuttx/lib/math/lib_sqrtf.c @@ -0,0 +1,84 @@ +/************************************************************************ + * lib/math/lib_sqrtf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> +#include <errno.h> + +#include "lib_internal.h" + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float sqrtf(float x) +{ + float y; + + /* Filter out invalid/trivial inputs */ + + if (x < 0.0) + { + errno = EDOM; + return NAN; + } + + if (isnan(x)) + { + return NAN; + } + + if (isinf(x)) + { + return INFINITY; + } + + if (x == 0.0) + { + return 0.0; + } + + /* Guess square root (using bit manipulation) */ + + y = lib_sqrtapprox(x); + + /* Perform three iterations of approximation. This number (3) is + * definitely optimal + */ + + y = 0.5 * (y + x / y); + y = 0.5 * (y + x / y); + y = 0.5 * (y + x / y); + + return y; +} diff --git a/nuttx/lib/math/lib_sqrtl.c b/nuttx/lib/math/lib_sqrtl.c new file mode 100644 index 000000000..f305092e7 --- /dev/null +++ b/nuttx/lib/math/lib_sqrtl.c @@ -0,0 +1,101 @@ +/************************************************************************ + * lib/math/lib_sqrtl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> +#include <errno.h> + +#include "lib_internal.h" + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double sqrtl(long double x) +{ + long double y, y1; + + /* Filter out invalid/trivial inputs */ + + if (x < 0.0) + { + errno = EDOM; + return NAN; + } + + if (isnan(x)) + { + return NAN; + } + + if (isinf(x)) + { + return INFINITY; + } + + if (x == 0.0) + { + return 0.0; + } + + /* Guess square root (using bit manipulation) */ + + y = lib_sqrtapprox(x); + + /* Perform four iterations of approximation. This number (4) is + * definitely optimal + */ + + y = 0.5 * (y + x / y); + y = 0.5 * (y + x / y); + y = 0.5 * (y + x / y); + y = 0.5 * (y + x / y); + + /* If guess was terribe (out of range of float). Repeat approximation + * until convergence + */ + + if (y * y < x - 1.0 || y * y > x + 1.0) + { + y1 = -1.0; + while (y != y1) + { + y1 = y; + y = 0.5 * (y + x / y); + } + } + + return y; +} +#endif diff --git a/nuttx/lib/math/lib_tan.c b/nuttx/lib/math/lib_tan.c index c336a6260..a9a1d8ebe 100644 --- a/nuttx/lib/math/lib_tan.c +++ b/nuttx/lib/math/lib_tan.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_tan.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,20 +22,25 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <stdint.h> -#include <float.h> -#include <apps/math.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float tanf(float x) { - return (sinf(x) / cosf(x)); -} +#include <nuttx/config.h> +#include <nuttx/compiler.h> -double tan(double x) { - return (sin(x) / cos(x)); -} +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ -long double tanl(long double x) { - return (sinl(x) / cosl(x)); +#if CONFIG_HAVE_DOUBLE +double tan(double x) +{ + return (sin(x) / cos(x)); } +#endif diff --git a/nuttx/lib/math/lib_tanf.c b/nuttx/lib/math/lib_tanf.c new file mode 100644 index 000000000..3db3bda26 --- /dev/null +++ b/nuttx/lib/math/lib_tanf.c @@ -0,0 +1,41 @@ +/************************************************************************ + * lib/math/lib_tanf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float tanf(float x) +{ + return (sinf(x) / cosf(x)); +} diff --git a/nuttx/lib/math/lib_tanh.c b/nuttx/lib/math/lib_tanh.c index c6a1a1c87..5cc1d9176 100644 --- a/nuttx/lib/math/lib_tanh.c +++ b/nuttx/lib/math/lib_tanh.c @@ -1,4 +1,14 @@ -/* +/************************************************************************ + * lib/math/lib_tanh.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> * * Permission to use, copy, modify, and distribute this software for any @@ -12,28 +22,28 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * + ************************************************************************/ -#include <apps/math.h> -#include <float.h> +/************************************************************************ + * Included Files + ************************************************************************/ -float tanhf(float x) { - float x0 = expf(x); - float x1 = 1.0 / x0; +#include <nuttx/config.h> +#include <nuttx/compiler.h> - return ((x0 + x1) / (x0 - x1)); -} - -double tanh(double x) { - double x0 = exp(x); - double x1 = 1.0 / x0; +#include <math.h> - return ((x0 + x1) / (x0 - x1)); -} +/************************************************************************ + * Public Functions + ************************************************************************/ -long double tanhl(long double x) { - long double x0 = exp(x); - long double x1 = 1.0 / x0; +#if CONFIG_HAVE_DOUBLE +double tanh(double x) +{ + double x0 = exp(x); + double x1 = 1.0 / x0; - return ((x0 + x1) / (x0 - x1)); + return ((x0 + x1) / (x0 - x1)); } +#endif diff --git a/nuttx/lib/math/lib_tanhf.c b/nuttx/lib/math/lib_tanhf.c new file mode 100644 index 000000000..94d15cc60 --- /dev/null +++ b/nuttx/lib/math/lib_tanhf.c @@ -0,0 +1,44 @@ +/************************************************************************ + * lib/math/lib_tanhf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float tanhf(float x) +{ + float x0 = expf(x); + float x1 = 1.0 / x0; + + return ((x0 + x1) / (x0 - x1)); +} diff --git a/nuttx/lib/math/lib_tanhl.c b/nuttx/lib/math/lib_tanhl.c new file mode 100644 index 000000000..8b897451e --- /dev/null +++ b/nuttx/lib/math/lib_tanhl.c @@ -0,0 +1,49 @@ +/************************************************************************ + * lib/math/lib_tanhl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double tanhl(long double x) +{ + long double x0 = exp(x); + long double x1 = 1.0 / x0; + + return ((x0 + x1) / (x0 - x1)); +} +#endif diff --git a/nuttx/lib/math/lib_tanl.c b/nuttx/lib/math/lib_tanl.c new file mode 100644 index 000000000..c4c3712c1 --- /dev/null +++ b/nuttx/lib/math/lib_tanl.c @@ -0,0 +1,46 @@ +/************************************************************************ + * lib/math/lib_tanl.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Ported by: Darcy Gong + * + * It derives from the Rhombs OS math library by Nick Johnson which has + * a compatibile, MIT-style license: + * + * Copyright (C) 2009, 2010 Nick Johnson <nickbjohnson4224 at gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#if CONFIG_HAVE_LONG_DOUBLE +long double tanl(long double x) +{ + return (sinl(x) / cosl(x)); +} +#endif |