Grok 10.0.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 (defined GROK_ELLIPSIS_BUILD)
26#define HWY_IDE 1
27#else
28#define HWY_IDE 0
29#endif
30
31//------------------------------------------------------------------------------
32// Compiler
33
34// Actual MSVC, not clang-cl, which defines _MSC_VER but doesn't behave like
35// MSVC in other aspects (e.g. HWY_DIAGNOSTICS).
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#if defined(_MSC_VER) && defined(__clang__)
43#define HWY_COMPILER_CLANGCL _MSC_VER
44#else
45#define HWY_COMPILER_CLANGCL 0
46#endif
47
48#ifdef __INTEL_COMPILER
49#define HWY_COMPILER_ICC __INTEL_COMPILER
50#else
51#define HWY_COMPILER_ICC 0
52#endif
53
54#ifdef __INTEL_LLVM_COMPILER
55#define HWY_COMPILER_ICX __INTEL_LLVM_COMPILER
56#else
57#define HWY_COMPILER_ICX 0
58#endif
59
60// HWY_COMPILER_GCC is a generic macro for all compilers implementing the GNU
61// compiler extensions (eg. Clang, Intel...)
62#ifdef __GNUC__
63#define HWY_COMPILER_GCC (__GNUC__ * 100 + __GNUC_MINOR__)
64#else
65#define HWY_COMPILER_GCC 0
66#endif
67
68// Clang or clang-cl, not GCC.
69#ifdef __clang__
70// In case of Apple LLVM (whose version number is unrelated to that of LLVM) or
71// an invalid version number, deduce it from the presence of warnings.
72// Adapted from https://github.com/simd-everywhere/simde/ simde-detect-clang.h.
73#if defined(__apple_build_version__) || __clang_major__ >= 999
74#if __has_warning("-Wbitwise-instead-of-logical")
75#define HWY_COMPILER_CLANG 1400
76#elif __has_warning("-Wreserved-identifier")
77#define HWY_COMPILER_CLANG 1300
78#elif __has_warning("-Wformat-insufficient-args")
79#define HWY_COMPILER_CLANG 1200
80#elif __has_warning("-Wimplicit-const-int-float-conversion")
81#define HWY_COMPILER_CLANG 1100
82#elif __has_warning("-Wmisleading-indentation")
83#define HWY_COMPILER_CLANG 1000
84#elif defined(__FILE_NAME__)
85#define HWY_COMPILER_CLANG 900
86#elif __has_warning("-Wextra-semi-stmt") || \
87 __has_builtin(__builtin_rotateleft32)
88#define HWY_COMPILER_CLANG 800
89// For reasons unknown, XCode 10.3 (Apple LLVM version 10.0.1) is apparently
90// based on Clang 7, but does not support the warning we test.
91// See https://en.wikipedia.org/wiki/Xcode#Toolchain_versions and
92// https://trac.macports.org/wiki/XcodeVersionInfo.
93#elif __has_warning("-Wc++98-compat-extra-semi") || \
94 (defined(__apple_build_version__) && __apple_build_version__ >= 10010000)
95#define HWY_COMPILER_CLANG 700
96#else // Anything older than 7.0 is not recommended for Highway.
97#define HWY_COMPILER_CLANG 600
98#endif // __has_warning chain
99#else // use normal version
100#define HWY_COMPILER_CLANG (__clang_major__ * 100 + __clang_minor__)
101#endif
102#else // Not clang
103#define HWY_COMPILER_CLANG 0
104#endif
105
106#if HWY_COMPILER_GCC && !HWY_COMPILER_CLANG
107#define HWY_COMPILER_GCC_ACTUAL HWY_COMPILER_GCC
108#else
109#define HWY_COMPILER_GCC_ACTUAL 0
110#endif
111
112// More than one may be nonzero, but we want at least one.
113#if 0 == (HWY_COMPILER_MSVC + HWY_COMPILER_CLANGCL + HWY_COMPILER_ICC + \
114 HWY_COMPILER_GCC + HWY_COMPILER_CLANG)
115#error "Unsupported compiler"
116#endif
117
118// We should only detect one of these (only clang/clangcl overlap)
119#if 1 < \
120 (!!HWY_COMPILER_MSVC + !!HWY_COMPILER_ICC + !!HWY_COMPILER_GCC_ACTUAL + \
121 !!(HWY_COMPILER_CLANGCL | HWY_COMPILER_CLANG))
122#error "Detected multiple compilers"
123#endif
124
125#ifdef __has_builtin
126#define HWY_HAS_BUILTIN(name) __has_builtin(name)
127#else
128#define HWY_HAS_BUILTIN(name) 0
129#endif
130
131#ifdef __has_attribute
132#define HWY_HAS_ATTRIBUTE(name) __has_attribute(name)
133#else
134#define HWY_HAS_ATTRIBUTE(name) 0
135#endif
136
137#ifdef __has_feature
138#define HWY_HAS_FEATURE(name) __has_feature(name)
139#else
140#define HWY_HAS_FEATURE(name) 0
141#endif
142
143//------------------------------------------------------------------------------
144// Architecture
145
146#if defined(__i386__) || defined(_M_IX86)
147#define HWY_ARCH_X86_32 1
148#else
149#define HWY_ARCH_X86_32 0
150#endif
151
152#if defined(__x86_64__) || defined(_M_X64)
153#define HWY_ARCH_X86_64 1
154#else
155#define HWY_ARCH_X86_64 0
156#endif
157
158#if HWY_ARCH_X86_32 && HWY_ARCH_X86_64
159#error "Cannot have both x86-32 and x86-64"
160#endif
161
162#if HWY_ARCH_X86_32 || HWY_ARCH_X86_64
163#define HWY_ARCH_X86 1
164#else
165#define HWY_ARCH_X86 0
166#endif
167
168#if defined(__powerpc64__) || defined(_M_PPC)
169#define HWY_ARCH_PPC 1
170#else
171#define HWY_ARCH_PPC 0
172#endif
173
174#if defined(__ARM_ARCH_ISA_A64) || defined(__aarch64__) || defined(_M_ARM64)
175#define HWY_ARCH_ARM_A64 1
176#else
177#define HWY_ARCH_ARM_A64 0
178#endif
179
180#if (defined(__ARM_ARCH) && __ARM_ARCH == 7) || (defined(_M_ARM) && _M_ARM == 7)
181#define HWY_ARCH_ARM_V7 1
182#else
183#define HWY_ARCH_ARM_V7 0
184#endif
185
186#if HWY_ARCH_ARM_A64 && HWY_ARCH_ARM_V7
187#error "Cannot have both A64 and V7"
188#endif
189
190// Any *supported* version of Arm, i.e. 7 or later
191#if HWY_ARCH_ARM_A64 || HWY_ARCH_ARM_V7
192#define HWY_ARCH_ARM 1
193#else
194#define HWY_ARCH_ARM 0
195#endif
196
197// Older than v7 (e.g. armel aka Arm v5), in which case we do not support SIMD.
198#if (defined(__arm__) || defined(_M_ARM)) && !HWY_ARCH_ARM
199#define HWY_ARCH_ARM_OLD 1
200#else
201#define HWY_ARCH_ARM_OLD 0
202#endif
203
204#if defined(__EMSCRIPTEN__) || defined(__wasm__) || defined(__WASM__)
205#define HWY_ARCH_WASM 1
206#else
207#define HWY_ARCH_WASM 0
208#endif
209
210#ifdef __riscv
211#define HWY_ARCH_RVV 1
212#else
213#define HWY_ARCH_RVV 0
214#endif
215
216// It is an error to detect multiple architectures at the same time, but OK to
217// detect none of the above.
218#if (HWY_ARCH_X86 + HWY_ARCH_PPC + HWY_ARCH_ARM + HWY_ARCH_ARM_OLD + \
219 HWY_ARCH_WASM + HWY_ARCH_RVV) > 1
220#error "Must not detect more than one architecture"
221#endif
222
223#if defined(_WIN32) || defined(_WIN64)
224#define HWY_OS_WIN 1
225#else
226#define HWY_OS_WIN 0
227#endif
228
229#if defined(linux) || defined(__linux__)
230#define HWY_OS_LINUX 1
231#else
232#define HWY_OS_LINUX 0
233#endif
234
235#endif // HIGHWAY_HWY_DETECT_COMPILER_ARCH_H_