blob: 50ed3579b7f8e2db06c523583a850017b706e6e8 [file] [log] [blame]
Alexandre Ramesb78f1392016-07-01 14:22:22 +01001// Copyright 2015, VIXL authors
armvixl6e2c8272015-03-31 11:04:14 +01002// 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 Bramleyaf0ca0a2019-02-19 11:47:31 +000031#include <limits.h>
Alexandre Rames1f9074d2016-05-23 15:50:01 +010032#include "globals-vixl.h"
armvixl6e2c8272015-03-31 11:04:14 +010033
34namespace vixl {
35
36// Helper to check whether the version of GCC used is greater than the specified
37// requirement.
38#define MAJOR 1000000
39#define MINOR 1000
40#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
armvixl0f35e362016-05-10 13:57:58 +010041#define GCC_VERSION_OR_NEWER(major, minor, patchlevel) \
42 ((__GNUC__ * (MAJOR) + __GNUC_MINOR__ * (MINOR) + __GNUC_PATCHLEVEL__) >= \
43 ((major) * (MAJOR) + ((minor)) * (MINOR) + (patchlevel)))
armvixl6e2c8272015-03-31 11:04:14 +010044#elif defined(__GNUC__) && defined(__GNUC_MINOR__)
armvixl0f35e362016-05-10 13:57:58 +010045#define GCC_VERSION_OR_NEWER(major, minor, patchlevel) \
46 ((__GNUC__ * (MAJOR) + __GNUC_MINOR__ * (MINOR)) >= \
47 ((major) * (MAJOR) + ((minor)) * (MINOR) + (patchlevel)))
armvixl6e2c8272015-03-31 11:04:14 +010048#else
49#define GCC_VERSION_OR_NEWER(major, minor, patchlevel) 0
50#endif
51
52
53#if defined(__clang__) && !defined(VIXL_NO_COMPILER_BUILTINS)
54
armvixl0f35e362016-05-10 13:57:58 +010055// clang-format off
armvixl6e2c8272015-03-31 11:04:14 +010056#define COMPILER_HAS_BUILTIN_CLRSB (__has_builtin(__builtin_clrsb))
57#define COMPILER_HAS_BUILTIN_CLZ (__has_builtin(__builtin_clz))
58#define COMPILER_HAS_BUILTIN_CTZ (__has_builtin(__builtin_ctz))
59#define COMPILER_HAS_BUILTIN_FFS (__has_builtin(__builtin_ffs))
60#define COMPILER_HAS_BUILTIN_POPCOUNT (__has_builtin(__builtin_popcount))
armvixl0f35e362016-05-10 13:57:58 +010061// clang-format on
armvixl6e2c8272015-03-31 11:04:14 +010062
63#elif defined(__GNUC__) && !defined(VIXL_NO_COMPILER_BUILTINS)
64// The documentation for these builtins is available at:
65// https://gcc.gnu.org/onlinedocs/gcc-$MAJOR.$MINOR.$PATCHLEVEL/gcc//Other-Builtins.html
66
armvixl0f35e362016-05-10 13:57:58 +010067// clang-format off
armvixl6e2c8272015-03-31 11:04:14 +010068# define COMPILER_HAS_BUILTIN_CLRSB (GCC_VERSION_OR_NEWER(4, 7, 0))
69# define COMPILER_HAS_BUILTIN_CLZ (GCC_VERSION_OR_NEWER(3, 4, 0))
70# define COMPILER_HAS_BUILTIN_CTZ (GCC_VERSION_OR_NEWER(3, 4, 0))
71# define COMPILER_HAS_BUILTIN_FFS (GCC_VERSION_OR_NEWER(3, 4, 0))
72# define COMPILER_HAS_BUILTIN_POPCOUNT (GCC_VERSION_OR_NEWER(3, 4, 0))
armvixl0f35e362016-05-10 13:57:58 +010073// clang-format on
armvixl6e2c8272015-03-31 11:04:14 +010074
75#else
76// One can define VIXL_NO_COMPILER_BUILTINS to force using the manually
77// implemented C++ methods.
78
armvixl0f35e362016-05-10 13:57:58 +010079// clang-format off
armvixl6e2c8272015-03-31 11:04:14 +010080#define COMPILER_HAS_BUILTIN_BSWAP false
81#define COMPILER_HAS_BUILTIN_CLRSB false
82#define COMPILER_HAS_BUILTIN_CLZ false
83#define COMPILER_HAS_BUILTIN_CTZ false
84#define COMPILER_HAS_BUILTIN_FFS false
85#define COMPILER_HAS_BUILTIN_POPCOUNT false
armvixl0f35e362016-05-10 13:57:58 +010086// clang-format on
armvixl6e2c8272015-03-31 11:04:14 +010087
88#endif
89
90
armvixl0f35e362016-05-10 13:57:58 +010091template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +010092inline bool IsPowerOf2(V value) {
93 return (value != 0) && ((value & (value - 1)) == 0);
94}
95
96
97// Declaration of fallback functions.
98int CountLeadingSignBitsFallBack(int64_t value, int width);
99int CountLeadingZerosFallBack(uint64_t value, int width);
100int CountSetBitsFallBack(uint64_t value, int width);
101int CountTrailingZerosFallBack(uint64_t value, int width);
102
103
104// Implementation of intrinsics functions.
105// TODO: The implementations could be improved for sizes different from 32bit
106// and 64bit: we could mask the values and call the appropriate builtin.
107
Jacob Bramleyaf0ca0a2019-02-19 11:47:31 +0000108// Return the number of leading bits that match the topmost (sign) bit,
109// excluding the topmost bit itself.
armvixl0f35e362016-05-10 13:57:58 +0100110template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100111inline int CountLeadingSignBits(V value, int width = (sizeof(V) * 8)) {
Jacob Bramleyaf0ca0a2019-02-19 11:47:31 +0000112 VIXL_ASSERT(IsPowerOf2(width) && (width <= 64));
armvixl6e2c8272015-03-31 11:04:14 +0100113#if COMPILER_HAS_BUILTIN_CLRSB
Jacob Bramleyaf0ca0a2019-02-19 11:47:31 +0000114 VIXL_ASSERT((LLONG_MIN <= value) && (value <= LLONG_MAX));
115 int ll_width = sizeof(long long) * kBitsPerByte; // NOLINT(runtime/int)
116 int result = __builtin_clrsbll(value) - (ll_width - width);
117 // Check that the value fits in the specified width.
118 VIXL_ASSERT(result >= 0);
119 return result;
120#else
121 VIXL_ASSERT((INT64_MIN <= value) && (value <= INT64_MAX));
armvixl6e2c8272015-03-31 11:04:14 +0100122 return CountLeadingSignBitsFallBack(value, width);
Jacob Bramleyaf0ca0a2019-02-19 11:47:31 +0000123#endif
armvixl6e2c8272015-03-31 11:04:14 +0100124}
125
126
armvixl0f35e362016-05-10 13:57:58 +0100127template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100128inline int CountLeadingZeros(V value, int width = (sizeof(V) * 8)) {
129#if COMPILER_HAS_BUILTIN_CLZ
130 if (width == 32) {
armvixldb644342015-07-21 11:37:10 +0100131 return (value == 0) ? 32 : __builtin_clz(static_cast<unsigned>(value));
armvixl6e2c8272015-03-31 11:04:14 +0100132 } else if (width == 64) {
133 return (value == 0) ? 64 : __builtin_clzll(value);
134 }
135#endif
136 return CountLeadingZerosFallBack(value, width);
137}
138
139
armvixl0f35e362016-05-10 13:57:58 +0100140template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100141inline int CountSetBits(V value, int width = (sizeof(V) * 8)) {
142#if COMPILER_HAS_BUILTIN_POPCOUNT
143 if (width == 32) {
armvixldb644342015-07-21 11:37:10 +0100144 return __builtin_popcount(static_cast<unsigned>(value));
armvixl6e2c8272015-03-31 11:04:14 +0100145 } else if (width == 64) {
146 return __builtin_popcountll(value);
147 }
148#endif
149 return CountSetBitsFallBack(value, width);
150}
151
152
armvixl0f35e362016-05-10 13:57:58 +0100153template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100154inline int CountTrailingZeros(V value, int width = (sizeof(V) * 8)) {
155#if COMPILER_HAS_BUILTIN_CTZ
156 if (width == 32) {
armvixldb644342015-07-21 11:37:10 +0100157 return (value == 0) ? 32 : __builtin_ctz(static_cast<unsigned>(value));
armvixl6e2c8272015-03-31 11:04:14 +0100158 } else if (width == 64) {
159 return (value == 0) ? 64 : __builtin_ctzll(value);
160 }
161#endif
162 return CountTrailingZerosFallBack(value, width);
163}
164
165} // namespace vixl
166
167#endif // VIXL_COMPILER_INTRINSICS_H