[libc-commits] [libc] d412804 - [libc] Add implementations of fdim[f|l].

Tue Ly via libc-commits libc-commits at lists.llvm.org
Tue Nov 10 15:49:30 PST 2020


Author: Tue Ly
Date: 2020-11-10T18:48:11-05:00
New Revision: d41280467d69f056fc74b3bf31e82c86dcbd62bf

URL: https://github.com/llvm/llvm-project/commit/d41280467d69f056fc74b3bf31e82c86dcbd62bf
DIFF: https://github.com/llvm/llvm-project/commit/d41280467d69f056fc74b3bf31e82c86dcbd62bf.diff

LOG: [libc] Add implementations of fdim[f|l].

Implementing fdim, fdimf, and fdiml for llvm-libc.

Differential Revision: https://reviews.llvm.org/D90906

Added: 
    libc/src/math/fdim.cpp
    libc/src/math/fdim.h
    libc/src/math/fdimf.cpp
    libc/src/math/fdimf.h
    libc/src/math/fdiml.cpp
    libc/src/math/fdiml.h
    libc/test/src/math/FDimTest.h
    libc/test/src/math/fdim_test.cpp
    libc/test/src/math/fdimf_test.cpp
    libc/test/src/math/fdiml_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/spec/stdc.td
    libc/src/math/CMakeLists.txt
    libc/test/src/math/CMakeLists.txt
    libc/utils/FPUtil/BasicOperations.h

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index a8aeb6dc0021..6ee0e98541c4 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -52,6 +52,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsl
+    libc.src.math.fdim
+    libc.src.math.fdimf
+    libc.src.math.fdiml
     libc.src.math.floor
     libc.src.math.floorf
     libc.src.math.floorl

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index f72fb77357f3..c1a80288a6a6 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -85,6 +85,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsl
+    libc.src.math.fdim
+    libc.src.math.fdimf
+    libc.src.math.fdiml
     libc.src.math.floor
     libc.src.math.floorf
     libc.src.math.floorl

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index de2cbea179cd..c587f5b73013 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -258,6 +258,10 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"fabsf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
           FunctionSpec<"fabsl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
 
+          FunctionSpec<"fdim", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
+          FunctionSpec<"fdimf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
+          FunctionSpec<"fdiml", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+
           FunctionSpec<"floor", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
           FunctionSpec<"floorf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
           FunctionSpec<"floorl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,

diff  --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 6c2ff2cc7c62..b7614cfec612 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -641,3 +641,39 @@ add_entrypoint_object(
   COMPILE_OPTIONS
     -O2
 )
+
+add_entrypoint_object(
+  fdim
+  SRCS
+    fdim.cpp
+  HDRS
+    fdim.h
+  DEPENDS
+    libc.utils.FPUtil.fputil
+  COMPILE_OPTIONS
+    -O2
+)
+
+add_entrypoint_object(
+  fdimf
+  SRCS
+    fdimf.cpp
+  HDRS
+    fdimf.h
+  DEPENDS
+    libc.utils.FPUtil.fputil
+  COMPILE_OPTIONS
+    -O2
+)
+
+add_entrypoint_object(
+  fdiml
+  SRCS
+    fdiml.cpp
+  HDRS
+    fdiml.h
+  DEPENDS
+    libc.utils.FPUtil.fputil
+  COMPILE_OPTIONS
+    -O2
+)

