diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt --- a/compiler-rt/lib/builtins/CMakeLists.txt +++ b/compiler-rt/lib/builtins/CMakeLists.txt @@ -52,8 +52,10 @@ addvti3.c apple_versioning.c ashldi3.c + ashlsi3.c ashlti3.c ashrdi3.c + ashrsi3.c ashrti3.c bswapdi2.c bswapsi2.c @@ -110,6 +112,7 @@ fp_mode.c int_util.c lshrdi3.c + lshrsi3.c lshrti3.c moddi3.c modsi3.c diff --git a/compiler-rt/lib/builtins/ashldi3.c b/compiler-rt/lib/builtins/ashldi3.c --- a/compiler-rt/lib/builtins/ashldi3.c +++ b/compiler-rt/lib/builtins/ashldi3.c @@ -12,25 +12,16 @@ #include "int_lib.h" +typedef di_int ashl_int_t; +typedef dwords ashl_words_t; +#include "int_ashl_impl.inc" + // Returns: a << b // Precondition: 0 <= b < bits_in_dword COMPILER_RT_ABI di_int __ashldi3(di_int a, int b) { - const int bits_in_word = (int)(sizeof(si_int) * CHAR_BIT); - dwords input; - dwords result; - input.all = a; - if (b & bits_in_word) /* bits_in_word <= b < bits_in_dword */ { - result.s.low = 0; - result.s.high = input.s.low << (b - bits_in_word); - } else /* 0 <= b < bits_in_word */ { - if (b == 0) - return a; - result.s.low = input.s.low << b; - result.s.high = (input.s.high << b) | (input.s.low >> (bits_in_word - b)); - } - return result.all; + return __ashlXi3(a, b); } #if defined(__ARM_EABI__) diff --git a/compiler-rt/lib/builtins/ashlsi3.c b/compiler-rt/lib/builtins/ashlsi3.c new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/builtins/ashlsi3.c @@ -0,0 +1,25 @@ +// ====-- ashlsi3.c - Implement __ashlsi3 ---------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file implements __ashlsi3 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" + +typedef si_int ashl_int_t; +typedef swords ashl_words_t; +#include "int_ashl_impl.inc" + +// Returns: a << b + +// Preconsition: 0 <= b < bits_in_sword + +COMPILER_RT_ABI si_int __ashlsi3(si_int a, int b) { + return __ashlXi3(a, b); +} diff --git a/compiler-rt/lib/builtins/ashlti3.c b/compiler-rt/lib/builtins/ashlti3.c --- a/compiler-rt/lib/builtins/ashlti3.c +++ b/compiler-rt/lib/builtins/ashlti3.c @@ -14,25 +14,16 @@ #ifdef CRT_HAS_128BIT +typedef ti_int ashl_int_t; +typedef twords ashl_words_t; +#include "int_ashl_impl.inc" + // Returns: a << b // Precondition: 0 <= b < bits_in_tword COMPILER_RT_ABI ti_int __ashlti3(ti_int a, si_int b) { - const int bits_in_dword = (int)(sizeof(di_int) * CHAR_BIT); - twords input; - twords result; - input.all = a; - if (b & bits_in_dword) /* bits_in_dword <= b < bits_in_tword */ { - result.s.low = 0; - result.s.high = input.s.low << (b - bits_in_dword); - } else /* 0 <= b < bits_in_dword */ { - if (b == 0) - return a; - result.s.low = input.s.low << b; - result.s.high = (input.s.high << b) | (input.s.low >> (bits_in_dword - b)); - } - return result.all; + return __ashlXi3(a, b); } #endif // CRT_HAS_128BIT diff --git a/compiler-rt/lib/builtins/ashrdi3.c b/compiler-rt/lib/builtins/ashrdi3.c --- a/compiler-rt/lib/builtins/ashrdi3.c +++ b/compiler-rt/lib/builtins/ashrdi3.c @@ -12,26 +12,16 @@ #include "int_lib.h" +typedef di_int ashr_int_t; +typedef dwords ashr_words_t; +#include "int_ashr_impl.inc" + // Returns: arithmetic a >> b // Precondition: 0 <= b < bits_in_dword COMPILER_RT_ABI di_int __ashrdi3(di_int a, int b) { - const int bits_in_word = (int)(sizeof(si_int) * CHAR_BIT); - dwords input; - dwords result; - input.all = a; - if (b & bits_in_word) /* bits_in_word <= b < bits_in_dword */ { - // result.s.high = input.s.high < 0 ? -1 : 0 - result.s.high = input.s.high >> (bits_in_word - 1); - result.s.low = input.s.high >> (b - bits_in_word); - } else /* 0 <= b < bits_in_word */ { - if (b == 0) - return a; - result.s.high = input.s.high >> b; - result.s.low = (input.s.high << (bits_in_word - b)) | (input.s.low >> b); - } - return result.all; + return __ashrXi3(a, b); } #if defined(__ARM_EABI__) diff --git a/compiler-rt/lib/builtins/ashrsi3.c b/compiler-rt/lib/builtins/ashrsi3.c new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/builtins/ashrsi3.c @@ -0,0 +1,25 @@ +//===-- ashrsi3.c - Implement __ashrsi3 -----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file implements __ashrsi3 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" + +typedef si_int ashr_int_t; +typedef swords ashr_words_t; +#include "int_ashr_impl.inc" + +// Returns: arithmetic a >> b + +// Precondition: 0 <= b < bits_in_dword + +COMPILER_RT_ABI si_int __ashrsi3(si_int a, int b) { + return __ashrXi3(a, b); +} diff --git a/compiler-rt/lib/builtins/ashrti3.c b/compiler-rt/lib/builtins/ashrti3.c --- a/compiler-rt/lib/builtins/ashrti3.c +++ b/compiler-rt/lib/builtins/ashrti3.c @@ -14,26 +14,16 @@ #ifdef CRT_HAS_128BIT +typedef ti_int ashr_int_t; +typedef twords ashr_words_t; +#include "int_ashr_impl.inc" + // Returns: arithmetic a >> b // Precondition: 0 <= b < bits_in_tword COMPILER_RT_ABI ti_int __ashrti3(ti_int a, si_int b) { - const int bits_in_dword = (int)(sizeof(di_int) * CHAR_BIT); - twords input; - twords result; - input.all = a; - if (b & bits_in_dword) /* bits_in_dword <= b < bits_in_tword */ { - // result.s.high = input.s.high < 0 ? -1 : 0 - result.s.high = input.s.high >> (bits_in_dword - 1); - result.s.low = input.s.high >> (b - bits_in_dword); - } else /* 0 <= b < bits_in_dword */ { - if (b == 0) - return a; - result.s.high = input.s.high >> b; - result.s.low = (input.s.high << (bits_in_dword - b)) | (input.s.low >> b); - } - return result.all; + return __ashrXi3(a, b); } #endif // CRT_HAS_128BIT diff --git a/compiler-rt/lib/builtins/int_ashl_impl.inc b/compiler-rt/lib/builtins/int_ashl_impl.inc new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/builtins/int_ashl_impl.inc @@ -0,0 +1,30 @@ +//===-- int_ashl_impl.inc - Integer bitshifts -----------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Helpers used by __ashlsi3, __ashldi3, and __ashlti3. +// +//===----------------------------------------------------------------------===// + +static __inline ashl_int_t __ashlXi3(ashl_int_t a, int b) { + const int bits_in_word = (int)(sizeof(ashl_int_t) * CHAR_BIT) / 2; + ashl_words_t input; + ashl_words_t result; + input.all = a; + if (b & bits_in_word) { + // bits_in_word <= b < bits_in_sword + result.s.low = 0; + result.s.high = input.s.low << (b - bits_in_word); + } else { + // 0 <= b < bits_in_word + if (b == 0) + return a; + result.s.low = input.s.low << b; + result.s.high = (input.s.high << b) | (input.s.low >> (bits_in_word - b)); + } + return result.all; +} diff --git a/compiler-rt/lib/builtins/int_ashr_impl.inc b/compiler-rt/lib/builtins/int_ashr_impl.inc new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/builtins/int_ashr_impl.inc @@ -0,0 +1,29 @@ +//===-- int_ashr_impl.inc - Integer bitshifts -----------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Helpers used by __ashrsi3, __ashrdi3, and __ashrti3. +// +//===----------------------------------------------------------------------===// + +static __inline ashr_int_t __ashrXi3(ashr_int_t a, int b) { + const int bits_in_word = (int)(sizeof(ashr_int_t) * CHAR_BIT) / 2; + ashr_words_t input; + ashr_words_t result; + input.all = a; + if (b & bits_in_word) /* bits_in_word <= b < bits_in_sword */ { + // result.s.high = input.s.high < 0 ? -1 : 0 + result.s.high = input.s.high >> (bits_in_word - 1); + result.s.low = input.s.high >> (b - bits_in_word); + } else /* 0 <= b < bits_in_word */ { + if (b == 0) + return a; + result.s.high = input.s.high >> b; + result.s.low = (input.s.high << (bits_in_word - b)) | (input.s.low >> b); + } + return result.all; +} diff --git a/compiler-rt/lib/builtins/int_lshr_impl.inc b/compiler-rt/lib/builtins/int_lshr_impl.inc new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/builtins/int_lshr_impl.inc @@ -0,0 +1,30 @@ +//===-- int_lshr_impl.inc - Integer bitshifts -----------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Helpers used by __lshrsi3, __lshrdi3, and __lshrti3. +// +//===----------------------------------------------------------------------===// + +static __inline lshr_int_t __lshrXi3(lshr_int_t a, int b) { + const int bits_in_word = (int)(sizeof(lshr_int_t) * CHAR_BIT) / 2; + lshr_uwords_t input; + lshr_uwords_t result; + input.all = a; + if (b & bits_in_word) { + // bits_in_word <= b < bits_in_sword + result.s.high = 0; + result.s.low = input.s.high >> (b - bits_in_word); + } else { + // 0 <= b < bits_in_word + if (b == 0) + return a; + result.s.high = input.s.high >> b; + result.s.low = (input.s.high << (bits_in_word - b)) | (input.s.low >> b); + } + return result.all; +} diff --git a/compiler-rt/lib/builtins/int_types.h b/compiler-rt/lib/builtins/int_types.h --- a/compiler-rt/lib/builtins/int_types.h +++ b/compiler-rt/lib/builtins/int_types.h @@ -18,6 +18,9 @@ #include "int_endianness.h" +typedef int16_t hi_int; +typedef uint16_t hu_int; + // si_int is defined in Linux sysroot's asm-generic/siginfo.h #ifdef si_int #undef si_int @@ -37,6 +40,32 @@ typedef int64_t di_int; typedef uint64_t du_int; +typedef union { + si_int all; + struct { +#if _YUGA_LITTLE_ENDIAN + hu_int low; + hi_int high; +#else + hi_int high; + hu_int low; +#endif // _YUGA_LITTLE_ENDIAN + } s; +} swords; + +typedef union { + su_int all; + struct { +#if _YUGA_LITTLE_ENDIAN + hu_int low; + hu_int high; +#else + hu_int high; + hu_int low; +#endif // _YUGA_LITTLE_ENDIAN + } s; +} uswords; + typedef union { di_int all; struct { diff --git a/compiler-rt/lib/builtins/lshrdi3.c b/compiler-rt/lib/builtins/lshrdi3.c --- a/compiler-rt/lib/builtins/lshrdi3.c +++ b/compiler-rt/lib/builtins/lshrdi3.c @@ -12,25 +12,16 @@ #include "int_lib.h" +typedef di_int lshr_int_t; +typedef udwords lshr_uwords_t; +#include "int_lshr_impl.inc" + // Returns: logical a >> b // Precondition: 0 <= b < bits_in_dword COMPILER_RT_ABI di_int __lshrdi3(di_int a, int b) { - const int bits_in_word = (int)(sizeof(si_int) * CHAR_BIT); - udwords input; - udwords result; - input.all = a; - if (b & bits_in_word) /* bits_in_word <= b < bits_in_dword */ { - result.s.high = 0; - result.s.low = input.s.high >> (b - bits_in_word); - } else /* 0 <= b < bits_in_word */ { - if (b == 0) - return a; - result.s.high = input.s.high >> b; - result.s.low = (input.s.high << (bits_in_word - b)) | (input.s.low >> b); - } - return result.all; + return __lshrXi3(a, b); } #if defined(__ARM_EABI__) diff --git a/compiler-rt/lib/builtins/lshrsi3.c b/compiler-rt/lib/builtins/lshrsi3.c new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/builtins/lshrsi3.c @@ -0,0 +1,25 @@ +//===-- lshrsi3.c - Implement __lshrsi3 -----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file implements __lshrsi3 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" + +typedef si_int lshr_int_t; +typedef uswords lshr_uwords_t; +#include "int_lshr_impl.inc" + +// Returns: logical a >> b + +// Precondition: 0 <= b < bits_in_sword + +COMPILER_RT_ABI si_int __lshrsi3(si_int a, int b) { + return __lshrXi3(a, b); +} diff --git a/compiler-rt/lib/builtins/lshrti3.c b/compiler-rt/lib/builtins/lshrti3.c --- a/compiler-rt/lib/builtins/lshrti3.c +++ b/compiler-rt/lib/builtins/lshrti3.c @@ -14,25 +14,16 @@ #ifdef CRT_HAS_128BIT +typedef ti_int lshr_int_t; +typedef utwords lshr_uwords_t; +#include "int_lshr_impl.inc" + // Returns: logical a >> b // Precondition: 0 <= b < bits_in_tword COMPILER_RT_ABI ti_int __lshrti3(ti_int a, si_int b) { - const int bits_in_dword = (int)(sizeof(di_int) * CHAR_BIT); - utwords input; - utwords result; - input.all = a; - if (b & bits_in_dword) /* bits_in_dword <= b < bits_in_tword */ { - result.s.high = 0; - result.s.low = input.s.high >> (b - bits_in_dword); - } else /* 0 <= b < bits_in_dword */ { - if (b == 0) - return a; - result.s.high = input.s.high >> b; - result.s.low = (input.s.high << (bits_in_dword - b)) | (input.s.low >> b); - } - return result.all; + return __lshrXi3(a, b); } #endif // CRT_HAS_128BIT diff --git a/compiler-rt/test/builtins/Unit/ashlsi3_test.c b/compiler-rt/test/builtins/Unit/ashlsi3_test.c new file mode 100644 --- /dev/null +++ b/compiler-rt/test/builtins/Unit/ashlsi3_test.c @@ -0,0 +1,74 @@ +// RUN: %clang_builtins %s %librt -o %t && %run %t +// REQUIRES: librt_has_ashlsi3 +//===-- ashlsi3_test.c - Test __ashlsi3 -----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file tests __ashlsi3 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" +#include + +// Returns: a << b + +// Precondition: 0 <= b < bits_in_dword + +COMPILER_RT_ABI si_int __ashlsi3(si_int a, int b); + +int test__ashlsi3(si_int a, int b, si_int expected) { + si_int x = __ashlsi3(a, b); + if (x != expected) + printf("error in __ashlsi3: %X << %d = %X, expected %X\n", + a, b, __ashlsi3(a, b), expected); + return x != expected; +} + +int main() { + if (test__ashlsi3(0x01234567, 0, 0x1234567)) + return 1; + if (test__ashlsi3(0x01234567, 1, 0x2468ACE)) + return 1; + if (test__ashlsi3(0x01234567, 2, 0x048D159C)) + return 1; + if (test__ashlsi3(0x01234567, 3, 0x091A2B38)) + return 1; + if (test__ashlsi3(0x01234567, 4, 0x12345670)) + return 1; + + if (test__ashlsi3(0x01234567, 12, 0x34567000)) + return 1; + if (test__ashlsi3(0x01234567, 13, 0x68ACE000)) + return 1; + if (test__ashlsi3(0x01234567, 14, 0xD159C000)) + return 1; + if (test__ashlsi3(0x01234567, 15, 0xA2B38000)) + return 1; + + if (test__ashlsi3(0x01234567, 16, 0x45670000)) + return 1; + + if (test__ashlsi3(0x01234567, 17, 0x8ACE0000)) + return 1; + if (test__ashlsi3(0x01234567, 18, 0x159C0000)) + return 1; + if (test__ashlsi3(0x01234567, 19, 0x2B380000)) + return 1; + if (test__ashlsi3(0x01234567, 20, 0x56700000)) + return 1; + + if (test__ashlsi3(0x01234567, 28, 0x70000000)) + return 1; + if (test__ashlsi3(0x01234567, 29, 0xE0000000)) + return 1; + if (test__ashlsi3(0x01234567, 30, 0xC0000000)) + return 1; + if (test__ashlsi3(0x01234567, 31, 0x80000000)) + return 1; + return 0; +} diff --git a/compiler-rt/test/builtins/Unit/ashrsi3_test.c b/compiler-rt/test/builtins/Unit/ashrsi3_test.c new file mode 100644 --- /dev/null +++ b/compiler-rt/test/builtins/Unit/ashrsi3_test.c @@ -0,0 +1,115 @@ +// RUN: %clang_builtins %s %librt -o %t && %run %t +// REQUIRES: librt_has_ashrsi3 +//===-- ashrsi3_test.c - Test __ashrsi3 -----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file tests __ashrsi3 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" +#include + +// Returns: arithmetic a >> b + +// Precondition: 0 <= b < bits_in_dword + +COMPILER_RT_ABI si_int __ashrsi3(si_int a, int b); + +int test__ashrsi3(si_int a, int b, si_int expected) { + si_int x = __ashrsi3(a, b); + if (x != expected) + printf("error in __ashrsi3: %X >> %d = %X, expected %X\n", + a, b, __ashrsi3(a, b), expected); + return x != expected; +} + +int main() { + if (test__ashrsi3(0x01234567, 0, 0x1234567)) + return 1; + if (test__ashrsi3(0x01234567, 1, 0x91A2B3)) + return 1; + if (test__ashrsi3(0x01234567, 2, 0x48D159)) + return 1; + if (test__ashrsi3(0x01234567, 3, 0x2468AC)) + return 1; + if (test__ashrsi3(0x01234567, 4, 0x123456)) + return 1; + + if (test__ashrsi3(0x01234567, 12, 0x1234)) + return 1; + if (test__ashrsi3(0x01234567, 13, 0x91A)) + return 1; + if (test__ashrsi3(0x01234567, 14, 0x48D)) + return 1; + if (test__ashrsi3(0x01234567, 15, 0x246)) + return 1; + + if (test__ashrsi3(0x01234567, 16, 0x123)) + return 1; + + if (test__ashrsi3(0x01234567, 17, 0x91)) + return 1; + if (test__ashrsi3(0x01234567, 18, 0x48)) + return 1; + if (test__ashrsi3(0x01234567, 19, 0x24)) + return 1; + if (test__ashrsi3(0x01234567, 20, 0x12)) + return 1; + + if (test__ashrsi3(0x01234567, 28, 0)) + return 1; + if (test__ashrsi3(0x01234567, 29, 0)) + return 1; + if (test__ashrsi3(0x01234567, 30, 0)) + return 1; + if (test__ashrsi3(0x01234567, 31, 0)) + return 1; + + if (test__ashrsi3(0xFEDCBA98, 0, 0xFEDCBA98)) + return 1; + if (test__ashrsi3(0xFEDCBA98, 1, 0xFF6E5D4C)) + return 1; + if (test__ashrsi3(0xFEDCBA98, 2, 0xFFB72EA6)) + return 1; + if (test__ashrsi3(0xFEDCBA98, 3, 0xFFDB9753)) + return 1; + if (test__ashrsi3(0xFEDCBA98, 4, 0xFFEDCBA9)) + return 1; + + if (test__ashrsi3(0xFEDCBA98, 12, 0xFFFFEDCB)) + return 1; + if (test__ashrsi3(0xFEDCBA98, 13, 0xFFFFF6E5)) + return 1; + if (test__ashrsi3(0xFEDCBA98, 14, 0xFFFFFB72)) + return 1; + if (test__ashrsi3(0xFEDCBA98, 15, 0xFFFFFDB9)) + return 1; + + if (test__ashrsi3(0xFEDCBA98, 16, 0xFFFFFEDC)) + return 1; + + if (test__ashrsi3(0xFEDCBA98, 17, 0xFFFFFF6E)) + return 1; + if (test__ashrsi3(0xFEDCBA98, 18, 0xFFFFFFB7)) + return 1; + if (test__ashrsi3(0xFEDCBA98, 19, 0xFFFFFFDB)) + return 1; + if (test__ashrsi3(0xFEDCBA98, 20, 0xFFFFFFED)) + return 1; + + if (test__ashrsi3(0xAEDCBA98, 28, 0xFFFFFFFA)) + return 1; + if (test__ashrsi3(0xAEDCBA98, 29, 0xFFFFFFFD)) + return 1; + if (test__ashrsi3(0xAEDCBA98, 30, 0xFFFFFFFE)) + return 1; + if (test__ashrsi3(0xAEDCBA98, 31, 0xFFFFFFFF)) + return 1; + return 0; +} diff --git a/compiler-rt/test/builtins/Unit/lshrsi3_test.c b/compiler-rt/test/builtins/Unit/lshrsi3_test.c new file mode 100644 --- /dev/null +++ b/compiler-rt/test/builtins/Unit/lshrsi3_test.c @@ -0,0 +1,115 @@ +// RUN: %clang_builtins %s %librt -o %t && %run %t +// REQUIRES: librt_has_lshrsi3 +//===-- lshrsi3_test.c - Test __lshrsi3 -----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file tests __lshrsi3 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" +#include + +// Returns: logical a >> b + +// Precondition: 0 <= b < bits_in_dword + +COMPILER_RT_ABI si_int __lshrsi3(si_int a, int b); + +int test__lshrsi3(si_int a, int b, si_int expected) { + si_int x = __lshrsi3(a, b); + if (x != expected) + printf("error in __lshrsi3: %X >> %d = %X, expected %X\n", + a, b, __lshrsi3(a, b), expected); + return x != expected; +} + +int main() { + if (test__lshrsi3(0x01234567, 0, 0x1234567)) + return 1; + if (test__lshrsi3(0x01234567, 1, 0x91A2B3)) + return 1; + if (test__lshrsi3(0x01234567, 2, 0x48D159)) + return 1; + if (test__lshrsi3(0x01234567, 3, 0x2468AC)) + return 1; + if (test__lshrsi3(0x01234567, 4, 0x123456)) + return 1; + + if (test__lshrsi3(0x01234567, 12, 0x1234)) + return 1; + if (test__lshrsi3(0x01234567, 13, 0x91A)) + return 1; + if (test__lshrsi3(0x01234567, 14, 0x48D)) + return 1; + if (test__lshrsi3(0x01234567, 15, 0x246)) + return 1; + + if (test__lshrsi3(0x01234567, 16, 0x123)) + return 1; + + if (test__lshrsi3(0x01234567, 17, 0x91)) + return 1; + if (test__lshrsi3(0x01234567, 18, 0x48)) + return 1; + if (test__lshrsi3(0x01234567, 19, 0x24)) + return 1; + if (test__lshrsi3(0x01234567, 20, 0x12)) + return 1; + + if (test__lshrsi3(0x01234567, 28, 0)) + return 1; + if (test__lshrsi3(0x01234567, 29, 0)) + return 1; + if (test__lshrsi3(0x01234567, 30, 0)) + return 1; + if (test__lshrsi3(0x01234567, 31, 0)) + return 1; + + if (test__lshrsi3(0xFEDCBA98, 0, 0xFEDCBA98)) + return 1; + if (test__lshrsi3(0xFEDCBA98, 1, 0x7F6E5D4C)) + return 1; + if (test__lshrsi3(0xFEDCBA98, 2, 0x3FB72EA6)) + return 1; + if (test__lshrsi3(0xFEDCBA98, 3, 0x1FDB9753)) + return 1; + if (test__lshrsi3(0xFEDCBA98, 4, 0xFEDCBA9)) + return 1; + + if (test__lshrsi3(0xFEDCBA98, 12, 0xFEDCB)) + return 1; + if (test__lshrsi3(0xFEDCBA98, 13, 0x7F6E5)) + return 1; + if (test__lshrsi3(0xFEDCBA98, 14, 0x3FB72)) + return 1; + if (test__lshrsi3(0xFEDCBA98, 15, 0x1FDB9)) + return 1; + + if (test__lshrsi3(0xFEDCBA98, 16, 0xFEDC)) + return 1; + + if (test__lshrsi3(0xFEDCBA98, 17, 0x7F6E)) + return 1; + if (test__lshrsi3(0xFEDCBA98, 18, 0x3FB7)) + return 1; + if (test__lshrsi3(0xFEDCBA98, 19, 0x1FDB)) + return 1; + if (test__lshrsi3(0xFEDCBA98, 20, 0xFED)) + return 1; + + if (test__lshrsi3(0xAEDCBA98, 28, 0xA)) + return 1; + if (test__lshrsi3(0xAEDCBA98, 29, 0x5)) + return 1; + if (test__lshrsi3(0xAEDCBA98, 30, 0x2)) + return 1; + if (test__lshrsi3(0xAEDCBA98, 31, 0x1)) + return 1; + return 0; +}