From 861c7bc75094e91c46e414beea3c05a3413745e0 Mon Sep 17 00:00:00 2001 From: Adam Szewczyk Date: Wed, 26 Aug 2015 22:48:35 -0400 Subject: [PATCH] math: p_round: Implement Round Function --- README.md | 3 +- include/pal_math.h | 3 + src/math/Makefile.am | 1 + src/math/p_round.c | 31 ++++++++++ tests/math/Makefile.am | 5 ++ tests/math/gold/p_round_f32.dat | 100 ++++++++++++++++++++++++++++++++ tests/math/p_round.c | 10 ++++ 7 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/math/p_round.c create mode 100644 tests/math/gold/p_round_f32.dat create mode 100644 tests/math/p_round.c diff --git a/README.md b/README.md index fc86fbb..0483e7d 100644 --- a/README.md +++ b/README.md @@ -233,7 +233,8 @@ FUNCTION | NOTES [p_popcount()](src/math/p_popcount.c) | count the number of bits set [p_pow()](src/math/p_pow.c) | element raised to a power [p_rand()](src/math/p_rand.c) | random number generator -[p_randinit()](src/math/p_rand.c) | init random number generator +[p_randinit()](src/math/p_rand.c) | init random number generator +[p_round()](src/math/p_round.c) | round [p_sort()](src/math/p_sort.c) | heap sort [p_sin()](src/math/p_sin.c) | sine [p_sinh()](src/math/p_sinh.c) | hyperbolic sine diff --git a/include/pal_math.h b/include/pal_math.h index e1d4bcc..470a731 100644 --- a/include/pal_math.h +++ b/include/pal_math.h @@ -158,6 +158,9 @@ void p_log10_f32(const float *a, float *c, int n); /*element raised to a power: c = pow ( a , b ) */ void p_pow_f32(const float *a, const float *b, float *c, int n); +/*round: c = round ( a ) */ +void p_round_f32(const float *a, float *c, int n); + /*sine: c = sin ( a ) */ void p_sin_f32(const float *a, float *c, int n); diff --git a/src/math/Makefile.am b/src/math/Makefile.am index 3dea94c..41588fb 100644 --- a/src/math/Makefile.am +++ b/src/math/Makefile.am @@ -38,6 +38,7 @@ libpal_math_la_SOURCES = \ p_popcount.c \ p_pow.c \ p_rand.c \ + p_round.c \ p_sin.c \ p_sincos.c \ p_sinh.c \ diff --git a/src/math/p_round.c b/src/math/p_round.c new file mode 100644 index 0000000..6ab15d7 --- /dev/null +++ b/src/math/p_round.c @@ -0,0 +1,31 @@ +#include + +/* + * + * Compute the round of a + * + * @param a Pointer to input vector + * + * @param c Pointer to output vector + * + * @param n Size of 'a' and 'c' vector. + * + * @return None + * + */ + +void p_round_f32(const float *a, float *c, int n) +{ + int i;int sign; float ff; float x; + for ( i = 0; i < n; i++) + { + const float *pa = (a+i); + float *pc = (c+i); + ff = *pa; + if (ff >= 0) sign = 1.0; else sign = -1.0; // store sign of input variable + if (ff < 0) x = ff * (-1.0f); else x = ff; // this is equivalent of x=abs(a) + + if (x >= 0x1.0p23) *pc = sign*x; + *pc = sign*((float) (unsigned int) (x + 0.49999997f)); + } +} \ No newline at end of file diff --git a/tests/math/Makefile.am b/tests/math/Makefile.am index 332b441..7fa8946 100644 --- a/tests/math/Makefile.am +++ b/tests/math/Makefile.am @@ -52,6 +52,7 @@ BUILT_SOURCES = \ gold/p_mode_f32.gold.h \ gold/p_mul_f32.gold.h \ gold/p_pow_f32.gold.h \ + gold/p_round_f32.gold.h \ gold/p_sin_f32.gold.h \ gold/p_sincos_f32.gold.h \ gold/p_sinh_f32.gold.h \ @@ -114,6 +115,7 @@ check_PROGRAMS = \ check_p_popcount \ check_p_pow_f32 \ check_p_rand \ + check_p_round_f32 \ check_p_sin_f32 \ check_p_sincos_f32 \ check_p_sinh_f32 \ @@ -159,6 +161,7 @@ check_p_mul_f32_SOURCES = $(SIMPLE) check_p_popcount_SOURCES = $(NOTEST) check_p_pow_f32_SOURCES = $(SIMPLE) p_pow.c check_p_rand_SOURCES = $(NOTEST) +check_p_round_f32_SOURCES = $(SIMPLE) p_round.c check_p_sin_f32_SOURCES = $(SIMPLE) p_sin.c check_p_sincos_f32_SOURCES = check_p_sincos.c check_p_sinh_f32_SOURCES = $(SIMPLE) p_sinh.c @@ -205,6 +208,7 @@ check_p_popcount_CFLAGS = -DFUNCTION=p_popcount # Override fault tolerance for p_pow_f32 check_p_pow_f32_CFLAGS = -DFUNCTION=p_pow_f32 -DIS_BINARY -DEPSILON_MAX=0.003f check_p_rand_CFLAGS = -DFUNCTION=p_rand -DSCALAR_OUTPUT +check_p_round_f32_CFLAGS = -DFUNCTION=p_round_f32 -DIS_UNARY check_p_sin_f32_CFLAGS = -DFUNCTION=p_sin_f32 -DIS_UNARY check_p_sincos_f32_CFLAGS = -DFUNCTION=p_sincos_f32 check_p_sinh_f32_CFLAGS = -DFUNCTION=p_sinh_f32 -DIS_UNARY @@ -251,6 +255,7 @@ check_p_mul_f32_LDFLAGS = $(CHECKLDFLAGS) check_p_popcount_LDFLAGS = $(CHECKLDFLAGS) check_p_pow_f32_LDFLAGS = $(CHECKLDFLAGS) check_p_rand_LDFLAGS = $(CHECKLDFLAGS) +check_p_round_f32_LDFLAGS = $(CHECKLDFLAGS) check_p_sin_f32_LDFLAGS = $(CHECKLDFLAGS) check_p_sincos_f32_LDFLAGS = $(CHECKLDFLAGS) check_p_sinh_f32_LDFLAGS = $(CHECKLDFLAGS) diff --git a/tests/math/gold/p_round_f32.dat b/tests/math/gold/p_round_f32.dat new file mode 100644 index 0000000..3b2ad10 --- /dev/null +++ b/tests/math/gold/p_round_f32.dat @@ -0,0 +1,100 @@ +4.625243,0.000000,0.000000,5.000000 +10.712888,0.000000,0.000000,11.000000 +2.252235,0.000000,0.000000,2.000000 +2.280635,0.000000,0.000000,2.000000 +10.588324,0.000000,0.000000,11.000000 +6.065655,0.000000,0.000000,6.000000 +1.591502,0.000000,0.000000,2.000000 +10.343284,0.000000,0.000000,10.000000 +5.718785,0.000000,0.000000,6.000000 +5.982005,0.000000,0.000000,6.000000 +10.705152,0.000000,0.000000,11.000000 +7.696878,0.000000,0.000000,8.000000 +8.047289,0.000000,0.000000,8.000000 +10.776376,0.000000,0.000000,11.000000 +9.148800,0.000000,0.000000,9.000000 +3.082119,0.000000,0.000000,3.000000 +3.194645,0.000000,0.000000,3.000000 +7.935914,0.000000,0.000000,8.000000 +7.476189,0.000000,0.000000,7.000000 +7.470324,0.000000,0.000000,7.000000 +6.958097,0.000000,0.000000,7.000000 +8.072808,0.000000,0.000000,8.000000 +8.835902,0.000000,0.000000,9.000000 +4.629804,0.000000,0.000000,5.000000 +8.474785,0.000000,0.000000,8.000000 +6.133525,0.000000,0.000000,6.000000 +3.945584,0.000000,0.000000,4.000000 +9.232484,0.000000,0.000000,9.000000 +6.067547,0.000000,0.000000,6.000000 +8.317123,0.000000,0.000000,8.000000 +10.442951,0.000000,0.000000,10.000000 +10.692790,0.000000,0.000000,11.000000 +5.546363,0.000000,0.000000,6.000000 +10.211538,0.000000,0.000000,10.000000 +10.489777,0.000000,0.000000,10.000000 +5.134687,0.000000,0.000000,5.000000 +5.277193,0.000000,0.000000,5.000000 +1.081279,0.000000,0.000000,1.000000 +1.994324,0.000000,0.000000,2.000000 +8.512331,0.000000,0.000000,9.000000 +7.063284,0.000000,0.000000,7.000000 +1.699476,0.000000,0.000000,2.000000 +5.209209,0.000000,0.000000,5.000000 +1.626925,0.000000,0.000000,2.000000 +1.475853,0.000000,0.000000,1.000000 +0.874361,0.000000,0.000000,1.000000 +2.225396,0.000000,0.000000,2.000000 +2.186850,0.000000,0.000000,2.000000 +8.810275,0.000000,0.000000,9.000000 +7.217938,0.000000,0.000000,7.000000 +-9.657174,0.000000,0.000000,-10.000000 +-2.284724,0.000000,0.000000,-2.000000 +-4.290746,0.000000,0.000000,-4.000000 +-5.009429,0.000000,0.000000,-5.000000 +-4.430880,0.000000,0.000000,-4.000000 +-1.765531,0.000000,0.000000,-2.000000 +-0.142954,0.000000,0.000000,-0.000000 +-8.376464,0.000000,0.000000,-8.000000 +-10.998015,0.000000,0.000000,-11.000000 +-6.210501,0.000000,0.000000,-6.000000 +-3.209939,0.000000,0.000000,-3.000000 +-7.957318,0.000000,0.000000,-8.000000 +-3.419643,0.000000,0.000000,-3.000000 +-8.756303,0.000000,0.000000,-9.000000 +-7.168857,0.000000,0.000000,-7.000000 +-2.909420,0.000000,0.000000,-3.000000 +-0.407342,0.000000,0.000000,-0.000000 +-9.962402,0.000000,0.000000,-10.000000 +-3.990700,0.000000,0.000000,-4.000000 +-2.401666,0.000000,0.000000,-2.000000 +-4.991085,0.000000,0.000000,-5.000000 +-8.570336,0.000000,0.000000,-9.000000 +-1.617495,0.000000,0.000000,-2.000000 +-7.716646,0.000000,0.000000,-8.000000 +-7.713614,0.000000,0.000000,-8.000000 +-0.609700,0.000000,0.000000,-1.000000 +-8.591007,0.000000,0.000000,-9.000000 +-9.939010,0.000000,0.000000,-10.000000 +-2.796550,0.000000,0.000000,-3.000000 +-3.917634,0.000000,0.000000,-4.000000 +-6.156948,0.000000,0.000000,-6.000000 +-9.970076,0.000000,0.000000,-10.000000 +-6.202358,0.000000,0.000000,-6.000000 +-7.964046,0.000000,0.000000,-8.000000 +-3.979505,0.000000,0.000000,-4.000000 +-10.633238,0.000000,0.000000,-11.000000 +-7.245930,0.000000,0.000000,-7.000000 +-4.122460,0.000000,0.000000,-4.000000 +-5.526055,0.000000,0.000000,-6.000000 +-4.760297,0.000000,0.000000,-5.000000 +-7.849313,0.000000,0.000000,-8.000000 +-6.252346,0.000000,0.000000,-6.000000 +-10.233968,0.000000,0.000000,-10.000000 +-0.268957,0.000000,0.000000,-0.000000 +-4.008649,0.000000,0.000000,-4.000000 +-6.402825,0.000000,0.000000,-6.000000 +-3.178377,0.000000,0.000000,-3.000000 +-1.932344,0.000000,0.000000,-2.000000 +-2.881579,0.000000,0.000000,-3.000000 +-4.685429,0.000000,0.000000,-5.000000 diff --git a/tests/math/p_round.c b/tests/math/p_round.c new file mode 100644 index 0000000..5494d7e --- /dev/null +++ b/tests/math/p_round.c @@ -0,0 +1,10 @@ +#include +#include "simple.h" + +void generate_ref(float *out, size_t n) +{ + size_t i; + + for (i = 0; i < n; i++) + out[i] = roundf(ai[i]); +}