diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-12-07 16:00:56 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-12-07 16:00:56 +0000 |
commit | 756aa6b90920d0087ad708c3ad10e091be79e46e (patch) | |
tree | c29f3db20b8275cd22e7c4d36c3d722f2fdf6277 /nuttx/libc | |
parent | 4c98d2e73211a2bde3530c9f1f369efa0d0745e1 (diff) | |
download | px4-firmware-756aa6b90920d0087ad708c3ad10e091be79e46e.tar.gz px4-firmware-756aa6b90920d0087ad708c3ad10e091be79e46e.tar.bz2 px4-firmware-756aa6b90920d0087ad708c3ad10e091be79e46e.zip |
Patches from Petteri Aimonen + stdbool and rand() changes for Freddie Chopin
git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5415 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/libc')
-rw-r--r-- | nuttx/libc/math/Make.defs | 6 | ||||
-rw-r--r-- | nuttx/libc/math/lib_round.c | 40 | ||||
-rw-r--r-- | nuttx/libc/math/lib_roundf.c | 38 | ||||
-rw-r--r-- | nuttx/libc/math/lib_roundl.c | 40 | ||||
-rw-r--r-- | nuttx/libc/stdlib/lib_rand.c | 33 |
5 files changed, 140 insertions, 17 deletions
diff --git a/nuttx/libc/math/Make.defs b/nuttx/libc/math/Make.defs index bc6a265f0..ece25f4e5 100644 --- a/nuttx/libc/math/Make.defs +++ b/nuttx/libc/math/Make.defs @@ -40,17 +40,17 @@ ifeq ($(CONFIG_LIBM),y) 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_roundf.c 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 CSRCS += 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_round.c 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_roundl.c lib_sinl.c lib_sinhl.c lib_sqrtl.c lib_tanl.c lib_tanhl.c CSRCS += lib_libexpi.c lib_libsqrtapprox.c diff --git a/nuttx/libc/math/lib_round.c b/nuttx/libc/math/lib_round.c new file mode 100644 index 000000000..6191cee5b --- /dev/null +++ b/nuttx/libc/math/lib_round.c @@ -0,0 +1,40 @@ +/************************************************************************ + * lib/math/lib_round.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * (C) 2012 Petteri Aimonen <jpa@nx.mail.kapsi.fi> + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#ifdef CONFIG_HAVE_DOUBLE +double round(double x) +{ + double f = modf(x, &x); + if (x <= 0.0 && f <= -0.5) + { + x -= 1.0; + } + + if (x >= 0.0 && f >= 0.5) + { + x += 1.0; + } + + return x; +} +#endif diff --git a/nuttx/libc/math/lib_roundf.c b/nuttx/libc/math/lib_roundf.c new file mode 100644 index 000000000..145cf3df6 --- /dev/null +++ b/nuttx/libc/math/lib_roundf.c @@ -0,0 +1,38 @@ +/************************************************************************ + * lib/math/lib_roundf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * (C) 2012 Petteri Aimonen <jpa@nx.mail.kapsi.fi> + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float roundf(float x) +{ + float f = modff(x, &x); + if (x <= 0.0f && f <= -0.5f) + { + x -= 1.0f; + } + + if (x >= 0.0f && f >= 0.5f) + { + x += 1.0f; + } + + return x; +} diff --git a/nuttx/libc/math/lib_roundl.c b/nuttx/libc/math/lib_roundl.c new file mode 100644 index 000000000..b2ddba670 --- /dev/null +++ b/nuttx/libc/math/lib_roundl.c @@ -0,0 +1,40 @@ +/************************************************************************ + * lib/math/lib_round.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * (C) 2012 Petteri Aimonen <jpa@nx.mail.kapsi.fi> + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#ifdef CONFIG_HAVE_LONG_DOUBLE +long double roundl(long double x) +{ + long double f = modfl(x, &x); + if (x <= 0.0 && f <= -0.5) + { + x -= 1.0; + } + + if (x >= 0.0 && f >= 0.5) + { + x += 1.0; + } + + return x; +} +#endif diff --git a/nuttx/libc/stdlib/lib_rand.c b/nuttx/libc/stdlib/lib_rand.c index 0faef5d66..453a4537a 100644 --- a/nuttx/libc/stdlib/lib_rand.c +++ b/nuttx/libc/stdlib/lib_rand.c @@ -74,7 +74,7 @@ static unsigned int nrand(unsigned int nLimit); /* First order congruential generators */ -static inline void fgenerate1(void); +static inline unsigned long fgenerate1(void); #if (CONFIG_LIB_RAND_ORDER == 1) static double_t frand1(void); #endif @@ -82,7 +82,7 @@ static double_t frand1(void); /* Second order congruential generators */ #if (CONFIG_LIB_RAND_ORDER > 1) -static inline void fgenerate2(void); +static inline unsigned long fgenerate2(void); #if (CONFIG_LIB_RAND_ORDER == 2) static double_t frand2(void); #endif @@ -90,7 +90,7 @@ static double_t frand2(void); /* Third order congruential generators */ #if (CONFIG_LIB_RAND_ORDER > 2) -static inline void fgenerate3(void); +static inline unsigned long fgenerate3(void); static double_t frand3(void); #endif #endif @@ -141,7 +141,7 @@ static unsigned int nrand(unsigned int nLimit) /* First order congruential generators */ -static inline void fgenerate1(void) +static inline unsigned long fgenerate1(void) { unsigned long randint; @@ -152,6 +152,7 @@ static inline void fgenerate1(void) randint = (RND1_CONSTK * g_randint1) % RND1_CONSTP; g_randint1 = (randint == 0 ? 1 : randint); + return randint; } #if (CONFIG_LIB_RAND_ORDER == 1) @@ -159,18 +160,18 @@ static double_t frand1(void) { /* First order congruential generator. */ - fgenerate1(); + unsigned long randint = fgenerate1(); /* Construct an floating point value in the range from 0.0 up to 1.0 */ - return ((double_t)g_randint1) / ((double_t)RND1_CONSTP); + return ((double_t)randint) / ((double_t)RND1_CONSTP); } #endif /* Second order congruential generators */ #if (CONFIG_LIB_RAND_ORDER > 1) -static inline void fgenerate2(void) +static inline unsigned long fgenerate2(void) { unsigned long randint; @@ -190,6 +191,8 @@ static inline void fgenerate2(void) { g_randint2 = 1; } + + return randint; } #if (CONFIG_LIB_RAND_ORDER == 2) @@ -197,18 +200,18 @@ static double_t frand2(void) { /* Second order congruential generator */ - fgenerate2(); + unsigned long randint = fgenerate2(); /* Construct an floating point value in the range from 0.0 up to 1.0 */ - return ((double_t)g_randint1) / ((double_t)RND2_CONSTP); + return ((double_t)randint) / ((double_t)RND2_CONSTP); } #endif /* Third order congruential generators */ #if (CONFIG_LIB_RAND_ORDER > 2) -static inline void fgenerate3(void) +static inline unsigned long fgenerate3(void) { unsigned long randint; @@ -230,17 +233,19 @@ static inline void fgenerate3(void) { g_randint3 = 1; } + + return randint; } static double_t frand3(void) { /* Third order congruential generator */ - fgenerate3(); + unsigned long randint = fgenerate3(); /* Construct an floating point value in the range from 0.0 up to 1.0 */ - return ((double_t)g_randint1) / ((double_t)RND3_CONSTP); + return ((double_t)randint) / ((double_t)RND3_CONSTP); } #endif #endif @@ -258,10 +263,10 @@ void srand(unsigned int seed) g_randint1 = seed; #if (CONFIG_LIB_RAND_ORDER > 1) g_randint2 = seed; - fgenerate1(); + (void)fgenerate1(); #if (CONFIG_LIB_RAND_ORDER > 2) g_randint3 = seed; - fgenerate2(); + (void)fgenerate2(); #endif #endif } |