Skip to content

Commit

Permalink
[libc] Add implementations of remquo[f|l] and remainder[f|l].
Browse files Browse the repository at this point in the history
The implementation is not fully standards compliant in the sense that
errno is not set on error, and floating point exceptions are not raised.

Subnormal range and normal range are tested separately in the tests.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D86666
  • Loading branch information
Siva Chandra Reddy committed Sep 4, 2020
1 parent 060c9dd commit 8514ecb
Show file tree
Hide file tree
Showing 24 changed files with 759 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libc/config/linux/api.td
Expand Up @@ -199,6 +199,12 @@ def MathAPI : PublicAPI<"math.h"> {
"modfl",
"expf",
"exp2f",
"remainderf",
"remainder",
"remainderl",
"remquof",
"remquo",
"remquol",
"round",
"roundf",
"roundl",
Expand Down
6 changes: 6 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Expand Up @@ -103,6 +103,12 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.modf
libc.src.math.modff
libc.src.math.modfl
libc.src.math.remainderf
libc.src.math.remainder
libc.src.math.remainderl
libc.src.math.remquof
libc.src.math.remquo
libc.src.math.remquol
libc.src.math.round
libc.src.math.roundf
libc.src.math.roundl
Expand Down
8 changes: 8 additions & 0 deletions libc/spec/stdc.td
Expand Up @@ -310,6 +310,14 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"expf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"exp2f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

FunctionSpec<"remainderf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
FunctionSpec<"remainder", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
FunctionSpec<"remainderl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,

FunctionSpec<"remquof", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>, ArgSpec<IntPtr>]>,
FunctionSpec<"remquo", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>, ArgSpec<IntPtr>]>,
FunctionSpec<"remquol", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>, ArgSpec<IntPtr>]>,

FunctionSpec<"round", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"roundf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"roundl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
Expand Down
72 changes: 72 additions & 0 deletions libc/src/math/CMakeLists.txt
Expand Up @@ -521,3 +521,75 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
remquof
SRCS
remquof.cpp
HDRS
remquof.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
remquo
SRCS
remquo.cpp
HDRS
remquo.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
remquol
SRCS
remquol.cpp
HDRS
remquol.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
remainderf
SRCS
remainderf.cpp
HDRS
remainderf.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
remainder
SRCS
remainder.cpp
HDRS
remainder.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
remainderl
SRCS
remainderl.cpp
HDRS
remainderl.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)
19 changes: 19 additions & 0 deletions libc/src/math/remainder.cpp
@@ -0,0 +1,19 @@
//===-- Implementation of remainder 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/DivisionAndRemainderOperations.h"

namespace __llvm_libc {

double LLVM_LIBC_ENTRYPOINT(remainder)(double x, double y) {
int quotient;
return fputil::remquo(x, y, quotient);
}

} // namespace __llvm_libc
18 changes: 18 additions & 0 deletions libc/src/math/remainder.h
@@ -0,0 +1,18 @@
//===-- Implementation header for remainder ---------------------*- 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_REMAINDER_H
#define LLVM_LIBC_SRC_MATH_REMAINDER_H