diff  --git a/libc/src/math/fdim.cpp b/libc/src/math/fdim.cpp
new file mode 100644
index 000000000000..23b475c94b91
--- /dev/null
+++ b/libc/src/math/fdim.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of fdim function -----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/common.h"
+#include "utils/FPUtil/BasicOperations.h"
+
+namespace __llvm_libc {
+
+double LLVM_LIBC_ENTRYPOINT(fdim)(double x, double y) {
+  return fputil::fdim(x, y);
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/math/fdim.h b/libc/src/math/fdim.h
new file mode 100644
index 000000000000..f838c121e291
--- /dev/null
+++ b/libc/src/math/fdim.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for fdim --------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_FDIM_H
+#define LLVM_LIBC_SRC_MATH_FDIM_H
+
+namespace __llvm_libc {
+
+double fdim(double x, double y);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_FDIM_H

diff  --git a/libc/src/math/fdimf.cpp b/libc/src/math/fdimf.cpp
new file mode 100644
index 000000000000..84f307b917a3
--- /dev/null
+++ b/libc/src/math/fdimf.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of fdimf function ----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/common.h"
+#include "utils/FPUtil/BasicOperations.h"
+
+namespace __llvm_libc {
+
+float LLVM_LIBC_ENTRYPOINT(fdimf)(float x, float y) {
+  return fputil::fdim(x, y);
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/math/fdimf.h b/libc/src/math/fdimf.h
new file mode 100644
index 000000000000..50c586c49cc3
--- /dev/null
+++ b/libc/src/math/fdimf.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for fdimf -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_FDIMF_H
+#define LLVM_LIBC_SRC_MATH_FDIMF_H
+
+namespace __llvm_libc {
+
+float fdimf(float x, float y);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_FDIMF_H

diff  --git a/libc/src/math/fdiml.cpp b/libc/src/math/fdiml.cpp
new file mode 100644
index 000000000000..60c0a74984d4
--- /dev/null
+++ b/libc/src/math/fdiml.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of fdiml function ----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/common.h"
+#include "utils/FPUtil/BasicOperations.h"
+
+namespace __llvm_libc {
+
+long double LLVM_LIBC_ENTRYPOINT(fdiml)(long double x, long double y) {
+  return fputil::fdim(x, y);
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/math/fdiml.h b/libc/src/math/fdiml.h
new file mode 100644
index 000000000000..6de261fe861a
--- /dev/null
+++ b/libc/src/math/fdiml.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for fdiml -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_FDIML_H
+#define LLVM_LIBC_SRC_MATH_FDIML_H
+
+namespace __llvm_libc {
+
+long double fdiml(long double x, long double y);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_FDIML_H

diff  --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 4f8f77e4cf03..0f5405cb3a26 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -484,6 +484,48 @@ add_fp_unittest(
     libc.utils.FPUtil.fputil
 )
 
+add_fp_unittest(
+  fdimf_test
+  SUITE
+    libc_math_unittests
+  SRCS
+    fdimf_test.cpp
+  HDRS
+    FDimTest.h
+  DEPENDS
+    libc.include.math
+    libc.src.math.fdimf
+    libc.utils.FPUtil.fputil
+)
+
+add_fp_unittest(
+  fdim_test
+  SUITE
+    libc_math_unittests
+  SRCS
+    fdim_test.cpp
+  HDRS
+    FDimTest.h
+  DEPENDS
+    libc.include.math
+    libc.src.math.fdim
+    libc.utils.FPUtil.fputil
+)
+
+add_fp_unittest(
+  fdiml_test
+  SUITE
+    libc_math_unittests
+  SRCS
+    fdiml_test.cpp
+  HDRS
+    FDimTest.h
+  DEPENDS
+    libc.include.math
+    libc.src.math.fdiml
+    libc.utils.FPUtil.fputil
+)
+
 add_fp_unittest(
   fminf_test
   SUITE

diff  --git a/libc/test/src/math/FDimTest.h b/libc/test/src/math/FDimTest.h
new file mode 100644
index 000000000000..5ebe7d38ebe9
--- /dev/null
+++ b/libc/test/src/math/FDimTest.h
@@ -0,0 +1,82 @@
+//===-- Utility class to test 
diff erent flavors of fdim ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===---------------------------------------------------------------------===//
+
+#include "include/math.h"
+#include "utils/FPUtil/BasicOperations.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
+#include "utils/UnitTest/Test.h"
+
+template <typename T>
+class FDimTestTemplate : public __llvm_libc::testing::Test {
+public:
+  using FuncPtr = T (*)(T, T);
+  using FPBits = __llvm_libc::fputil::FPBits<T>;
+  using UIntType = typename FPBits::UIntType;
+
+  void testNaNArg(FuncPtr func) {
+    EXPECT_FP_EQ(nan, func(nan, inf));
+    EXPECT_FP_EQ(nan, func(negInf, nan));
+    EXPECT_FP_EQ(nan, func(nan, zero));
+    EXPECT_FP_EQ(nan, func(negZero, nan));
+    EXPECT_FP_EQ(nan, func(nan, T(-1.2345)));
+    EXPECT_FP_EQ(nan, func(T(1.2345), nan));
+    EXPECT_NE(isnan(func(nan, nan)), 0);
+  }
+
+  void testInfArg(FuncPtr func) {
+    EXPECT_FP_EQ(zero, func(negInf, inf));
+    EXPECT_FP_EQ(inf, func(inf, zero));
+    EXPECT_FP_EQ(zero, func(negZero, inf));
+    EXPECT_FP_EQ(inf, func(inf, T(1.2345)));
+    EXPECT_FP_EQ(zero, func(T(-1.2345), inf));
+  }
+
+  void testNegInfArg(FuncPtr func) {
+    EXPECT_FP_EQ(inf, func(inf, negInf));
+    EXPECT_FP_EQ(zero, func(negInf, zero));
+    EXPECT_FP_EQ(inf, func(negZero, negInf));
+    EXPECT_FP_EQ(zero, func(negInf, T(-1.2345)));
+    EXPECT_FP_EQ(inf, func(T(1.2345), negInf));
+  }
+
+  void testBothZero(FuncPtr func) {
+    EXPECT_FP_EQ(zero, func(zero, zero));
+    EXPECT_FP_EQ(zero, func(zero, negZero));
+    EXPECT_FP_EQ(zero, func(negZero, zero));
+    EXPECT_FP_EQ(zero, func(negZero, negZero));
+  }
+
+  void testInRange(FuncPtr func) {
+    constexpr UIntType count = 10000001;
+    constexpr UIntType step = UIntType(-1) / count;
+    for (UIntType i = 0, v = 0, w = UIntType(-1); i <= count;
+         ++i, v += step, w -= step) {
+      T x = FPBits(v), y = FPBits(w);
+      if (isnan(x) || isinf(x))
+        continue;
+      if (isnan(y) || isinf(y))
+        continue;
+
+      if (x > y) {
+        EXPECT_FP_EQ(x - y, func(x, y));
+      } else {
+        EXPECT_FP_EQ(zero, func(x, y));
+      }
+    }
+  }
+
+private:
+  // constexpr does not work on FPBits yet, so we cannot have these constants as
+  // static.
+  const T nan = __llvm_libc::fputil::FPBits<T>::buildNaN(1);
+  const T inf = __llvm_libc::fputil::FPBits<T>::inf();
+  const T negInf = __llvm_libc::fputil::FPBits<T>::negInf();
+  const T zero = __llvm_libc::fputil::FPBits<T>::zero();
+  const T negZero = __llvm_libc::fputil::FPBits<T>::negZero();
+};

diff  --git a/libc/test/src/math/fdim_test.cpp b/libc/test/src/math/fdim_test.cpp
new file mode 100644
index 000000000000..a30a1abab9c1
--- /dev/null
+++ b/libc/test/src/math/fdim_test.cpp
@@ -0,0 +1,27 @@
+//===-- Unittests for fdim ------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "FDimTest.h"
+
+#include "include/math.h"
+#include "src/math/fdim.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
+#include "utils/UnitTest/Test.h"
+
+using FDimTest = FDimTestTemplate<double>;
+
+TEST_F(FDimTest, NaNArg_fdim) { testNaNArg(&__llvm_libc::fdim); }
+
+TEST_F(FDimTest, InfArg_fdim) { testInfArg(&__llvm_libc::fdim); }
+
+TEST_F(FDimTest, NegInfArg_fdim) { testNegInfArg(&__llvm_libc::fdim); }
+
+TEST_F(FDimTest, BothZero_fdim) { testBothZero(&__llvm_libc::fdim); }
+
+TEST_F(FDimTest, InDoubleRange_fdim) { testInRange(&__llvm_libc::fdim); }

diff  --git a/libc/test/src/math/fdimf_test.cpp b/libc/test/src/math/fdimf_test.cpp
new file mode 100644
index 000000000000..1a6d640789fe
--- /dev/null
+++ b/libc/test/src/math/fdimf_test.cpp
@@ -0,0 +1,27 @@
+//===-- Unittests for fdimf -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "FDimTest.h"
+
+#include "include/math.h"
+#include "src/math/fdimf.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
+#include "utils/UnitTest/Test.h"
+
+using FDimTest = FDimTestTemplate<float>;
+
+TEST_F(FDimTest, NaNArg_fdimf) { testNaNArg(&__llvm_libc::fdimf); }
+
+TEST_F(FDimTest, InfArg_fdimf) { testInfArg(&__llvm_libc::fdimf); }
+
+TEST_F(FDimTest, NegInfArg_fdimf) { testNegInfArg(&__llvm_libc::fdimf); }
+
+TEST_F(FDimTest, BothZero_fdimf) { testBothZero(&__llvm_libc::fdimf); }
+
+TEST_F(FDimTest, InFloatRange_fdimf) { testInRange(&__llvm_libc::fdimf); }

diff  --git a/libc/test/src/math/fdiml_test.cpp b/libc/test/src/math/fdiml_test.cpp
new file mode 100644
index 000000000000..3453f7cd92fb
--- /dev/null
+++ b/libc/test/src/math/fdiml_test.cpp
@@ -0,0 +1,27 @@
+//===-- Unittests for fdiml -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "FDimTest.h"
+
+#include "include/math.h"
+#include "src/math/fdiml.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
+#include "utils/UnitTest/Test.h"
+
+using FDimTest = FDimTestTemplate<long double>;
+
+TEST_F(FDimTest, NaNArg_fdiml) { testNaNArg(&__llvm_libc::fdiml); }
+
+TEST_F(FDimTest, InfArg_fdiml) { testInfArg(&__llvm_libc::fdiml); }
+
+TEST_F(FDimTest, NegInfArg_fdiml) { testNegInfArg(&__llvm_libc::fdiml); }
+
+TEST_F(FDimTest, BothZero_fdiml) { testBothZero(&__llvm_libc::fdiml); }
+
+TEST_F(FDimTest, InLongDoubleRange_fdiml) { testInRange(&__llvm_libc::fdiml); }

diff  --git a/libc/utils/FPUtil/BasicOperations.h b/libc/utils/FPUtil/BasicOperations.h
index 78856926af4b..5bf3effc1c01 100644
--- a/libc/utils/FPUtil/BasicOperations.h
+++ b/libc/utils/FPUtil/BasicOperations.h
@@ -62,6 +62,22 @@ static inline T fmax(T x, T y) {
   }
 }
 
+template <typename T,
+          cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
+static inline T fdim(T x, T y) {
+  FPBits<T> bitx(x), bity(y);
+
+  if (bitx.isNaN()) {
+    return x;
+  }
+
+  if (bity.isNaN()) {
+    return y;
+  }
+
+  return (x > y ? x - y : 0);
+}
+
 } // namespace fputil
 } // namespace __llvm_libc
 


        


More information about the libc-commits mailing list