Daniel Dunbar | 4467b65 | 2009-10-27 17:48:46 +0000 | [diff] [blame] | 1 | /* ===-- int_lib.h - configuration header for compiler-rt -----------------=== |
Edward O'Callaghan | 0e4ad9c | 2009-08-05 01:47:29 +0000 | [diff] [blame] | 2 | * |
Chandler Carruth | 7a739a0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Edward O'Callaghan | 0e4ad9c | 2009-08-05 01:47:29 +0000 | [diff] [blame] | 6 | * |
| 7 | * ===----------------------------------------------------------------------=== |
| 8 | * |
Daniel Dunbar | 4467b65 | 2009-10-27 17:48:46 +0000 | [diff] [blame] | 9 | * This file is a configuration header for compiler-rt. |
Edward O'Callaghan | 0e4ad9c | 2009-08-05 01:47:29 +0000 | [diff] [blame] | 10 | * This file is not part of the interface of this library. |
| 11 | * |
| 12 | * ===----------------------------------------------------------------------=== |
| 13 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 14 | |
| 15 | #ifndef INT_LIB_H |
| 16 | #define INT_LIB_H |
| 17 | |
Daniel Dunbar | 0a3e350 | 2011-11-15 18:56:21 +0000 | [diff] [blame] | 18 | /* Assumption: Signed integral is 2's complement. */ |
| 19 | /* Assumption: Right shift of signed negative is arithmetic shift. */ |
| 20 | /* Assumption: Endianness is little or big (not mixed). */ |
| 21 | |
Saleem Abdulrasool | d3a8aba | 2015-08-21 04:39:52 +0000 | [diff] [blame] | 22 | #if defined(__ELF__) |
Josh Gao | c852516 | 2015-08-21 02:51:17 +0000 | [diff] [blame] | 23 | #define FNALIAS(alias_name, original_name) \ |
Eli Friedman | 83774b4 | 2017-10-03 21:25:07 +0000 | [diff] [blame] | 24 | void alias_name() __attribute__((__alias__(#original_name))) |
| 25 | #define COMPILER_RT_ALIAS(aliasee) __attribute__((__alias__(#aliasee))) |
Saleem Abdulrasool | d3a8aba | 2015-08-21 04:39:52 +0000 | [diff] [blame] | 26 | #else |
| 27 | #define FNALIAS(alias, name) _Pragma("GCC error(\"alias unsupported on this file format\")") |
Eli Friedman | 83774b4 | 2017-10-03 21:25:07 +0000 | [diff] [blame] | 28 | #define COMPILER_RT_ALIAS(aliasee) _Pragma("GCC error(\"alias unsupported on this file format\")") |
Saleem Abdulrasool | d3a8aba | 2015-08-21 04:39:52 +0000 | [diff] [blame] | 29 | #endif |
Josh Gao | c852516 | 2015-08-21 02:51:17 +0000 | [diff] [blame] | 30 | |
Daniel Dunbar | 0ae9d25 | 2011-11-15 18:34:44 +0000 | [diff] [blame] | 31 | /* ABI macro definitions */ |
| 32 | |
| 33 | #if __ARM_EABI__ |
Weiming Zhao | 260f2c5 | 2017-03-29 03:36:46 +0000 | [diff] [blame] | 34 | # ifdef COMPILER_RT_ARMHF_TARGET |
| 35 | # define COMPILER_RT_ABI |
| 36 | # else |
Saleem Abdulrasool | 5c1b865 | 2017-05-16 04:17:12 +0000 | [diff] [blame] | 37 | # define COMPILER_RT_ABI __attribute__((__pcs__("aapcs"))) |
Weiming Zhao | 260f2c5 | 2017-03-29 03:36:46 +0000 | [diff] [blame] | 38 | # endif |
Daniel Dunbar | 0ae9d25 | 2011-11-15 18:34:44 +0000 | [diff] [blame] | 39 | #else |
Saleem Abdulrasool | e917f6c | 2016-04-20 17:43:40 +0000 | [diff] [blame] | 40 | # define COMPILER_RT_ABI |
Daniel Dunbar | 0ae9d25 | 2011-11-15 18:34:44 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
Saleem Abdulrasool | 99e2e66 | 2017-05-16 16:41:37 +0000 | [diff] [blame] | 43 | #define AEABI_RTABI __attribute__((__pcs__("aapcs"))) |
| 44 | |
Reid Kleckner | d92c8cd | 2018-10-29 22:48:14 +0000 | [diff] [blame] | 45 | #if defined(_MSC_VER) && !defined(__clang__) |
Saleem Abdulrasool | 4a5943a | 2015-10-11 17:35:42 +0000 | [diff] [blame] | 46 | #define ALWAYS_INLINE __forceinline |
Saleem Abdulrasool | 08b610e | 2015-10-06 04:33:05 +0000 | [diff] [blame] | 47 | #define NOINLINE __declspec(noinline) |
| 48 | #define NORETURN __declspec(noreturn) |
| 49 | #define UNUSED |
| 50 | #else |
Saleem Abdulrasool | 4a5943a | 2015-10-11 17:35:42 +0000 | [diff] [blame] | 51 | #define ALWAYS_INLINE __attribute__((always_inline)) |
Saleem Abdulrasool | 08b610e | 2015-10-06 04:33:05 +0000 | [diff] [blame] | 52 | #define NOINLINE __attribute__((noinline)) |
| 53 | #define NORETURN __attribute__((noreturn)) |
| 54 | #define UNUSED __attribute__((unused)) |
| 55 | #endif |
| 56 | |
Joerg Sonnenberger | 09226bc | 2013-12-03 16:19:14 +0000 | [diff] [blame] | 57 | #if defined(__NetBSD__) && (defined(_KERNEL) || defined(_STANDALONE)) |
| 58 | /* |
| 59 | * Kernel and boot environment can't use normal headers, |
| 60 | * so use the equivalent system headers. |
| 61 | */ |
| 62 | # include <machine/limits.h> |
| 63 | # include <sys/stdint.h> |
| 64 | # include <sys/types.h> |
| 65 | #else |
Daniel Dunbar | 2bf9340 | 2011-11-16 00:20:36 +0000 | [diff] [blame] | 66 | /* Include the standard compiler builtin headers we use functionality from. */ |
Joerg Sonnenberger | 09226bc | 2013-12-03 16:19:14 +0000 | [diff] [blame] | 67 | # include <limits.h> |
| 68 | # include <stdint.h> |
| 69 | # include <stdbool.h> |
| 70 | # include <float.h> |
| 71 | #endif |
Daniel Dunbar | 2bf9340 | 2011-11-16 00:20:36 +0000 | [diff] [blame] | 72 | |
Daniel Dunbar | 396a72f | 2011-11-15 18:56:13 +0000 | [diff] [blame] | 73 | /* Include the commonly used internal type definitions. */ |
| 74 | #include "int_types.h" |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 75 | |
Daniel Dunbar | 401f693 | 2011-11-16 01:19:19 +0000 | [diff] [blame] | 76 | /* Include internal utility function declarations. */ |
| 77 | #include "int_util.h" |
| 78 | |
Joerg Sonnenberger | bfbb8bb | 2014-03-01 15:30:50 +0000 | [diff] [blame] | 79 | COMPILER_RT_ABI si_int __paritysi2(si_int a); |
| 80 | COMPILER_RT_ABI si_int __paritydi2(di_int a); |
| 81 | |
| 82 | COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b); |
| 83 | COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b); |
| 84 | COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d); |
| 85 | |
| 86 | COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int* rem); |
| 87 | COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int* rem); |
| 88 | #ifdef CRT_HAS_128BIT |
Joerg Sonnenberger | ae87cca | 2014-03-01 15:57:30 +0000 | [diff] [blame] | 89 | COMPILER_RT_ABI si_int __clzti2(ti_int a); |
Joerg Sonnenberger | bfbb8bb | 2014-03-01 15:30:50 +0000 | [diff] [blame] | 90 | COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem); |
| 91 | #endif |
| 92 | |
Saleem Abdulrasool | 52f9fb1 | 2015-10-07 02:58:11 +0000 | [diff] [blame] | 93 | /* Definitions for builtins unavailable on MSVC */ |
| 94 | #if defined(_MSC_VER) && !defined(__clang__) |
| 95 | #include <intrin.h> |
| 96 | |
| 97 | uint32_t __inline __builtin_ctz(uint32_t value) { |
Reid Kleckner | 8d02463 | 2016-08-01 18:39:27 +0000 | [diff] [blame] | 98 | unsigned long trailing_zero = 0; |
Saleem Abdulrasool | 52f9fb1 | 2015-10-07 02:58:11 +0000 | [diff] [blame] | 99 | if (_BitScanForward(&trailing_zero, value)) |
| 100 | return trailing_zero; |
| 101 | return 32; |
| 102 | } |
| 103 | |
| 104 | uint32_t __inline __builtin_clz(uint32_t value) { |
Reid Kleckner | 8d02463 | 2016-08-01 18:39:27 +0000 | [diff] [blame] | 105 | unsigned long leading_zero = 0; |
Saleem Abdulrasool | 52f9fb1 | 2015-10-07 02:58:11 +0000 | [diff] [blame] | 106 | if (_BitScanReverse(&leading_zero, value)) |
| 107 | return 31 - leading_zero; |
| 108 | return 32; |
| 109 | } |
| 110 | |
Saleem Abdulrasool | cdcbb4a | 2015-10-10 17:57:37 +0000 | [diff] [blame] | 111 | #if defined(_M_ARM) || defined(_M_X64) |
Saleem Abdulrasool | 52f9fb1 | 2015-10-07 02:58:11 +0000 | [diff] [blame] | 112 | uint32_t __inline __builtin_clzll(uint64_t value) { |
Reid Kleckner | 8d02463 | 2016-08-01 18:39:27 +0000 | [diff] [blame] | 113 | unsigned long leading_zero = 0; |
Saleem Abdulrasool | 52f9fb1 | 2015-10-07 02:58:11 +0000 | [diff] [blame] | 114 | if (_BitScanReverse64(&leading_zero, value)) |
| 115 | return 63 - leading_zero; |
| 116 | return 64; |
| 117 | } |
Saleem Abdulrasool | b6cbcef | 2015-10-15 02:46:37 +0000 | [diff] [blame] | 118 | #else |
| 119 | uint32_t __inline __builtin_clzll(uint64_t value) { |
| 120 | if (value == 0) |
| 121 | return 64; |
| 122 | uint32_t msh = (uint32_t)(value >> 32); |
| 123 | uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF); |
| 124 | if (msh != 0) |
| 125 | return __builtin_clz(msh); |
| 126 | return 32 + __builtin_clz(lsh); |
| 127 | } |
| 128 | #endif |
Saleem Abdulrasool | 52f9fb1 | 2015-10-07 02:58:11 +0000 | [diff] [blame] | 129 | |
| 130 | #define __builtin_clzl __builtin_clzll |
Nico Weber | d800108 | 2015-12-22 17:22:25 +0000 | [diff] [blame] | 131 | #endif /* defined(_MSC_VER) && !defined(__clang__) */ |
Saleem Abdulrasool | 52f9fb1 | 2015-10-07 02:58:11 +0000 | [diff] [blame] | 132 | |
Edward O'Callaghan | 0898ee9 | 2009-08-05 19:57:20 +0000 | [diff] [blame] | 133 | #endif /* INT_LIB_H */ |