Grok  9.7.5
detect_compiler_arch.h
Go to the documentation of this file.
1 // Copyright 2020 Google LLC
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef HIGHWAY_HWY_DETECT_COMPILER_ARCH_H_
17 #define HIGHWAY_HWY_DETECT_COMPILER_ARCH_H_
18 
19 // Detects compiler and arch from predefined macros. Zero dependencies for
20 // inclusion by foreach_target.h.
21 
22 // Add to #if conditions to prevent IDE from graying out code.
23 #if (defined __CDT_PARSER__) || (defined __INTELLISENSE__) || \
24  (defined Q_CREATOR_RUN) || (defined(__CLANGD__))
25 #define HWY_IDE 1
26 #else
27 #define HWY_IDE 0
28 #endif
29 
30 //------------------------------------------------------------------------------
31 // Compiler
32 
33 // clang-cl defines _MSC_VER but doesn't behave like MSVC in other aspects like
34 // used in HWY_DIAGNOSTICS(). We include a check that we are not clang for that
35 // purpose.
36 #if defined(_MSC_VER) && !defined(__clang__)
37 #define HWY_COMPILER_MSVC _MSC_VER
38 #else
39 #define HWY_COMPILER_MSVC 0
40 #endif
41 
42 #ifdef __INTEL_COMPILER
43 #define HWY_COMPILER_ICC __INTEL_COMPILER
44 #else
45 #define HWY_COMPILER_ICC 0
46 #endif
47 
48 #ifdef __GNUC__
49 #define HWY_COMPILER_GCC (__GNUC__ * 100 + __GNUC_MINOR__)
50 #else
51 #define HWY_COMPILER_GCC 0
52 #endif
53 
54 // Clang can masquerade as MSVC/GCC, in which case both are set.
55 #ifdef __clang__
56 #ifdef __APPLE__
57 // Apple LLVM version is unrelated to the actual Clang version, which we need
58 // for enabling workarounds. Use the presence of warning flags to deduce it.
59 // Adapted from https://github.com/simd-everywhere/simde/ simde-detect-clang.h.
60 #if __has_warning("-Wformat-insufficient-args")
61 #define HWY_COMPILER_CLANG 1200
62 #elif __has_warning("-Wimplicit-const-int-float-conversion")
63 #define HWY_COMPILER_CLANG 1100
64 #elif __has_warning("-Wmisleading-indentation")
65 #define HWY_COMPILER_CLANG 1000
66 #elif defined(__FILE_NAME__)
67 #define HWY_COMPILER_CLANG 900
68 #elif __has_warning("-Wextra-semi-stmt") || \
69  __has_builtin(__builtin_rotateleft32)
70 #define HWY_COMPILER_CLANG 800
71 #elif __has_warning("-Wc++98-compat-extra-semi")
72 #define HWY_COMPILER_CLANG 700
73 #else // Anything older than 7.0 is not recommended for Highway.
74 #define HWY_COMPILER_CLANG 600
75 #endif // __has_warning chain
76 #else // Non-Apple: normal version
77 #define HWY_COMPILER_CLANG (__clang_major__ * 100 + __clang_minor__)
78 #endif
79 #else // Not clang
80 #define HWY_COMPILER_CLANG 0
81 #endif
82 
83 // More than one may be nonzero, but we want at least one.
84 #if !HWY_COMPILER_MSVC && !HWY_COMPILER_ICC && !HWY_COMPILER_GCC && \
85  !HWY_COMPILER_CLANG
86 #error "Unsupported compiler"
87 #endif
88 
89 #ifdef __has_builtin
90 #define HWY_HAS_BUILTIN(name) __has_builtin(name)
91 #else
92 #define HWY_HAS_BUILTIN(name) 0
93 #endif
94 
95 #ifdef __has_attribute
96 #define HWY_HAS_ATTRIBUTE(name) __has_attribute(name)
97 #else
98 #define HWY_HAS_ATTRIBUTE(name) 0
99 #endif
100 
101 #ifdef __has_feature
102 #define HWY_HAS_FEATURE(name) __has_feature(name)
103 #else
104 #define HWY_HAS_FEATURE(name) 0
105 #endif
106 
107 //------------------------------------------------------------------------------
108 // Architecture
109 
110 #if defined(__i386__) || defined(_M_IX86)
111 #define HWY_ARCH_X86_32 1
112 #else
113 #define HWY_ARCH_X86_32 0
114 #endif
115 
116 #if defined(__x86_64__) || defined(_M_X64)
117 #define HWY_ARCH_X86_64 1
118 #else
119 #define HWY_ARCH_X86_64 0
120 #endif
121 
122 #if HWY_ARCH_X86_32 && HWY_ARCH_X86_64
123 #error "Cannot have both x86-32 and x86-64"
124 #endif
125 
126 #if HWY_ARCH_X86_32 || HWY_ARCH_X86_64
127 #define HWY_ARCH_X86 1
128 #else
129 #define HWY_ARCH_X86 0
130 #endif
131 
132 #if defined(__powerpc64__) || defined(_M_PPC)
133 #define HWY_ARCH_PPC 1
134 #else
135 #define HWY_ARCH_PPC 0
136 #endif
137 
138 #if defined(__ARM_ARCH_ISA_A64) || defined(__aarch64__) || defined(_M_ARM64)
139 #define HWY_ARCH_ARM_A64 1
140 #else
141 #define HWY_ARCH_ARM_A64 0
142 #endif
143 
144 #if defined(__arm__) || defined(_M_ARM)
145 #define HWY_ARCH_ARM_V7 1
146 #else
147 #define HWY_ARCH_ARM_V7 0
148 #endif
149 
150 #if HWY_ARCH_ARM_A64 && HWY_ARCH_ARM_V7
151 #error "Cannot have both A64 and V7"
152 #endif
153 
154 #if HWY_ARCH_ARM_A64 || HWY_ARCH_ARM_V7
155 #define HWY_ARCH_ARM 1
156 #else
157 #define HWY_ARCH_ARM 0
158 #endif
159 
160 #if defined(__EMSCRIPTEN__) || defined(__wasm__) || defined(__WASM__)
161 #define HWY_ARCH_WASM 1
162 #else
163 #define HWY_ARCH_WASM 0
164 #endif
165 
166 #ifdef __riscv
167 #define HWY_ARCH_RVV 1
168 #else
169 #define HWY_ARCH_RVV 0
170 #endif
171 
172 // It is an error to detect multiple architectures at the same time, but OK to
173 // detect none of the above.
174 #if (HWY_ARCH_X86 + HWY_ARCH_PPC + HWY_ARCH_ARM + HWY_ARCH_WASM + \
175  HWY_ARCH_RVV) > 1
176 #error "Must not detect more than one architecture"
177 #endif
178 
179 #endif // HIGHWAY_HWY_DETECT_COMPILER_ARCH_H_