Index: compiler-rt/lib/builtins/CMakeLists.txt =================================================================== --- compiler-rt/lib/builtins/CMakeLists.txt +++ 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 Index: compiler-rt/lib/builtins/ashlsi3.c =================================================================== --- /dev/null +++ compiler-rt/lib/builtins/ashlsi3.c @@ -0,0 +1,34 @@ +// ====-- 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" + +// Returns: a << b + +// Preconsition: 0 <= b < bits_in_sword + +COMPILER_RT_ABI si_int __ashlsi3(si_int a, int b) { + const int bits_in_word = (int)(sizeof(hi_int) * CHAR_BIT); + swords input; + swords 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; +} Index: compiler-rt/lib/builtins/ashrsi3.c =================================================================== --- /dev/null +++ compiler-rt/lib/builtins/ashrsi3.c @@ -0,0 +1,35 @@ +//===-- 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" + +// Returns: arithmetic a >> b + +// Precondition: 0 <= b < bits_in_dword + +COMPILER_RT_ABI si_int __ashrsi3(si_int a, int b) { + const int bits_in_word = (int)(sizeof(hi_int) * CHAR_BIT); + swords input; + swords 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; +} Index: compiler-rt/lib/builtins/int_types.h =================================================================== --- compiler-rt/lib/builtins/int_types.h +++ 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 @@ -28,6 +31,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 { Index: compiler-rt/lib/builtins/lshrsi3.c =================================================================== --- /dev/null +++ compiler-rt/lib/builtins/lshrsi3.c @@ -0,0 +1,34 @@ +//===-- 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" + +// Returns: logical a >> b + +// Precondition: 0 <= b < bits_in_sword + +COMPILER_RT_ABI si_int __lshrsi3(si_int a, int b) { + const int bits_in_word = (int)(sizeof(hi_int) * CHAR_BIT); + uswords input; + uswords 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; +} Index: compiler-rt/test/builtins/Unit/ashlsi3_test.c =================================================================== --- /dev/null +++ compiler-rt/test/builtins/Unit/ashlsi3_test.c @@ -0,0 +1,76 @@ +// 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; +} Index: compiler-rt/test/builtins/Unit/ashrsi3_test.c =================================================================== --- /dev/null +++ compiler-rt/test/builtins/Unit/ashrsi3_test.c @@ -0,0 +1,117 @@ +// 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; +} Index: compiler-rt/test/builtins/Unit/lshrsi3_test.c =================================================================== --- /dev/null +++ compiler-rt/test/builtins/Unit/lshrsi3_test.c @@ -0,0 +1,117 @@ +// 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; +}