Alexandre Rames | b78f139 | 2016-07-01 14:22:22 +0100 | [diff] [blame] | 1 | // Copyright 2015, VIXL authors |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, |
| 8 | // this list of conditions and the following disclaimer. |
| 9 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // * Neither the name of ARM Limited nor the names of its contributors may be |
| 13 | // used to endorse or promote products derived from this software without |
| 14 | // specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND |
| 17 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 20 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | |
| 27 | |
| 28 | #ifndef VIXL_COMPILER_INTRINSICS_H |
| 29 | #define VIXL_COMPILER_INTRINSICS_H |
| 30 | |
Jacob Bramley | af0ca0a | 2019-02-19 11:47:31 +0000 | [diff] [blame] | 31 | #include <limits.h> |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 32 | |
Alexandre Rames | 1f9074d | 2016-05-23 15:50:01 +0100 | [diff] [blame] | 33 | #include "globals-vixl.h" |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 34 | |
| 35 | namespace vixl { |
| 36 | |
| 37 | // Helper to check whether the version of GCC used is greater than the specified |
| 38 | // requirement. |
| 39 | #define MAJOR 1000000 |
| 40 | #define MINOR 1000 |
| 41 | #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 42 | #define GCC_VERSION_OR_NEWER(major, minor, patchlevel) \ |
| 43 | ((__GNUC__ * (MAJOR) + __GNUC_MINOR__ * (MINOR) + __GNUC_PATCHLEVEL__) >= \ |
| 44 | ((major) * (MAJOR) + ((minor)) * (MINOR) + (patchlevel))) |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 45 | #elif defined(__GNUC__) && defined(__GNUC_MINOR__) |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 46 | #define GCC_VERSION_OR_NEWER(major, minor, patchlevel) \ |
| 47 | ((__GNUC__ * (MAJOR) + __GNUC_MINOR__ * (MINOR)) >= \ |
| 48 | ((major) * (MAJOR) + ((minor)) * (MINOR) + (patchlevel))) |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 49 | #else |
| 50 | #define GCC_VERSION_OR_NEWER(major, minor, patchlevel) 0 |
| 51 | #endif |
| 52 | |
| 53 | |
| 54 | #if defined(__clang__) && !defined(VIXL_NO_COMPILER_BUILTINS) |
| 55 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 56 | // clang-format off |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 57 | #define COMPILER_HAS_BUILTIN_CLRSB (__has_builtin(__builtin_clrsb)) |
| 58 | #define COMPILER_HAS_BUILTIN_CLZ (__has_builtin(__builtin_clz)) |
| 59 | #define COMPILER_HAS_BUILTIN_CTZ (__has_builtin(__builtin_ctz)) |
| 60 | #define COMPILER_HAS_BUILTIN_FFS (__has_builtin(__builtin_ffs)) |
| 61 | #define COMPILER_HAS_BUILTIN_POPCOUNT (__has_builtin(__builtin_popcount)) |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 62 | // clang-format on |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 63 | |
| 64 | #elif defined(__GNUC__) && !defined(VIXL_NO_COMPILER_BUILTINS) |
| 65 | // The documentation for these builtins is available at: |
| 66 | // https://gcc.gnu.org/onlinedocs/gcc-$MAJOR.$MINOR.$PATCHLEVEL/gcc//Other-Builtins.html |
| 67 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 68 | // clang-format off |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 69 | # define COMPILER_HAS_BUILTIN_CLRSB (GCC_VERSION_OR_NEWER(4, 7, 0)) |
| 70 | # define COMPILER_HAS_BUILTIN_CLZ (GCC_VERSION_OR_NEWER(3, 4, 0)) |
| 71 | # define COMPILER_HAS_BUILTIN_CTZ (GCC_VERSION_OR_NEWER(3, 4, 0)) |
| 72 | # define COMPILER_HAS_BUILTIN_FFS (GCC_VERSION_OR_NEWER(3, 4, 0)) |
| 73 | # define COMPILER_HAS_BUILTIN_POPCOUNT (GCC_VERSION_OR_NEWER(3, 4, 0)) |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 74 | // clang-format on |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 75 | |
| 76 | #else |
| 77 | // One can define VIXL_NO_COMPILER_BUILTINS to force using the manually |
| 78 | // implemented C++ methods. |
| 79 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 80 | // clang-format off |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 81 | #define COMPILER_HAS_BUILTIN_BSWAP false |
| 82 | #define COMPILER_HAS_BUILTIN_CLRSB false |
| 83 | #define COMPILER_HAS_BUILTIN_CLZ false |
| 84 | #define COMPILER_HAS_BUILTIN_CTZ false |
| 85 | #define COMPILER_HAS_BUILTIN_FFS false |
| 86 | #define COMPILER_HAS_BUILTIN_POPCOUNT false |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 87 | // clang-format on |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 88 | |
| 89 | #endif |
| 90 | |
| 91 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 92 | template <typename V> |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 93 | inline bool IsPowerOf2(V value) { |
| 94 | return (value != 0) && ((value & (value - 1)) == 0); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | // Declaration of fallback functions. |
| 99 | int CountLeadingSignBitsFallBack(int64_t value, int width); |
| 100 | int CountLeadingZerosFallBack(uint64_t value, int width); |
| 101 | int CountSetBitsFallBack(uint64_t value, int width); |
| 102 | int CountTrailingZerosFallBack(uint64_t value, int width); |
| 103 | |
| 104 | |
| 105 | // Implementation of intrinsics functions. |
| 106 | // TODO: The implementations could be improved for sizes different from 32bit |
| 107 | // and 64bit: we could mask the values and call the appropriate builtin. |
| 108 | |
Jacob Bramley | af0ca0a | 2019-02-19 11:47:31 +0000 | [diff] [blame] | 109 | // Return the number of leading bits that match the topmost (sign) bit, |
| 110 | // excluding the topmost bit itself. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 111 | template <typename V> |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 112 | inline int CountLeadingSignBits(V value, int width = (sizeof(V) * 8)) { |
Jacob Bramley | af0ca0a | 2019-02-19 11:47:31 +0000 | [diff] [blame] | 113 | VIXL_ASSERT(IsPowerOf2(width) && (width <= 64)); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 114 | #if COMPILER_HAS_BUILTIN_CLRSB |
Jacob Bramley | af0ca0a | 2019-02-19 11:47:31 +0000 | [diff] [blame] | 115 | VIXL_ASSERT((LLONG_MIN <= value) && (value <= LLONG_MAX)); |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 116 | int ll_width = |
| 117 | sizeof(long long) * kBitsPerByte; // NOLINT(google-runtime-int) |
Jacob Bramley | af0ca0a | 2019-02-19 11:47:31 +0000 | [diff] [blame] | 118 | int result = __builtin_clrsbll(value) - (ll_width - width); |
| 119 | // Check that the value fits in the specified width. |
| 120 | VIXL_ASSERT(result >= 0); |
| 121 | return result; |
| 122 | #else |
| 123 | VIXL_ASSERT((INT64_MIN <= value) && (value <= INT64_MAX)); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 124 | return CountLeadingSignBitsFallBack(value, width); |
Jacob Bramley | af0ca0a | 2019-02-19 11:47:31 +0000 | [diff] [blame] | 125 | #endif |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 129 | template <typename V> |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 130 | inline int CountLeadingZeros(V value, int width = (sizeof(V) * 8)) { |
| 131 | #if COMPILER_HAS_BUILTIN_CLZ |
| 132 | if (width == 32) { |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 133 | return (value == 0) ? 32 : __builtin_clz(static_cast<unsigned>(value)); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 134 | } else if (width == 64) { |
| 135 | return (value == 0) ? 64 : __builtin_clzll(value); |
| 136 | } |
| 137 | #endif |
| 138 | return CountLeadingZerosFallBack(value, width); |
| 139 | } |
| 140 | |
| 141 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 142 | template <typename V> |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 143 | inline int CountSetBits(V value, int width = (sizeof(V) * 8)) { |
| 144 | #if COMPILER_HAS_BUILTIN_POPCOUNT |
| 145 | if (width == 32) { |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 146 | return __builtin_popcount(static_cast<unsigned>(value)); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 147 | } else if (width == 64) { |
| 148 | return __builtin_popcountll(value); |
| 149 | } |
| 150 | #endif |
| 151 | return CountSetBitsFallBack(value, width); |
| 152 | } |
| 153 | |
| 154 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 155 | template <typename V> |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 156 | inline int CountTrailingZeros(V value, int width = (sizeof(V) * 8)) { |
| 157 | #if COMPILER_HAS_BUILTIN_CTZ |
| 158 | if (width == 32) { |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 159 | return (value == 0) ? 32 : __builtin_ctz(static_cast<unsigned>(value)); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 160 | } else if (width == 64) { |
| 161 | return (value == 0) ? 64 : __builtin_ctzll(value); |
| 162 | } |
| 163 | #endif |
| 164 | return CountTrailingZerosFallBack(value, width); |
| 165 | } |
| 166 | |
| 167 | } // namespace vixl |
| 168 | |
| 169 | #endif // VIXL_COMPILER_INTRINSICS_H |