namespace __llvm_libc {

double remainder(double x, double y);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_MATH_REMAINDER_H
19 changes: 19 additions & 0 deletions libc/src/math/remainderf.cpp
@@ -0,0 +1,19 @@
//===-- Implementation of remainderf 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/DivisionAndRemainderOperations.h"

namespace __llvm_libc {

float LLVM_LIBC_ENTRYPOINT(remainderf)(float x, float y) {
int quotient;
return fputil::remquo(x, y, quotient);
}

} // namespace __llvm_libc
18 changes: 18 additions & 0 deletions libc/src/math/remainderf.h
@@ -0,0 +1,18 @@
//===-- Implementation header for remainderf --------------------*- 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_REMAINDERF_H
#define LLVM_LIBC_SRC_MATH_REMAINDERF_H

namespace __llvm_libc {

float remainderf(float x, float y);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_MATH_REMAINDERF_H
19 changes: 19 additions & 0 deletions libc/src/math/remainderl.cpp
@@ -0,0 +1,19 @@
//===-- Implementation of remainderl 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/DivisionAndRemainderOperations.h"

namespace __llvm_libc {

long double LLVM_LIBC_ENTRYPOINT(remainderl)(long double x, long double y) {
int quotient;
return fputil::remquo(x, y, quotient);
}

} // namespace __llvm_libc
18 changes: 18 additions & 0 deletions libc/src/math/remainderl.h
@@ -0,0 +1,18 @@
//===-- Implementation header for remainderl --------------------*- 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_REMAINDERL_H
#define LLVM_LIBC_SRC_MATH_REMAINDERL_H

namespace __llvm_libc {

long double remainderl(long double x, long double y);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_MATH_REMAINDERL_H
18 changes: 18 additions & 0 deletions libc/src/math/remquo.cpp
@@ -0,0 +1,18 @@
//===-- Implementation of remquo 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/DivisionAndRemainderOperations.h"

namespace __llvm_libc {

double LLVM_LIBC_ENTRYPOINT(remquo)(double x, double y, int *exp) {
return fputil::remquo(x, y, *exp);
}

} // namespace __llvm_libc
18 changes: 18 additions & 0 deletions libc/src/math/remquo.h
@@ -0,0 +1,18 @@
//===-- Implementation header for remquo ------------------------*- 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_REMQUO_H
#define LLVM_LIBC_SRC_MATH_REMQUO_H

namespace __llvm_libc {

double remquo(double x, double y, int *exp);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_MATH_REMQUO_H
18 changes: 18 additions & 0 deletions libc/src/math/remquof.cpp
@@ -0,0 +1,18 @@
//===-- Implementation of remquof 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/DivisionAndRemainderOperations.h"

namespace __llvm_libc {

float LLVM_LIBC_ENTRYPOINT(remquof)(float x, float y, int *exp) {
return fputil::remquo(x, y, *exp);
}

} // namespace __llvm_libc
18 changes: 18 additions & 0 deletions libc/src/math/remquof.h
@@ -0,0 +1,18 @@
//===-- Implementation header for remquof -----------------------*- 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_REMQUOF_H
#define LLVM_LIBC_SRC_MATH_REMQUOF_H

namespace __llvm_libc {

float remquof(float x, float y, int *exp);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_MATH_REMQUOF_H
19 changes: 19 additions & 0 deletions libc/src/math/remquol.cpp
@@ -0,0 +1,19 @@
//===-- Implementation of remquol 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/DivisionAndRemainderOperations.h"

namespace __llvm_libc {

long double LLVM_LIBC_ENTRYPOINT(remquol)(long double x, long double y,
int *exp) {
return fputil::remquo(x, y, *exp);
}

} // namespace __llvm_libc
18 changes: 18 additions & 0 deletions libc/src/math/remquol.h
@@ -0,0 +1,18 @@
//===-- Implementation header for remquol -----------------------*- 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_REMQUOL_H
#define LLVM_LIBC_SRC_MATH_REMQUOL_H

namespace __llvm_libc {

long double remquol(long double x, long double y, int *exp);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_MATH_REMQUOL_H
39 changes: 39 additions & 0 deletions libc/test/src/math/CMakeLists.txt
Expand Up @@ -552,3 +552,42 @@ add_fp_unittest(
libc.src.math.sqrtl
libc.utils.FPUtil.fputil
)

add_fp_unittest(
remquof_test
NEED_MPFR
SUITE
libc_math_unittests
SRCS
remquof_test.cpp
DEPENDS
libc.include.math
libc.src.math.remquof
libc.utils.FPUtil.fputil
)

add_fp_unittest(
remquo_test
NEED_MPFR
SUITE
libc_math_unittests
SRCS
remquo_test.cpp
DEPENDS
libc.include.math
libc.src.math.remquo
libc.utils.FPUtil.fputil
)

add_fp_unittest(
remquol_test
NEED_MPFR
SUITE
libc_math_unittests
SRCS
remquol_test.cpp
DEPENDS
libc.include.math
libc.src.math.remquol
libc.utils.FPUtil.fputil
)

0 comments on commit 8514ecb

Please sign in to comment.