27 changes: 27 additions & 0 deletions libc/test/src/math/fdiml_test.cpp
Original file line number Diff line number Diff line change
@@ -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); }
16 changes: 16 additions & 0 deletions libc/utils/FPUtil/BasicOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down