21 changes: 21 additions & 0 deletions libc/src/stdlib/lldiv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of lldiv -------------------------------------------===//
//
// 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/stdlib/lldiv.h"
#include "src/__support/common.h"
#include "src/__support/integer_operations.h"

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(lldiv_t, lldiv, (long long x, long long y)) {
lldiv_t res;
integerRemQuo(x, y, res.quot, res.rem);
return res;
}

} // namespace __llvm_libc
20 changes: 20 additions & 0 deletions libc/src/stdlib/lldiv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for lldiv -------------------------*- 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 <stdlib.h>

#ifndef LLVM_LIBC_SRC_STDLIB_LLDIV_H
#define LLVM_LIBC_SRC_STDLIB_LLDIV_H

namespace __llvm_libc {

lldiv_t lldiv(long long x, long long y);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_STDLIB_LDIV_H
13 changes: 13 additions & 0 deletions libc/test/src/inttypes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ add_libc_unittest(
DEPENDS
libc.src.inttypes.strtoumax
)

add_libc_unittest(
imaxdiv_test
SUITE
libc_inttypes_unittests
SRCS
imaxdiv_test.cpp
HDRS
../stdlib/DivTest.h
DEPENDS
libc.include.stdlib
libc.src.inttypes.imaxdiv
)
15 changes: 15 additions & 0 deletions libc/test/src/inttypes/imaxdiv_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//===-- Unittests for imaxdiv ---------------------------------------------===//
//
// 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 "../stdlib/DivTest.h"

#include "src/inttypes/imaxdiv.h"

#include <inttypes.h>

LIST_DIV_TESTS(intmax_t, imaxdiv_t, __llvm_libc::imaxdiv)
39 changes: 39 additions & 0 deletions libc/test/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,42 @@ add_libc_unittest(
DEPENDS
libc.src.stdlib.llabs
)

add_libc_unittest(
div_test
SUITE
libc_stdlib_unittests
SRCS
div_test.cpp
HDRS
DivTest.h
DEPENDS
libc.include.stdlib
libc.src.stdlib.div
)

add_libc_unittest(
ldiv_test
SUITE
libc_stdlib_unittests
SRCS
ldiv_test.cpp
HDRS
DivTest.h
DEPENDS
libc.include.stdlib
libc.src.stdlib.ldiv
)

add_libc_unittest(
lldiv_test
SUITE
libc_stdlib_unittests
SRCS
lldiv_test.cpp
HDRS
DivTest.h
DEPENDS
libc.include.stdlib
libc.src.stdlib.lldiv
)
37 changes: 37 additions & 0 deletions libc/test/src/stdlib/DivTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===-- A template class for testing div functions --------------*- 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 "utils/UnitTest/Test.h"

template <typename IntType, typename ReturnType>
class DivTest : public __llvm_libc::testing::Test {
public:
using DivFunc = ReturnType(IntType, IntType);

void simpleTest(DivFunc func) {
auto result = func(10, 3);
EXPECT_EQ(result.quot, IntType(3));
EXPECT_EQ(result.rem, IntType(1));

result = func(-10, 3);
EXPECT_EQ(result.quot, IntType(-3));
EXPECT_EQ(result.rem, IntType(-1));

result = func(-10, -3);
EXPECT_EQ(result.quot, IntType(3));
EXPECT_EQ(result.rem, IntType(-1));

result = func(10, -3);
EXPECT_EQ(result.quot, IntType(-3));
EXPECT_EQ(result.rem, IntType(1));
}
};

#define LIST_DIV_TESTS(IntType, ReturnType, func) \
using LlvmLibcDivTest = DivTest<IntType, ReturnType>; \
TEST_F(LlvmLibcDivTest, SimpleTest) { simpleTest(func); }
15 changes: 15 additions & 0 deletions libc/test/src/stdlib/div_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//===-- Unittests for div -------------------------------------------------===//
//
// 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 "DivTest.h"

#include "src/stdlib/div.h"

#include <stdlib.h>

LIST_DIV_TESTS(int, div_t, __llvm_libc::div)
15 changes: 15 additions & 0 deletions libc/test/src/stdlib/ldiv_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//===-- Unittests for ldiv ------------------------------------------------===//
//
// 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 "DivTest.h"

#include "src/stdlib/ldiv.h"

#include <stdlib.h>

LIST_DIV_TESTS(long, ldiv_t, __llvm_libc::ldiv)
15 changes: 15 additions & 0 deletions libc/test/src/stdlib/lldiv_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//===-- Unittests for lldiv -----------------------------------------------===//
//
// 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 "DivTest.h"

#include "src/stdlib/lldiv.h"

#include <stdlib.h>

LIST_DIV_TESTS(long long, lldiv_t, __llvm_libc::lldiv)