[LIBM] Add ldexp.c and ldexpf.c from aocl-libm-ose

This commit is contained in:
Timo Kreuzer 2022-06-24 20:05:47 +02:00
parent 105426b81a
commit 4f348ee971
2 changed files with 226 additions and 0 deletions

View file

@ -0,0 +1,116 @@
/*
* Copyright (C) 2008-2020 Advanced Micro Devices, Inc. All rights reserved.
*
* 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 of the copyright holder 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 HOLDER 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 "fn_macros.h"
#include "libm_util_amd.h"
#include "libm_special.h"
double FN_PROTOTYPE_REF(ldexp)(double x, int n)
{
UT64 val,val_x;
unsigned int sign;
int exponent;
val.f64 = x;
val_x.f64 = x;
sign = val.u32[1] & 0x80000000;
val.u32[1] = val.u32[1] & 0x7fffffff; /* remove the sign bit */
if (val.u64 > 0x7ff0000000000000) /* x is NaN */
#ifdef WINDOWS
return __amd_handle_error("ldexp", __amd_ldexp, val_x.u64|0x0008000000000000, _DOMAIN, 0, EDOM, x, n, 2);
#else
{
if(!(val.u64 & 0x0008000000000000))// x is snan
return __amd_handle_error("ldexp", __amd_ldexp, val_x.u64|0x0008000000000000, _DOMAIN, AMD_F_INVALID, EDOM, x, n, 2);
else
return x;
}
#endif
if(val.u64 == 0x7ff0000000000000)/* x = +-inf*/
return x;
if((val.u64 == 0x0000000000000000) || (n==0))
return x; /* x= +-0 or n= 0*/
exponent = val.u32[1] >> 20; /* get the exponent */
if(exponent == 0)/*x is denormal*/
{
val.f64 = val.f64 * VAL_2PMULTIPLIER_DP;/*multiply by 2^53 to bring it to the normal range*/
exponent = val.u32[1] >> 20; /* get the exponent */
exponent = exponent + n - MULTIPLIER_DP;
if(exponent < -MULTIPLIER_DP)/*underflow*/
{
val.u32[1] = sign | 0x00000000;
val.u32[0] = 0x00000000;
return __amd_handle_error("ldexp", __amd_ldexp, val.u64, _UNDERFLOW, AMD_F_INEXACT|AMD_F_UNDERFLOW, ERANGE, x, (double)n, 2);
}
if(exponent > 2046)/*overflow*/
{
val.u32[1] = sign | 0x7ff00000;
val.u32[0] = 0x00000000;
return __amd_handle_error("ldexp", __amd_ldexp, val.u64, _OVERFLOW, AMD_F_INEXACT|AMD_F_OVERFLOW, ERANGE ,x, (double)n, 2);
}
exponent += MULTIPLIER_DP;
val.u32[1] = sign | (exponent << 20) | (val.u32[1] & 0x000fffff);
val.f64 = val.f64 * VAL_2PMMULTIPLIER_DP;
return val.f64;
}
exponent += n;
if(exponent < -MULTIPLIER_DP)/*underflow*/
{
val.u32[1] = sign | 0x00000000;
val.u32[0] = 0x00000000;
return __amd_handle_error("ldexp", __amd_ldexp, val.u64, _UNDERFLOW, AMD_F_INEXACT|AMD_F_UNDERFLOW, ERANGE, x, (double)n, 2);
}
if(exponent < 1)/*x is normal but output is debnormal*/
{
exponent += MULTIPLIER_DP;
val.u32[1] = sign | (exponent << 20) | (val.u32[1] & 0x000fffff);
val.f64 = val.f64 * VAL_2PMMULTIPLIER_DP;
return val.f64;
}
if(exponent > 2046)/*overflow*/
{
val.u32[1] = sign | 0x7ff00000;
val.u32[0] = 0x00000000;
return __amd_handle_error("ldexp", __amd_ldexp, val.u64, _OVERFLOW, AMD_F_INEXACT|AMD_F_OVERFLOW, ERANGE ,x, (double)n, 2);
}
val.u32[1] = sign | (exponent << 20) | (val.u32[1] & 0x000fffff);
return val.f64;
}

