From 68ee9cf18871d7c71747b6d5538bf50330db9864 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 30 Jun 2025 21:17:33 +0900 Subject: [PATCH 1/7] Remove trailing spaces [ci skip] --- ext/json/ext/parser/parser.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index d77969482..9bf247039 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -908,7 +908,7 @@ static inline bool FORCE_INLINE string_scan(JSON_ParserState *state) { #ifdef HAVE_SIMD #if defined(HAVE_SIMD_NEON) - + uint64_t mask = 0; if (string_scan_simd_neon(&state->cursor, state->end, &mask)) { state->cursor += trailing_zeros64(mask) >> 2; From 8e775320b755bd2d0f428f5bf36019fb9cb3df74 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 30 Jun 2025 21:17:57 +0900 Subject: [PATCH 2/7] Use `load` simd/conf.rb When both extconf.rb of generator and parser are run in one process, the second `require_relative` does nothing. --- ext/json/ext/generator/extconf.rb | 2 +- ext/json/ext/parser/extconf.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/json/ext/generator/extconf.rb b/ext/json/ext/generator/extconf.rb index aaf02c77d..fb9afd07f 100644 --- a/ext/json/ext/generator/extconf.rb +++ b/ext/json/ext/generator/extconf.rb @@ -9,7 +9,7 @@ $defs << "-DJSON_DEBUG" if ENV["JSON_DEBUG"] if enable_config('generator-use-simd', default=!ENV["JSON_DISABLE_SIMD"]) - require_relative "../simd/conf.rb" + load __dir__ + "/../simd/conf.rb" end create_makefile 'json/ext/generator' diff --git a/ext/json/ext/parser/extconf.rb b/ext/json/ext/parser/extconf.rb index 0b62fd613..84049a7fe 100644 --- a/ext/json/ext/parser/extconf.rb +++ b/ext/json/ext/parser/extconf.rb @@ -9,7 +9,7 @@ append_cflags("-std=c99") if enable_config('parser-use-simd', default=!ENV["JSON_DISABLE_SIMD"]) - require_relative "../simd/conf.rb" + load __dir__ + "/../simd/conf.rb" end create_makefile 'json/ext/parser' From 58dc0aa938245e58adcf52be6aed7a223d1d09e2 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 30 Jun 2025 21:37:20 +0900 Subject: [PATCH 3/7] Refactor simd/conf.rb - compiler warnings Suppress warnings for old style function definition and unused variable. --- ext/json/ext/simd/conf.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ext/json/ext/simd/conf.rb b/ext/json/ext/simd/conf.rb index 6393cf789..ee56718be 100644 --- a/ext/json/ext/simd/conf.rb +++ b/ext/json/ext/simd/conf.rb @@ -3,8 +3,9 @@ if have_header('arm_neon.h') have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC') #include - int main() { + int main(int argc, char **argv) { uint8x16_t test = vdupq_n_u8(32); + if (argc > 100000) printf("%p", &test); return 0; } SRC @@ -14,8 +15,9 @@ if have_header('x86intrin.h') && have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC') #include - int main() { + int main(int argc, char **argv) { __m128i test = _mm_set1_epi8(32); + if (argc > 100000) printf("%p", &test); return 0; } SRC From 2211e30a59a31cabd6c8642164314a3d37bb0f1e Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 30 Jun 2025 21:38:56 +0900 Subject: [PATCH 4/7] Refactor simd/conf.rb - balance Align code for arm and x86_64 in parallel. --- ext/json/ext/simd/conf.rb | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/ext/json/ext/simd/conf.rb b/ext/json/ext/simd/conf.rb index ee56718be..deaac412d 100644 --- a/ext/json/ext/simd/conf.rb +++ b/ext/json/ext/simd/conf.rb @@ -1,4 +1,5 @@ -if RbConfig::CONFIG['host_cpu'] =~ /^(arm.*|aarch64.*)/ +case RbConfig::CONFIG['host_cpu'] +when /^(arm|aarch64)/ # Try to compile a small program using NEON instructions if have_header('arm_neon.h') have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC') @@ -8,20 +9,20 @@ if (argc > 100000) printf("%p", &test); return 0; } - SRC - $defs.push("-DJSON_ENABLE_SIMD") + SRC + $defs.push("-DJSON_ENABLE_SIMD") end -end - -if have_header('x86intrin.h') && have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC') - #include - int main(int argc, char **argv) { - __m128i test = _mm_set1_epi8(32); - if (argc > 100000) printf("%p", &test); - return 0; - } - SRC +when /^(x86_64|x64)/ + if have_header('x86intrin.h') && have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC') + #include + int main(int argc, char **argv) { + __m128i test = _mm_set1_epi8(32); + if (argc > 100000) printf("%p", &test); + return 0; + } + SRC $defs.push("-DJSON_ENABLE_SIMD") + end end have_header('cpuid.h') From fdbb6062c2608565f2901bbd4f0c82dce92fea84 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 30 Jun 2025 21:43:13 +0900 Subject: [PATCH 5/7] Refactor simd/conf.rb - conditions to enable See the results of `have_type` and `try_compile` in addition to `have_header` for NEON as well as x86_64. The former results were just ignored, and `HAVE_TYPE_` macros are unused too. --- ext/json/ext/simd/conf.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ext/json/ext/simd/conf.rb b/ext/json/ext/simd/conf.rb index deaac412d..3b3cf68af 100644 --- a/ext/json/ext/simd/conf.rb +++ b/ext/json/ext/simd/conf.rb @@ -1,8 +1,8 @@ case RbConfig::CONFIG['host_cpu'] when /^(arm|aarch64)/ # Try to compile a small program using NEON instructions - if have_header('arm_neon.h') - have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC') + if have_header('arm_neon.h') && + have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC') #include int main(int argc, char **argv) { uint8x16_t test = vdupq_n_u8(32); @@ -13,7 +13,8 @@ $defs.push("-DJSON_ENABLE_SIMD") end when /^(x86_64|x64)/ - if have_header('x86intrin.h') && have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC') + if have_header('x86intrin.h') && + have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC') #include int main(int argc, char **argv) { __m128i test = _mm_set1_epi8(32); From b08e1ca2c104e400756807787225c37c9a9823e8 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 30 Jun 2025 21:53:27 +0900 Subject: [PATCH 6/7] Refactor simd/conf.rb - unnecessary `have_type` Remove `have_type` calls because the next `try_compile` calls check those types. --- ext/json/ext/simd/conf.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/json/ext/simd/conf.rb b/ext/json/ext/simd/conf.rb index 3b3cf68af..fa5b97801 100644 --- a/ext/json/ext/simd/conf.rb +++ b/ext/json/ext/simd/conf.rb @@ -2,7 +2,7 @@ when /^(arm|aarch64)/ # Try to compile a small program using NEON instructions if have_header('arm_neon.h') && - have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC') + try_compile(<<~'SRC') #include int main(int argc, char **argv) { uint8x16_t test = vdupq_n_u8(32); @@ -14,7 +14,7 @@ end when /^(x86_64|x64)/ if have_header('x86intrin.h') && - have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC') + try_compile(<<~'SRC') #include int main(int argc, char **argv) { __m128i test = _mm_set1_epi8(32); From 1a768d91796cefffa6e273b7362e462a1cbc4c7a Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 30 Jun 2025 22:34:14 +0900 Subject: [PATCH 7/7] Refactor simd/conf.rb - duplicate code Integrate duplicate code by extracting headers, types and initialization code. --- ext/json/ext/simd/conf.rb | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/ext/json/ext/simd/conf.rb b/ext/json/ext/simd/conf.rb index fa5b97801..8e7d8ee26 100644 --- a/ext/json/ext/simd/conf.rb +++ b/ext/json/ext/simd/conf.rb @@ -1,29 +1,20 @@ case RbConfig::CONFIG['host_cpu'] when /^(arm|aarch64)/ # Try to compile a small program using NEON instructions - if have_header('arm_neon.h') && - try_compile(<<~'SRC') - #include - int main(int argc, char **argv) { - uint8x16_t test = vdupq_n_u8(32); - if (argc > 100000) printf("%p", &test); - return 0; - } - SRC - $defs.push("-DJSON_ENABLE_SIMD") - end + header, type, init = 'arm_neon.h', 'uint8x16_t', 'vdupq_n_u8(32)' when /^(x86_64|x64)/ - if have_header('x86intrin.h') && - try_compile(<<~'SRC') - #include - int main(int argc, char **argv) { - __m128i test = _mm_set1_epi8(32); - if (argc > 100000) printf("%p", &test); - return 0; - } - SRC - $defs.push("-DJSON_ENABLE_SIMD") - end + header, type, init = 'x86intrin.h', '__m128i', '_mm_set1_epi8(32)' +end +if header + have_header(header) && try_compile(<<~SRC) + #{cpp_include(header)} + int main(int argc, char **argv) { + #{type} test = #{init}; + if (argc > 100000) printf("%p", &test); + return 0; + } + SRC + $defs.push("-DJSON_ENABLE_SIMD") end have_header('cpuid.h')