Skip to content

Commit 7aeb380

Browse files
author
Siva Chandra Reddy
committed
[libc] Add implementations of lround[f|l] and llround[f|l].
A new function to MPFRWrapper has been added, which is used to set up the unit tests. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D93007
1 parent a593d22 commit 7aeb380

28 files changed

+805
-2
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,15 @@ set(TARGET_LIBM_ENTRYPOINTS
122122
libc.src.math.ldexp
123123
libc.src.math.ldexpf
124124
libc.src.math.ldexpl
125+
libc.src.math.llround
126+
libc.src.math.llroundf
127+
libc.src.math.llroundl
125128
libc.src.math.logb
126129
libc.src.math.logbf
127130
libc.src.math.logbl
131+
libc.src.math.lround
132+
libc.src.math.lroundf
133+
libc.src.math.lroundl
128134
libc.src.math.modf
129135
libc.src.math.modff
130136
libc.src.math.modfl

libc/spec/spec.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ class RestrictedPtrType<Type type> : Type {
3939
def VarArgType : Type {}
4040
def VoidType : NamedType<"void">;
4141
def IntType : NamedType<"int">;
42+
def LongType : NamedType<"long">;
43+
def LongLongType : NamedType<"long long">;
4244
def FloatType : NamedType<"float">;
4345
def DoubleType : NamedType<"double">;
4446
def LongDoubleType : NamedType<"long double">;
45-
def LongLongType : NamedType<"long long">;
46-
def LongType : NamedType<"long">;
4747
def CharType : NamedType<"char">;
4848

4949
// Common types

libc/spec/stdc.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ def StdC : StandardSpec<"stdc"> {
363363
FunctionSpec<"roundf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
364364
FunctionSpec<"roundl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
365365

366+
FunctionSpec<"lround", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
367+
FunctionSpec<"lroundf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
368+
FunctionSpec<"lroundl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
369+
370+
FunctionSpec<"llround", RetValSpec<LongLongType>, [ArgSpec<DoubleType>]>,
371+
FunctionSpec<"llroundf", RetValSpec<LongLongType>, [ArgSpec<FloatType>]>,
372+
FunctionSpec<"llroundl", RetValSpec<LongLongType>, [ArgSpec<LongDoubleType>]>,
373+
366374
FunctionSpec<"sqrt", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
367375
FunctionSpec<"sqrtf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
368376
FunctionSpec<"sqrtl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,78 @@ add_entrypoint_object(
236236
-O2
237237
)
238238

239+
add_entrypoint_object(
240+
lround
241+
SRCS
242+
lround.cpp
243+
HDRS
244+
lround.h
245+
DEPENDS
246+
libc.utils.FPUtil.fputil
247+
COMPILE_OPTIONS
248+
-O2
249+
)
250+
251+
add_entrypoint_object(
252+
lroundf
253+
SRCS
254+
lroundf.cpp
255+
HDRS
256+
lroundf.h
257+
DEPENDS
258+
libc.utils.FPUtil.fputil
259+
COMPILE_OPTIONS
260+
-O2
261+
)
262+
263+
add_entrypoint_object(
264+
lroundl
265+
SRCS
266+
lroundl.cpp
267+
HDRS
268+
lroundl.h
269+
DEPENDS
270+
libc.utils.FPUtil.fputil
271+
COMPILE_OPTIONS
272+
-O2
273+
)
274+
275+
add_entrypoint_object(
276+
llround
277+
SRCS
278+
llround.cpp
279+
HDRS
280+
llround.h
281+
DEPENDS
282+
libc.utils.FPUtil.fputil
283+
COMPILE_OPTIONS
284+
-O2
285+
)
286+
287+
add_entrypoint_object(
288+
llroundf
289+
SRCS
290+
llroundf.cpp
291+
HDRS
292+
llroundf.h
293+
DEPENDS
294+
libc.utils.FPUtil.fputil
295+
COMPILE_OPTIONS
296+
-O2
297+
)
298+
299+
add_entrypoint_object(
300+
llroundl
301+
SRCS
302+
llroundl.cpp
303+
HDRS
304+
llroundl.h
305+
DEPENDS
306+
libc.utils.FPUtil.fputil
307+
COMPILE_OPTIONS
308+
-O2
309+
)
310+
239311
add_object_library(
240312
exp_utils
241313
HDRS

libc/src/math/llround.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of llround function --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/common.h"
10+
#include "utils/FPUtil/NearestIntegerOperations.h"
11+
12+
namespace __llvm_libc {
13+
14+
long long LLVM_LIBC_ENTRYPOINT(llround)(double x) {
15+
return fputil::roundToSignedInteger<double, long long>(x);
16+
}
17+
18+
} // namespace __llvm_libc

libc/src/math/llround.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for llround -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_LLROUND_H
10+
#define LLVM_LIBC_SRC_MATH_LLROUND_H
11+
12+
namespace __llvm_libc {
13+
14+
long long llround(double x);
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_MATH_LLROUND_H

libc/src/math/llroundf.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of llroundf function -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/common.h"
10+
#include "utils/FPUtil/NearestIntegerOperations.h"
11+
12+
namespace __llvm_libc {
13+
14+
long long LLVM_LIBC_ENTRYPOINT(llroundf)(float x) {
15+
return fputil::roundToSignedInteger<float, long long>(x);
16+
}
17+
18+
} // namespace __llvm_libc

libc/src/math/llroundf.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for llroundf ----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_LLROUNDF_H
10+
#define LLVM_LIBC_SRC_MATH_LLROUNDF_H
11+
12+
namespace __llvm_libc {
13+
14+
long long llroundf(float x);
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_MATH_LLROUNDF_H

libc/src/math/llroundl.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of llroundl function -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/common.h"
10+
#include "utils/FPUtil/NearestIntegerOperations.h"
11+
12+
namespace __llvm_libc {
13+
14+
long long LLVM_LIBC_ENTRYPOINT(llroundl)(long double x) {
15+
return fputil::roundToSignedInteger<long double, long long>(x);
16+
}
17+
18+
} // namespace __llvm_libc

libc/src/math/llroundl.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for llroundl ----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_LLROUNDL_H
10+
#define LLVM_LIBC_SRC_MATH_LLROUNDL_H
11+
12+
namespace __llvm_libc {
13+
14+
long long llroundl(long double x);
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_MATH_LLROUNDL_H

libc/src/math/lround.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of lround function ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/common.h"
10+
#include "utils/FPUtil/NearestIntegerOperations.h"
11+
12+
namespace __llvm_libc {
13+
14+
long LLVM_LIBC_ENTRYPOINT(lround)(double x) {
15+
return fputil::roundToSignedInteger<double, long>(x);
16+
}
17+
18+
} // namespace __llvm_libc

libc/src/math/lround.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for lround ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_LROUND_H
10+
#define LLVM_LIBC_SRC_MATH_LROUND_H
11+
12+
namespace __llvm_libc {
13+
14+
long lround(double x);
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_MATH_LROUND_H

libc/src/math/lroundf.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of lroundf function --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/common.h"
10+
#include "utils/FPUtil/NearestIntegerOperations.h"
11+
12+
namespace __llvm_libc {
13+
14+
long LLVM_LIBC_ENTRYPOINT(lroundf)(float x) {
15+
return fputil::roundToSignedInteger<float, long>(x);
16+
}
17+
18+
} // namespace __llvm_libc

libc/src/math/lroundf.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for lroundf -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_LROUNDF_H
10+
#define LLVM_LIBC_SRC_MATH_LROUNDF_H
11+
12+
namespace __llvm_libc {
13+
14+
long lroundf(float x);
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_MATH_LROUNDF_H

libc/src/math/lroundl.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of lroundl function --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/common.h"
10+
#include "utils/FPUtil/NearestIntegerOperations.h"
11+
12+
namespace __llvm_libc {
13+
14+
long LLVM_LIBC_ENTRYPOINT(lroundl)(long double x) {
15+
return fputil::roundToSignedInteger<long double, long>(x);
16+
}
17+
18+
} // namespace __llvm_libc

libc/src/math/lroundl.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for lroundl -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_LROUNDL_H
10+
#define LLVM_LIBC_SRC_MATH_LROUNDL_H
11+
12+
namespace __llvm_libc {
13+
14+
long lroundl(long double x);
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_MATH_LROUNDL_H

0 commit comments

Comments
 (0)