Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math: p_round: Implement Round Function #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions include/pal_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/math/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
31 changes: 31 additions & 0 deletions src/math/p_round.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <pal.h>

/*
*
* 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));
}
}
5 changes: 5 additions & 0 deletions tests/math/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
100 changes: 100 additions & 0 deletions tests/math/gold/p_round_f32.dat
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions tests/math/p_round.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <math.h>
#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]);
}