[LIBM] Add ldexp.c and ldexpf.c from aocl-libm-ose
authorTimo Kreuzer <timo.kreuzer@reactos.org>
Fri, 24 Jun 2022 18:05:47 +0000 (20:05 +0200)
committerTimo Kreuzer <timo.kreuzer@reactos.org>
Thu, 1 Dec 2022 13:21:59 +0000 (15:21 +0200)
sdk/lib/crt/math/libm_sse2/ldexp.c [new file with mode: 0644]
sdk/lib/crt/math/libm_sse2/ldexpf.c [new file with mode: 0644]

diff --git a/sdk/lib/crt/math/libm_sse2/ldexp.c b/sdk/lib/crt/math/libm_sse2/ldexp.c
new file mode 100644 (file)
index 0000000..3724e7d
--- /dev/null
@@ -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;
+}
+
+
+
+
diff --git a/sdk/lib/crt/math/libm_sse2/ldexpf.c b/sdk/lib/crt/math/libm_sse2/ldexpf.c
new file mode 100644 (file)
index 0000000..49b9ac1
--- /dev/null
@@ -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;
+}
+
+