diff options
author | Yi Sun <yi.sun@intel.com> | 2014-05-30 11:22:32 +0800 |
---|---|---|
committer | Zhigang Gong <zhigang.gong@intel.com> | 2014-05-30 14:59:46 +0800 |
commit | 1dd2362c1c323d9aca9c93009888992cac9cb224 (patch) | |
tree | f8d6f15e85c19e405a14f0be7fe20cbbf3fe8b4a | |
parent | 1af480108c48e58f04126d195b413ab4c6552594 (diff) |
Refine some test for math function
1. nextafter: we originally use nextafter as cpu execution result, It's return value is double, so changed it to nextafterf.
2. sinpi: add judgement to reduce input data limitation from [-2pi,2pi] to [-pi,pi]
3. cospi: define cospi function.
4. tanpi: define tanpi function by using sinpi/cospi.
Signed-off-by: Yi Sun <yi.sun@intel.com>
Signed-off-by: YangweiX Shui <yangweix.shui@intel.com>
-rwxr-xr-x | utests/utest_math_gen.py | 124 |
1 files changed, 77 insertions, 47 deletions
diff --git a/utests/utest_math_gen.py b/utests/utest_math_gen.py index 5a015adf..e6dde10e 100755 --- a/utests/utest_math_gen.py +++ b/utests/utest_math_gen.py @@ -14,6 +14,74 @@ import os,sys # values # ulp +# reduce pi*x limitation to [-pi,pi] +reduce1=''' +static float reduce1( float x ) +{ + SF fx, fy; + fx.f = fy.f = x; + int n; + + fy.spliter.exponent = fx.spliter.exponent - 1; + n = (int)fy.f; + + fx.f = fx.f - 2.0 * n; + + // reduce to [-1.0, 1.0] + fx.f = (fx.f < -1)?(fx.f + 2.0):((fx.f > 1)?(fx.f - 2.0):fx.f); + + return fx.f; +} +''' +# define fuction: cospi +cospi=''' +static float cospi(float x){ + float r = x; + if ( x > 1 || x < -1) r = reduce1(x); + + // reduce to [0.0, 1.0] + if (r < 0) + r = fabs(r); + + if (r >= 0 && r <= 0.25) + return cosf(r * M_PI); + else if (r > 0.25 && r <= 0.5) + return sinf((0.5 - r) * M_PI); + else if (r > 0.5 && r <= 0.75) + return sinf(-(r-0.5) * M_PI); + else if (r > 0.75 && r <= 1.0){ + return -cosf((1 - r) * M_PI);} + + // Error return + return 0xffffffff; +} +''' +# define function: sinpi +sinpi=''' +static float sinpi(float x){ + float r = x; + if ( x > 1 || x < -1) r = reduce1(x); + + // reduce to [-0.5, 0.5] + if (r < -0.5) + r = -1 - r; + else if (r > 0.5) + r = 1 - r; + + if (r > 0.25 && r <= 0.5) + return cosf((0.5 - r) * M_PI); + else if (r >= 0 && r <= 0.25) + return sinf(r * M_PI); + else if (r >= -0.25 && r < 0) + return -sinf(r * -M_PI); + else if (r >= -0.5 && r < -0.25){ + return -cosf((0.5 + r) * M_PI);} + + // Error return + return 0xffffffff; +} +''' + base_input_values = [ 0, 1, 3.14] def main(): ##### gentype acos(gentype) @@ -144,10 +212,7 @@ static float atanpi(float x){ cospi_input_values = base_input_values cospi_input_type = ['float','float2','float4','float8','float16'] cospi_output_type = ['float','float2','float4','float8','float16'] - cospi_cpu_func=''' -static float cospi(float x){ - return cos(M_PI * x); -} ''' + cospi_cpu_func=reduce1+cospi cospiUtests = func('cospi','cospi',[cospi_input_type],cospi_output_type,[cospi_input_values],'2 * FLT_ULP',cospi_cpu_func) # ##### gentype erf(gentype) @@ -364,7 +429,7 @@ static float minmag(float x, float y){ nextafter_input_type1 = ['float','float2','float4','float8','float16'] nextafter_input_type2 = ['float','float2','float4','float8','float16'] nextafter_output_type = ['float','float2','float4','float8','float16'] - nextafterUtests = func('nextafter','nextafter',[nextafter_input_type1,nextafter_input_type2],nextafter_output_type,[nextafter_input_values1,nextafter_input_values2],'0 * FLT_ULP') + nextafterUtests = func('nextafter','nextafterf',[nextafter_input_type1,nextafter_input_type2],nextafter_output_type,[nextafter_input_values1,nextafter_input_values2],'0 * FLT_ULP') ##### gentype pow(gentype x, gentype y) pow_base_values = base_input_values @@ -374,7 +439,7 @@ static float minmag(float x, float y){ pow_input_type1 = ['float','float2','float4','float8','float16'] pow_input_type2 = ['float','float2','float4','float8','float16'] pow_output_type = ['float','float2','float4','float8','float16'] - powUtests = func('pow','pow',[pow_input_type1,pow_input_type2],pow_output_type,[pow_input_values1,pow_input_values2],'16 * FLT_ULP') + powUtests = func('pow','powf',[pow_input_type1,pow_input_type2],pow_output_type,[pow_input_values1,pow_input_values2],'16 * FLT_ULP') ##### floatn pown(floatn x, intn y) pown_input_values1 = [FLT_MAX_POSI,FLT_MIN_NEGA,FLT_MIN_POSI,FLT_MAX_NEGA,80, -80, 3.14, -3.14, -0.5, 0.5, 1, -1, 0.0,6,-6,1500.24,-1500.24] @@ -469,43 +534,7 @@ static float rsqrt(float x) sinpi_input_values = [0, 1, 3.14, -0.88, -0.12, -0.5, 0.5, -0.49, 0.49, 0.51, -0.51, -0.1, 0.1] sinpi_input_type = ['float','float2','float4','float8','float16'] sinpi_output_type = ['float','float2','float4','float8','float16'] - sinpi_cpu_func=''' -static float reduce1( float x ) -{ - SF fx, fy; - fx.f = fy.f = x; - int n; - - fy.spliter.exponent = fx.spliter.exponent - 1; - n = (int)fy.f; - - fx.f = fx.f - 2.0 * n; - - return fx.f; -} - -static float sinpi(float x){ - float r = x; - if ( x > 1 || x < -1) r = reduce1(x); - - // reduce to [-0.5, 0.5] - if(r < -0.5) - r = -1 - r; - else if (r > 0.5) - r = 1 - r; - - if (r > 0.25 && r <= 0.5) - return cos((0.5 - r) * M_PI); - else if (r >= 0 && r <= 0.25) - return sin(r * M_PI); - else if (r >= -0.25 && r < 0) - return -sin(r * -M_PI); - else if (r >= -0.5 && r < -0.25){ - return -cos((0.5 + r) * M_PI);} - - // Error return - return 0xffffffff; -} ''' + sinpi_cpu_func=reduce1+sinpi sinpiUtests = func('sinpi','sinpi',[sinpi_input_type],sinpi_output_type,[sinpi_input_values],'4 * FLT_ULP',sinpi_cpu_func) ##### gentype sqrt(gentype) @@ -530,11 +559,12 @@ static float sinpi(float x){ tanpi_input_values = base_input_values tanpi_input_type = ['float','float2','float4','float8','float16'] tanpi_output_type = ['float','float2','float4','float8','float16'] - tanpi_cpu_func=''' + tanpi_cpu_func=reduce1+sinpi+cospi+''' static float tanpi(float x){ - return tan(M_PI*x); -} ''' - tanpiUtests = func('tanpi','tanpi',[tanpi_input_type],tanpi_output_type,[tanpi_input_values],'4 * FLT_ULP',tanpi_cpu_func) + return sinpi(x)/cospi(x); +} +''' + tanpiUtests = func('tanpi','tanpi',[tanpi_input_type],tanpi_output_type,[tanpi_input_values],'400 * FLT_ULP',tanpi_cpu_func) ##### gentype trunc(gentype) trunc_input_values = base_input_values |