2024-01-29 15:47:55 -08:00
|
|
|
//===-- Unittests for stdbit ----------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
2025-01-14 10:53:41 -08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2024-01-29 15:47:55 -08:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "test/UnitTest/Test.h"
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The intent of this test is validate that:
|
2024-02-01 12:08:00 -08:00
|
|
|
* 1. We provide the definition of the various type generic macros of stdbit.h
|
|
|
|
|
* (the macros are transitively included from stdbit-macros.h by stdbit.h).
|
2024-01-29 15:47:55 -08:00
|
|
|
* 2. It dispatches to the correct underlying function.
|
|
|
|
|
* Because unit tests build without public packaging, the object files produced
|
|
|
|
|
* do not contain non-namespaced symbols.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-02-01 12:08:00 -08:00
|
|
|
/*
|
|
|
|
|
* Declare these BEFORE including stdbit-macros.h so that this test may still be
|
|
|
|
|
* run even if a given target doesn't yet have these individual entrypoints
|
|
|
|
|
* enabled.
|
|
|
|
|
*/
|
2024-03-11 15:39:05 -04:00
|
|
|
#include "stdbit_stub.h"
|
2024-02-01 12:08:00 -08:00
|
|
|
|
2024-02-29 14:43:53 -05:00
|
|
|
#include "include/llvm-libc-macros/stdbit-macros.h"
|
2024-01-29 15:47:55 -08:00
|
|
|
|
2024-02-01 10:14:22 -08:00
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingZeros) {
|
2024-02-01 13:09:33 -08:00
|
|
|
EXPECT_EQ(stdc_leading_zeros(static_cast<unsigned char>(0U)), 0xAAU);
|
|
|
|
|
EXPECT_EQ(stdc_leading_zeros(static_cast<unsigned short>(0U)), 0xABU);
|
|
|
|
|
EXPECT_EQ(stdc_leading_zeros(0U), 0xACU);
|
|
|
|
|
EXPECT_EQ(stdc_leading_zeros(0UL), 0xADU);
|
2024-03-04 12:35:57 -08:00
|
|
|
EXPECT_EQ(stdc_leading_zeros(0ULL), 0xAEU);
|
2024-01-29 15:47:55 -08:00
|
|
|
}
|
2024-02-01 10:14:22 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingOnes) {
|
2024-02-01 13:09:33 -08:00
|
|
|
EXPECT_EQ(stdc_leading_ones(static_cast<unsigned char>(0U)), 0xBAU);
|
|
|
|
|
EXPECT_EQ(stdc_leading_ones(static_cast<unsigned short>(0U)), 0xBBU);
|
|
|
|
|
EXPECT_EQ(stdc_leading_ones(0U), 0xBCU);
|
|
|
|
|
EXPECT_EQ(stdc_leading_ones(0UL), 0xBDU);
|
2024-03-04 12:35:57 -08:00
|
|
|
EXPECT_EQ(stdc_leading_ones(0ULL), 0xBEU);
|
2024-02-01 10:14:22 -08:00
|
|
|
}
|
2024-02-06 06:27:03 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingZeros) {
|
|
|
|
|
EXPECT_EQ(stdc_trailing_zeros(static_cast<unsigned char>(0U)), 0xCAU);
|
|
|
|
|
EXPECT_EQ(stdc_trailing_zeros(static_cast<unsigned short>(0U)), 0xCBU);
|
|
|
|
|
EXPECT_EQ(stdc_trailing_zeros(0U), 0xCCU);
|
|
|
|
|
EXPECT_EQ(stdc_trailing_zeros(0UL), 0xCDU);
|
2024-03-04 12:35:57 -08:00
|
|
|
EXPECT_EQ(stdc_trailing_zeros(0ULL), 0xCEU);
|
2024-02-06 06:27:03 -08:00
|
|
|
}
|
2024-02-07 02:56:24 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingOnes) {
|
|
|
|
|
EXPECT_EQ(stdc_trailing_ones(static_cast<unsigned char>(0U)), 0xDAU);
|
|
|
|
|
EXPECT_EQ(stdc_trailing_ones(static_cast<unsigned short>(0U)), 0xDBU);
|
|
|
|
|
EXPECT_EQ(stdc_trailing_ones(0U), 0xDCU);
|
|
|
|
|
EXPECT_EQ(stdc_trailing_ones(0UL), 0xDDU);
|
2024-03-04 12:35:57 -08:00
|
|
|
EXPECT_EQ(stdc_trailing_ones(0ULL), 0xDEU);
|
2024-02-07 02:56:24 -08:00
|
|
|
}
|
2024-02-12 08:31:53 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingZero) {
|
|
|
|
|
EXPECT_EQ(stdc_first_leading_zero(static_cast<unsigned char>(0U)), 0xEAU);
|
|
|
|
|
EXPECT_EQ(stdc_first_leading_zero(static_cast<unsigned short>(0U)), 0xEBU);
|
|
|
|
|
EXPECT_EQ(stdc_first_leading_zero(0U), 0xECU);
|
|
|
|
|
EXPECT_EQ(stdc_first_leading_zero(0UL), 0xEDU);
|
2024-03-04 12:35:57 -08:00
|
|
|
EXPECT_EQ(stdc_first_leading_zero(0ULL), 0xEEU);
|
2024-02-12 08:31:53 -08:00
|
|
|
}
|
2024-02-14 09:16:00 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingOne) {
|
|
|
|
|
EXPECT_EQ(stdc_first_leading_one(static_cast<unsigned char>(0U)), 0xFAU);
|
|
|
|
|
EXPECT_EQ(stdc_first_leading_one(static_cast<unsigned short>(0U)), 0xFBU);
|
|
|
|
|
EXPECT_EQ(stdc_first_leading_one(0U), 0xFCU);
|
|
|
|
|
EXPECT_EQ(stdc_first_leading_one(0UL), 0xFDU);
|
2024-03-04 12:35:57 -08:00
|
|
|
EXPECT_EQ(stdc_first_leading_one(0ULL), 0xFEU);
|
2024-02-14 09:16:00 -08:00
|
|
|
}
|
2024-02-14 10:34:28 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstTrailingZero) {
|
|
|
|
|
EXPECT_EQ(stdc_first_trailing_zero(static_cast<unsigned char>(0U)), 0x0AU);
|
|
|
|
|
EXPECT_EQ(stdc_first_trailing_zero(static_cast<unsigned short>(0U)), 0x0BU);
|
|
|
|
|
EXPECT_EQ(stdc_first_trailing_zero(0U), 0x0CU);
|
|
|
|
|
EXPECT_EQ(stdc_first_trailing_zero(0UL), 0x0DU);
|
2024-03-04 12:35:57 -08:00
|
|
|
EXPECT_EQ(stdc_first_trailing_zero(0ULL), 0x0EU);
|
2024-02-14 10:34:28 -08:00
|
|
|
}
|
2024-02-14 11:10:48 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstTrailingOne) {
|
|
|
|
|
EXPECT_EQ(stdc_first_trailing_one(static_cast<unsigned char>(0U)), 0x1AU);
|
|
|
|
|
EXPECT_EQ(stdc_first_trailing_one(static_cast<unsigned short>(0U)), 0x1BU);
|
|
|
|
|
EXPECT_EQ(stdc_first_trailing_one(0U), 0x1CU);
|
|
|
|
|
EXPECT_EQ(stdc_first_trailing_one(0UL), 0x1DU);
|
2024-03-04 12:35:57 -08:00
|
|
|
EXPECT_EQ(stdc_first_trailing_one(0ULL), 0x1EU);
|
2024-02-14 11:10:48 -08:00
|
|
|
}
|
2024-02-26 09:23:15 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroCountZeros) {
|
|
|
|
|
EXPECT_EQ(stdc_count_zeros(static_cast<unsigned char>(0U)), 0x2AU);
|
|
|
|
|
EXPECT_EQ(stdc_count_zeros(static_cast<unsigned short>(0U)), 0x2BU);
|
|
|
|
|
EXPECT_EQ(stdc_count_zeros(0U), 0x2CU);
|
|
|
|
|
EXPECT_EQ(stdc_count_zeros(0UL), 0x2DU);
|
2024-03-04 12:35:57 -08:00
|
|
|
EXPECT_EQ(stdc_count_zeros(0ULL), 0x2EU);
|
2024-02-26 09:23:15 -08:00
|
|
|
}
|
2024-02-26 09:25:24 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroCountOnes) {
|
|
|
|
|
EXPECT_EQ(stdc_count_ones(static_cast<unsigned char>(0U)), 0x3AU);
|
|
|
|
|
EXPECT_EQ(stdc_count_ones(static_cast<unsigned short>(0U)), 0x3BU);
|
|
|
|
|
EXPECT_EQ(stdc_count_ones(0U), 0x3CU);
|
|
|
|
|
EXPECT_EQ(stdc_count_ones(0UL), 0x3DU);
|
2024-03-04 12:35:57 -08:00
|
|
|
EXPECT_EQ(stdc_count_ones(0ULL), 0x3EU);
|
2024-02-26 09:25:24 -08:00
|
|
|
}
|
2024-02-27 13:45:37 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroHasSingleBit) {
|
|
|
|
|
EXPECT_EQ(stdc_has_single_bit(static_cast<unsigned char>(1U)), false);
|
|
|
|
|
EXPECT_EQ(stdc_has_single_bit(static_cast<unsigned short>(1U)), false);
|
|
|
|
|
EXPECT_EQ(stdc_has_single_bit(1U), false);
|
|
|
|
|
EXPECT_EQ(stdc_has_single_bit(1UL), false);
|
|
|
|
|
EXPECT_EQ(stdc_has_single_bit(1ULL), false);
|
|
|
|
|
}
|
2024-03-05 09:49:41 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroBitWidth) {
|
|
|
|
|
EXPECT_EQ(stdc_bit_width(static_cast<unsigned char>(1U)), 0x4AU);
|
|
|
|
|
EXPECT_EQ(stdc_bit_width(static_cast<unsigned short>(1U)), 0x4BU);
|
|
|
|
|
EXPECT_EQ(stdc_bit_width(1U), 0x4CU);
|
|
|
|
|
EXPECT_EQ(stdc_bit_width(1UL), 0x4DU);
|
|
|
|
|
EXPECT_EQ(stdc_bit_width(1ULL), 0x4EU);
|
|
|
|
|
}
|
2024-03-07 08:38:04 -08:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroBitFloor) {
|
|
|
|
|
EXPECT_EQ(stdc_bit_floor(static_cast<unsigned char>(0U)),
|
|
|
|
|
static_cast<unsigned char>(0x5AU));
|
|
|
|
|
EXPECT_EQ(stdc_bit_floor(static_cast<unsigned short>(0U)),
|
|
|
|
|
static_cast<unsigned short>(0x5BU));
|
|
|
|
|
EXPECT_EQ(stdc_bit_floor(0U), 0x5CU);
|
|
|
|
|
EXPECT_EQ(stdc_bit_floor(0UL), 0x5DUL);
|
|
|
|
|
EXPECT_EQ(stdc_bit_floor(0ULL), 0x5EULL);
|
|
|
|
|
}
|
2024-03-10 09:53:28 -07:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, TypeGenericMacroBitCeil) {
|
|
|
|
|
EXPECT_EQ(stdc_bit_ceil(static_cast<unsigned char>(0U)),
|
|
|
|
|
static_cast<unsigned char>(0x6AU));
|
|
|
|
|
EXPECT_EQ(stdc_bit_ceil(static_cast<unsigned short>(0U)),
|
|
|
|
|
static_cast<unsigned short>(0x6BU));
|
|
|
|
|
EXPECT_EQ(stdc_bit_ceil(0U), 0x6CU);
|
|
|
|
|
EXPECT_EQ(stdc_bit_ceil(0UL), 0x6DUL);
|
|
|
|
|
EXPECT_EQ(stdc_bit_ceil(0ULL), 0x6EULL);
|
|
|
|
|
}
|
2024-03-12 08:39:17 -07:00
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, VersionMacro) {
|
|
|
|
|
// 7.18.1p2 an integer constant expression with a value equivalent to 202311L.
|
|
|
|
|
EXPECT_EQ(__STDC_VERSION_STDBIT_H__, 202311L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(LlvmLibcStdbitTest, EndianMacros) {
|
|
|
|
|
// 7.18.2p3 The values of the integer constant expressions for
|
|
|
|
|
// __STDC_ENDIAN_LITTLE__ and __STDC_ENDIAN_BIG__ are not equal.
|
|
|
|
|
EXPECT_NE(__STDC_ENDIAN_LITTLE__, __STDC_ENDIAN_BIG__);
|
|
|
|
|
// The standard does allow for __STDC_ENDIAN_NATIVE__ to be an integer
|
|
|
|
|
// constant expression with an implementation defined value for non-big or
|
|
|
|
|
// little endianness environments. I assert such machines are no longer
|
|
|
|
|
// relevant.
|
|
|
|
|
EXPECT_TRUE(__STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_LITTLE__ ||
|
|
|
|
|
__STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_BIG__);
|
|
|
|
|
}
|