View file

@ -0,0 +1,110 @@
/*
* Copyright (C) 2008-2020 Advanced Micro Devices, Inc. All rights reserved.
*
* 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 of the copyright holder 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 HOLDER 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 "fn_macros.h"
#include "libm_util_amd.h"
#include "libm_special.h"
float FN_PROTOTYPE_REF(ldexpf)(float x, int n)
{
UT32 val,val_x;
unsigned int sign;
int exponent;
val.f32 = x;
val_x.f32 = x;
sign = val.u32 & 0x80000000;
val.u32 = val.u32 & 0x7fffffff;/* remove the sign bit */
if(val.u32 > 0x7f800000)/* x= nan */
#ifdef WINDOWS
return __amd_handle_errorf("ldexpf", __amd_ldexp, val_x.u32|0x00400000, _DOMAIN, 0, EDOM, x, (float)n, 2);
#else
{
if(!(val.u32 & 0x00400000)) //x is snan
return __amd_handle_errorf("ldexpf", __amd_ldexp, val_x.u32|0x00400000, _DOMAIN, AMD_F_INVALID, EDOM, x, (float)n, 2);
else
return x;
}
#endif
if(val.u32 == 0x7f800000)/* x = +-inf*/
return x;
if((val.u32 == 0x00000000) || (n==0))/* x= +-0 or n= 0*/
return x;
exponent = val.u32 >> 23; /* get the exponent */
if(exponent == 0)/*x is denormal*/
{
val.f32 = val.f32 * VAL_2PMULTIPLIER_SP;/*multiply by 2^24 to bring it to the normal range*/
exponent = (val.u32 >> 23); /* get the exponent */
exponent = exponent + n - MULTIPLIER_SP;
if(exponent < -MULTIPLIER_SP)/*underflow*/
{
val.u32 = sign | 0x00000000;
return __amd_handle_errorf("ldexpf", __amd_ldexp, val.u32, _UNDERFLOW, AMD_F_INEXACT|AMD_F_UNDERFLOW, ERANGE, x, (float)n, 2);
}
if(exponent > 254)/*overflow*/
{
val.u32 = sign | 0x7f800000;
return __amd_handle_errorf("ldexpf", __amd_ldexp, val.u32, _OVERFLOW, AMD_F_INEXACT|AMD_F_OVERFLOW, ERANGE, x, (float)n, 2);
}
exponent += MULTIPLIER_SP;
val.u32 = sign | (exponent << 23) | (val.u32 & 0x007fffff);
val.f32 = val.f32 * VAL_2PMMULTIPLIER_SP;
return val.f32;
}
exponent += n;
if(exponent < -MULTIPLIER_SP)/*underflow*/
{
val.u32 = sign | 0x00000000;
return __amd_handle_errorf("ldexpf", __amd_ldexp, val.u32, _UNDERFLOW, AMD_F_INEXACT|AMD_F_UNDERFLOW, ERANGE, x, (float)n, 2);
}
if(exponent < 1)/*x is normal but output is debnormal*/
{
exponent += MULTIPLIER_SP;
val.u32 = sign | (exponent << 23) | (val.u32 & 0x007fffff);
val.f32 = val.f32 * VAL_2PMMULTIPLIER_SP;
return val.f32;
}
if(exponent > 254)/*overflow*/
{
val.u32 = sign | 0x7f800000;
return __amd_handle_errorf("ldexpf", __amd_ldexp, val.u32, _OVERFLOW, AMD_F_INEXACT|AMD_F_OVERFLOW, ERANGE, x, (float)n, 2);
}
val.u32 = sign | (exponent << 23) | (val.u32 & 0x007fffff);/*x is normal and output is normal*/
return val.f32;
}