Skip to content

Commit 35e3c09

Browse files
authored
Merge pull request #828 from byroot/check-builtin-cpu
Don't assume `__builtin_cpu_supports` exists
2 parents babdd2c + da87843 commit 35e3c09

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changes
22

3+
* Fix support for older compilers without `__builtin_cpu_supports`.
4+
35
### Unreleased
46

57
### 2025-05-23 (2.13.0)

ext/json/ext/simd/conf.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
case RbConfig::CONFIG['host_cpu']
22
when /^(arm|aarch64)/
33
# Try to compile a small program using NEON instructions
4-
header, type, init = 'arm_neon.h', 'uint8x16_t', 'vdupq_n_u8(32)'
4+
header, type, init, extra = 'arm_neon.h', 'uint8x16_t', 'vdupq_n_u8(32)', nil
55
when /^(x86_64|x64)/
6-
header, type, init = 'x86intrin.h', '__m128i', '_mm_set1_epi8(32)'
6+
header, type, init, extra = 'x86intrin.h', '__m128i', '_mm_set1_epi8(32)', 'if (__builtin_cpu_supports("sse2")) { printf("OK"); }'
77
end
88
if header
9-
have_header(header) && try_compile(<<~SRC)
10-
#{cpp_include(header)}
11-
int main(int argc, char **argv) {
12-
#{type} test = #{init};
13-
if (argc > 100000) printf("%p", &test);
14-
return 0;
15-
}
16-
SRC
17-
$defs.push("-DJSON_ENABLE_SIMD")
9+
if have_header(header) && try_compile(<<~SRC, '-Werror=implicit-function-declaration')
10+
#{cpp_include(header)}
11+
int main(int argc, char **argv) {
12+
#{type} test = #{init};
13+
#{extra}
14+
if (argc > 100000) printf("%p", &test);
15+
return 0;
16+
}
17+
SRC
18+
$defs.push("-DJSON_ENABLE_SIMD")
19+
else
20+
puts "Disable SIMD"
21+
end
1822
end
1923

2024
have_header('cpuid.h')

0 commit comments

Comments
 (0)