From e652b4b75f86f82a764cb0986c068581ac1e5360 Mon Sep 17 00:00:00 2001 From: yeweihong Date: Mon, 27 Jul 2026 12:03:04 +0800 Subject: [PATCH 1/3] feat: add SVE2-256 architecture support --- BUILD.bazel | 16 ++++++++ CMakeLists.txt | 5 +++ cmake/set_arch_flags.cmake | 2 + include/sonic/internal/arch/simd_dispatch.h | 3 ++ .../sonic/internal/arch/sonic_cpu_feature.h | 3 ++ include/sonic/internal/arch/sve2-256/base.h | 37 +++++++++++++++++++ include/sonic/internal/arch/sve2-256/itoa.h | 30 +++++++++++++++ include/sonic/internal/arch/sve2-256/quote.h | 30 +++++++++++++++ include/sonic/internal/arch/sve2-256/skip.h | 35 ++++++++++++++++++ .../sonic/internal/arch/sve2-256/str2int.h | 29 +++++++++++++++ tests/stringblock_test.cpp | 5 ++- 11 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 include/sonic/internal/arch/sve2-256/base.h create mode 100644 include/sonic/internal/arch/sve2-256/itoa.h create mode 100644 include/sonic/internal/arch/sve2-256/quote.h create mode 100644 include/sonic/internal/arch/sve2-256/skip.h create mode 100644 include/sonic/internal/arch/sve2-256/str2int.h diff --git a/BUILD.bazel b/BUILD.bazel index 194243c2..cc818079 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -10,6 +10,7 @@ sse_copts = ['-msse', '-msse2', '-msse4.1', '-msse4.2','-mpclmul'] arm_copts = ['-march=armv8-a'] apple_silicon_copts = [ '-mcpu=apple-m1' ] sve2_copts = ['-march=armv8-a+sve2', '-msve-vector-bits=128'] +sve2_256_copts = ['-march=armv8-a+sve2', '-msve-vector-bits=256'] riscv64_copts = ['-march=rv64gc'] riscv64_zbb_copts = ['-march=rv64gc_zbb'] default_copts = ['-march=native'] @@ -34,6 +35,12 @@ string_flag( values = ["default", "haswell", "westmere", "arm", "sve2", "riscv64", "riscv64_zbb"], ) +string_flag( + name = "sve_vector_bits", + build_setting_default = "128", + values = ["128", "256"], +) + string_flag( name = "sonic_dispatch", build_setting_default = "static", @@ -63,6 +70,14 @@ config_setting( flag_values = {":sonic_arch": "sve2"}, ) +config_setting( + name = "sve2_256_build", + flag_values = { + ":sonic_arch": "sve2", + ":sve_vector_bits": "256", + }, +) + config_setting( name = "riscv64_build", flag_values = {":sonic_arch": "riscv64"}, @@ -112,6 +127,7 @@ config_setting( auto_arch_copts = select({ "arm_build": arm_copts, + "sve2_256_build": sve2_256_copts, "sve2_build": sve2_copts, "sse_build": sse_copts, "avx2_build": avx2_copts, diff --git a/CMakeLists.txt b/CMakeLists.txt index 69bd55c3..36ca3606 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,13 @@ option(BUILD_UNITTEST "Build unittest." ON) option(BUILD_FUZZ "Build fuzz." OFF) option(BUILD_BENCH "Build benchmark." OFF) option(ENABLE_SVE2_128 "Build for Arm SVE2 with 128 bit vector size" OFF) +option(ENABLE_SVE2_256 "Build for Arm SVE2 with 256 bit vector size" OFF) option(ENABLE_RISCV_ZBB "Enable RISC-V Zbb extension for bit manipulation optimizations" OFF) +if(ENABLE_SVE2_128 AND ENABLE_SVE2_256) + message(FATAL_ERROR "ENABLE_SVE2_128 and ENABLE_SVE2_256 are mutually exclusive") +endif() + set(CMAKE_CXX_EXTENSIONS OFF) if(BUILD_UNITTEST) add_subdirectory(tests) diff --git a/cmake/set_arch_flags.cmake b/cmake/set_arch_flags.cmake index 8c679989..9dafbfbe 100644 --- a/cmake/set_arch_flags.cmake +++ b/cmake/set_arch_flags.cmake @@ -8,6 +8,8 @@ function(set_arch_flags target arch) else() if(ENABLE_SVE2_128) target_compile_options(${target} PRIVATE -march=armv8-a+sve2 -msve-vector-bits=128) + elseif(ENABLE_SVE2_256) + target_compile_options(${target} PRIVATE -march=armv8-a+sve2 -msve-vector-bits=256) else() target_compile_options(${target} PRIVATE -march=armv8-a) endif() diff --git a/include/sonic/internal/arch/simd_dispatch.h b/include/sonic/internal/arch/simd_dispatch.h index a8ee07f7..d80b3303 100644 --- a/include/sonic/internal/arch/simd_dispatch.h +++ b/include/sonic/internal/arch/simd_dispatch.h @@ -36,6 +36,9 @@ #if defined(SONIC_HAVE_SVE2_128) #define SONIC_USING_ARCH_FUNC(func) using sve2_128::func #define INCLUDE_ARCH_FILE(file) SONIC_STRINGIFY(sve2-128/file) +#elif defined(SONIC_HAVE_SVE2_256) +#define SONIC_USING_ARCH_FUNC(func) using sve2_256::func +#define INCLUDE_ARCH_FILE(file) SONIC_STRINGIFY(sve2-256/file) #elif defined(SONIC_HAVE_NEON) #define SONIC_USING_ARCH_FUNC(func) using neon::func #define INCLUDE_ARCH_FILE(file) SONIC_STRINGIFY(neon/file) diff --git a/include/sonic/internal/arch/sonic_cpu_feature.h b/include/sonic/internal/arch/sonic_cpu_feature.h index 17222fb9..4aebfbab 100644 --- a/include/sonic/internal/arch/sonic_cpu_feature.h +++ b/include/sonic/internal/arch/sonic_cpu_feature.h @@ -45,6 +45,9 @@ #if defined(__ARM_FEATURE_SVE2) && (__ARM_FEATURE_SVE_BITS == 128) #define SONIC_HAVE_SVE2_128 #endif +#if defined(__ARM_FEATURE_SVE2) && (__ARM_FEATURE_SVE_BITS == 256) +#define SONIC_HAVE_SVE2_256 +#endif #elif defined(__riscv) && defined(__riscv_xlen) && (__riscv_xlen == 64) #define SONIC_IS_RISCV 1 #if defined(__riscv_zbb) diff --git a/include/sonic/internal/arch/sve2-256/base.h b/include/sonic/internal/arch/sve2-256/base.h new file mode 100644 index 00000000..f075fea5 --- /dev/null +++ b/include/sonic/internal/arch/sve2-256/base.h @@ -0,0 +1,37 @@ +// Copyright 2018-2019 The simdjson authors + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This file may have been modified by ByteDance authors. All ByteDance +// Modifications are Copyright 2022 ByteDance Authors. + +#pragma once + +#include "../common/arm_common/base.h" + +namespace sonic_json { +namespace internal { +namespace sve2_256 { + +using sonic_json::internal::arm_common::ClearLowestBit; +using sonic_json::internal::arm_common::CountOnes; +using sonic_json::internal::arm_common::InlinedMemcmp; +using sonic_json::internal::arm_common::InlinedMemcmpEq; +using sonic_json::internal::arm_common::LeadingZeroes; +using sonic_json::internal::arm_common::PrefixXor; +using sonic_json::internal::arm_common::TrailingZeroes; +using sonic_json::internal::arm_common::Xmemcpy; + +} // namespace sve2_256 +} // namespace internal +} // namespace sonic_json diff --git a/include/sonic/internal/arch/sve2-256/itoa.h b/include/sonic/internal/arch/sve2-256/itoa.h new file mode 100644 index 00000000..4c082d35 --- /dev/null +++ b/include/sonic/internal/arch/sve2-256/itoa.h @@ -0,0 +1,30 @@ +/* + * Copyright 2022 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "../common/arm_common/itoa.h" + +namespace sonic_json { +namespace internal { +namespace sve2_256 { + +using sonic_json::internal::arm_common::Utoa_16; +using sonic_json::internal::arm_common::Utoa_8; + +} // namespace sve2_256 +} // namespace internal +} // namespace sonic_json diff --git a/include/sonic/internal/arch/sve2-256/quote.h b/include/sonic/internal/arch/sve2-256/quote.h new file mode 100644 index 00000000..803e137c --- /dev/null +++ b/include/sonic/internal/arch/sve2-256/quote.h @@ -0,0 +1,30 @@ +/* + * Copyright 2022 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "../neon/quote.h" + +namespace sonic_json { +namespace internal { +namespace sve2_256 { + +using sonic_json::internal::neon::parseStringInplace; +using sonic_json::internal::neon::Quote; + +} // namespace sve2_256 +} // namespace internal +} // namespace sonic_json diff --git a/include/sonic/internal/arch/sve2-256/skip.h b/include/sonic/internal/arch/sve2-256/skip.h new file mode 100644 index 00000000..d1da3b6e --- /dev/null +++ b/include/sonic/internal/arch/sve2-256/skip.h @@ -0,0 +1,35 @@ +/* + * Copyright 2022 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "../neon/skip.h" + +namespace sonic_json { +namespace internal { +namespace sve2_256 { + +using sonic_json::internal::neon::EqBytes4; +using sonic_json::internal::neon::SkipLiteral; +using sonic_json::internal::neon::GetNextToken; +using sonic_json::internal::neon::SkipString; +using sonic_json::internal::neon::SkipContainer; +using sonic_json::internal::neon::skip_space; +using sonic_json::internal::neon::skip_space_safe; + +} // namespace sve2_256 +} // namespace internal +} // namespace sonic_json diff --git a/include/sonic/internal/arch/sve2-256/str2int.h b/include/sonic/internal/arch/sve2-256/str2int.h new file mode 100644 index 00000000..08fc9e27 --- /dev/null +++ b/include/sonic/internal/arch/sve2-256/str2int.h @@ -0,0 +1,29 @@ +/* + * Copyright 2022 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "../common/arm_common/str2int.h" + +namespace sonic_json { +namespace internal { +namespace sve2_256 { + +using sonic_json::internal::arm_common::simd_str2int; + +} // namespace sve2_256 +} // namespace internal +} // namespace sonic_json diff --git a/tests/stringblock_test.cpp b/tests/stringblock_test.cpp index 16fcea28..56d6c1f4 100644 --- a/tests/stringblock_test.cpp +++ b/tests/stringblock_test.cpp @@ -1,7 +1,8 @@ // Copyright 2022 ByteDance Inc. // Unit test for StringBlock::Find control character handling -#if defined(__aarch64__) && defined(__ARM_FEATURE_SVE2) +#if defined(__aarch64__) && defined(__ARM_FEATURE_SVE2) && \ + (__ARM_FEATURE_SVE_BITS == 128) #include #include "sonic/internal/arch/sve2-128/unicode.h" @@ -72,4 +73,4 @@ TEST(StringBlockTest, IgnoresHighBitCharacters) { EXPECT_EQ(block.quote_index, 16u); EXPECT_EQ(block.bs_index, 16u); } -#endif // defined(__aarch64__) && defined(__ARM_FEATURE_SVE2) +#endif // defined(__aarch64__) && defined(__ARM_FEATURE_SVE2) && 128-bit SVE From 22c2db2777affd228fd48e2f2e0b10f2f373f75d Mon Sep 17 00:00:00 2001 From: yeweihong Date: Mon, 27 Jul 2026 12:03:04 +0800 Subject: [PATCH 2/3] feat: add SVE2-256 serialization optimization --- include/sonic/internal/arch/sve2-256/base.h | 27 ++++++ include/sonic/internal/arch/sve2-256/itoa.h | 86 +++++++++++++++++++- include/sonic/internal/arch/sve2-256/quote.h | 48 ++++++++++- 3 files changed, 157 insertions(+), 4 deletions(-) diff --git a/include/sonic/internal/arch/sve2-256/base.h b/include/sonic/internal/arch/sve2-256/base.h index f075fea5..ac399a7b 100644 --- a/include/sonic/internal/arch/sve2-256/base.h +++ b/include/sonic/internal/arch/sve2-256/base.h @@ -17,12 +17,16 @@ #pragma once +#include + #include "../common/arm_common/base.h" namespace sonic_json { namespace internal { namespace sve2_256 { +constexpr size_t kBlockBytes = 32; + using sonic_json::internal::arm_common::ClearLowestBit; using sonic_json::internal::arm_common::CountOnes; using sonic_json::internal::arm_common::InlinedMemcmp; @@ -32,6 +36,29 @@ using sonic_json::internal::arm_common::PrefixXor; using sonic_json::internal::arm_common::TrailingZeroes; using sonic_json::internal::arm_common::Xmemcpy; +template +sonic_force_inline svbool_t copy_get_escaped_mask_predicate(svbool_t pg, const char *src, char *dst) +{ + svuint8_t v = svld1_u8(pg, reinterpret_cast(src)); + svst1_u8(pg, reinterpret_cast(dst), v); + svbool_t m1 = svcmpeq_n_u8(pg, v, '\\'); + svbool_t m2 = svcmpeq_n_u8(pg, v, '"'); + svbool_t m3 = svcmplt_n_u8(pg, v, '\x20'); + svbool_t m4 = svorr_b_z(pg, m1, m2); + svbool_t m5 = svorr_b_z(pg, m3, m4); + if constexpr (EscapeEmoji) { + svbool_t m_emoji = svcmpge_n_u8(pg, v, (uint8_t)0xF0); + m5 = svorr_b_z(pg, m5, m_emoji); + } + return m5; +} + +// The function returns the index of first (to the rigth) active elem +sonic_force_inline int get_first_active_index(svbool_t input, svbool_t predicate) +{ + return svcntp_b8(predicate, svbrkb_b_z(predicate, input)); +} + } // namespace sve2_256 } // namespace internal } // namespace sonic_json diff --git a/include/sonic/internal/arch/sve2-256/itoa.h b/include/sonic/internal/arch/sve2-256/itoa.h index 4c082d35..82785d0b 100644 --- a/include/sonic/internal/arch/sve2-256/itoa.h +++ b/include/sonic/internal/arch/sve2-256/itoa.h @@ -14,16 +14,96 @@ * limitations under the License. */ +// This file has been modified by Huawei authors. +// Modifications are Copyright (c) 2026 Huawei Technologies Co., Ltd. + #pragma once -#include "../common/arm_common/itoa.h" +#include namespace sonic_json { namespace internal { namespace sve2_256 { -using sonic_json::internal::arm_common::Utoa_16; -using sonic_json::internal::arm_common::Utoa_8; +#define UTO8_FACTOR 10000 +#define UTO16_FACTOR 100000000 + +sonic_force_inline svuint16_t utoa_8_helper(uint16_t num1, uint16_t num2) +{ + svbool_t ptrue_32 = svptrue_b32(); + svuint32_t v1 = svdup_n_u32(num1); + svuint32_t v2 = svdup_n_u32(num2); + svuint32_t kVecDiv = svunpklo_u32(svreinterpret_u16_u64(svdup_n_u64(0x80003334147b20c5))); + svuint32_t v00 = svsplice_u32(svptrue_pat_b32(SV_VL4), v1, v2); + svuint32_t v01 = svmul_u32_x(ptrue_32, v00, kVecDiv); + svuint32_t v02 = svlsr_n_u32_x(ptrue_32, v01, 14); + svuint32_t kVecShift = svunpklo_u32(svreinterpret_u16_u64(svdup_n_u64(0x8000200008000080))); + svuint32_t v03 = svmul_u32_x(ptrue_32, v02, kVecShift); + return svreinterpret_u16_u32(v03); +} + +sonic_force_inline svuint16_t utoa_sve(uint32_t num) +{ + svbool_t ptrue_16 = svptrue_b16(); + svuint16_t help_vec = utoa_8_helper(num / UTO8_FACTOR, num % UTO8_FACTOR); + svuint16_t v10 = svuzp2_u16(help_vec, svdup_n_u16(0)); + svuint16_t v11 = svmul_n_u16_x(ptrue_16, v10, 0xa); + svuint16_t v12 = svreinterpret_u16_u64(svlsl_n_u64_x(svptrue_b64(), svreinterpret_u64_u16(v11), 0x10)); + svuint16_t v13 = svsub_u16_x(ptrue_16, v10, v12); + return v13; +} + +sonic_force_inline svuint16_t utoa_helper16(uint16_t num1, uint16_t num2, uint16_t num3, uint16_t num4) +{ + svbool_t ptrue_32 = svptrue_b32(); + svuint32_t v1 = svdup_n_u32(num1); + svuint32_t v2 = svdup_n_u32(num2); + svuint32_t v3 = svdup_n_u32(num3); + svuint32_t v4 = svdup_n_u32(num4); + + svuint32_t kVecDiv = svunpklo_u32(svreinterpret_u16_u64(svdup_n_u64(0x80003334147b20c5))); + svuint32_t v00 = svuzp1_u32(v1, v2); + svuint32_t v10 = svuzp1_u32(v3, v4); + svuint32_t v01 = svmul_u32_x(ptrue_32, v00, kVecDiv); + svuint32_t v11 = svmul_u32_x(ptrue_32, v10, kVecDiv); + svuint32_t v02 = svlsr_n_u32_x(ptrue_32, v01, 0xe); + svuint32_t v12 = svlsr_n_u32_x(ptrue_32, v11, 0xe); + svuint32_t kVecShift = svunpklo_u32(svreinterpret_u16_u64(svdup_n_u64(0x8000200008000080))); + svuint32_t v03 = svmul_u32_x(ptrue_32, v02, kVecShift); + svuint32_t v13 = svmul_u32_x(ptrue_32, v12, kVecShift); + svuint16_t res = svuzp2_u16(svreinterpret_u16_u32(v03), svreinterpret_u16_u32(v13)); + return res; +} + +sonic_force_inline svuint16_t utoa_sve16(uint32_t num1, uint32_t num2) +{ + svbool_t ptrue_16 = svptrue_b16(); + svuint16_t help_vec = utoa_helper16(num1 / UTO8_FACTOR, num1 % UTO8_FACTOR, num2 / UTO8_FACTOR, num2 % UTO8_FACTOR); + svuint16_t v11 = svmul_n_u16_x(ptrue_16, help_vec, 0xa); + svuint16_t v12 = svreinterpret_u16_u64(svlsl_n_u64_x(svptrue_b64(), svreinterpret_u64_u16(v11), 0x10)); + svuint16_t v13 = svsub_u16_x(ptrue_16, help_vec, v12); + return v13; +} + +static sonic_force_inline char *Utoa_8(uint32_t val, char *out) { + /* convert to digits */ + svuint16_t v0 = utoa_sve(val); + svuint16_t v3 = svadd_u16_x(svptrue_b16(), v0, svdup_n_u16((uint16_t)('0'))); + svuint8_t v4 = svuzp1_u8(svreinterpret_u8_u16(v3), svdup_n_u8(0)); + // /* store high 64 bits */ + svst1_u8(svptrue_pat_b8(SV_VL8), (uint8_t *)out, v4); + return out + 0x8; +} + +static sonic_force_inline char *Utoa_16(uint64_t val, char *out) +{ + /* remaining digits */ + svuint16_t v0 = utoa_sve16((uint32_t)(val / UTO16_FACTOR), (uint32_t)(val % UTO16_FACTOR)); + svuint16_t v3 = svadd_u16_x(svptrue_b16(), v0, svdup_n_u16((uint16_t)('0'))); + svuint8_t v4 = svuzp1_u8(svreinterpret_u8_u16(v3), svdup_n_u8(0)); + svst1_u8(svptrue_pat_b8(SV_VL16), (uint8_t *)out, v4); + return out + 0x10; +} } // namespace sve2_256 } // namespace internal diff --git a/include/sonic/internal/arch/sve2-256/quote.h b/include/sonic/internal/arch/sve2-256/quote.h index 803e137c..0b8ec08a 100644 --- a/include/sonic/internal/arch/sve2-256/quote.h +++ b/include/sonic/internal/arch/sve2-256/quote.h @@ -14,17 +14,63 @@ * limitations under the License. */ +// This file has been modified by Huawei authors. +// Modifications are Copyright (c) 2026 Huawei Technologies Co., Ltd. #pragma once #include "../neon/quote.h" +#include "base.h" + +#define MOVE_N_CHARS(src, N) \ + do { \ + (src) += (N); \ + nb -= (N); \ + dst += (N); \ + } while (0) namespace sonic_json { namespace internal { namespace sve2_256 { using sonic_json::internal::neon::parseStringInplace; -using sonic_json::internal::neon::Quote; + +template +sonic_force_inline char *Quote(const char *src, size_t nb, char *dst) +{ + constexpr bool EscapeEmoji = + serializeFlags & SerializeFlags::kSerializeEscapeEmoji; + *dst++ = '"'; + sonic_assert(nb < (1ULL << 0x20)); + constexpr size_t svelen = kBlockBytes; + svbool_t ptrue = svptrue_b8(); + while (nb > svelen) { + svbool_t mask = copy_get_escaped_mask_predicate(ptrue, src, dst); + if (svptest_any(ptrue, mask)) { + auto cn = get_first_active_index(mask, ptrue); + MOVE_N_CHARS(src, cn); + DoEscape(src, dst, nb); + } else { + MOVE_N_CHARS(src, svelen); + } + } + while (nb > 0) { + svbool_t predicate = svwhilelt_b8_u64(0, nb); + svbool_t mask = copy_get_escaped_mask_predicate(predicate, src, dst); + if (svptest_any(predicate, mask)) { + auto cn = get_first_active_index(mask, predicate); + MOVE_N_CHARS(src, cn); + DoEscape(src, dst, nb); + } else { + auto active_elems = svcntp_b8(predicate, predicate); + MOVE_N_CHARS(src, active_elems); + } + } + *dst++ = '"'; + return dst; +} } // namespace sve2_256 } // namespace internal } // namespace sonic_json + +#undef MOVE_N_CHARS From 5599851015561f33403879b18882421fed0e459d Mon Sep 17 00:00:00 2001 From: yeweihong Date: Mon, 27 Jul 2026 12:03:04 +0800 Subject: [PATCH 3/3] feat: add SVE2-256 deserialization optimizations --- include/sonic/dom/parser.h | 2 +- include/sonic/internal/arch/sve2-256/base.h | 65 ++++- include/sonic/internal/arch/sve2-256/quote.h | 235 +++++++++++++++- include/sonic/internal/arch/sve2-256/skip.h | 251 +++++++++++++++++- .../sonic/internal/arch/sve2-256/str2int.h | 127 ++++++++- tests/parsenumber_test.cpp | 35 +++ tests/skip_test.cpp | 5 + 7 files changed, 705 insertions(+), 15 deletions(-) diff --git a/include/sonic/dom/parser.h b/include/sonic/dom/parser.h index 05c2e163..dd2cadb7 100644 --- a/include/sonic/dom/parser.h +++ b/include/sonic/dom/parser.h @@ -708,7 +708,7 @@ class Parser { } template - void parsePrimitives(SAX &sax) { + sonic_force_inline void parsePrimitives(SAX &sax) { bool ok = true; switch (json_buf_[pos_ - 1]) { case '0': diff --git a/include/sonic/internal/arch/sve2-256/base.h b/include/sonic/internal/arch/sve2-256/base.h index ac399a7b..f23c7d79 100644 --- a/include/sonic/internal/arch/sve2-256/base.h +++ b/include/sonic/internal/arch/sve2-256/base.h @@ -15,10 +15,14 @@ // This file may have been modified by ByteDance authors. All ByteDance // Modifications are Copyright 2022 ByteDance Authors. +// This file has been modified by Huawei authors. +// Modifications are Copyright (c) 2026 Huawei Technologies Co., Ltd. + #pragma once +#include #include - +#include "sonic/macro.h" #include "../common/arm_common/base.h" namespace sonic_json { @@ -34,7 +38,6 @@ using sonic_json::internal::arm_common::InlinedMemcmpEq; using sonic_json::internal::arm_common::LeadingZeroes; using sonic_json::internal::arm_common::PrefixXor; using sonic_json::internal::arm_common::TrailingZeroes; -using sonic_json::internal::arm_common::Xmemcpy; template sonic_force_inline svbool_t copy_get_escaped_mask_predicate(svbool_t pg, const char *src, char *dst) @@ -59,6 +62,64 @@ sonic_force_inline int get_first_active_index(svbool_t input, svbool_t predicate return svcntp_b8(predicate, svbrkb_b_z(predicate, input)); } +sonic_force_inline int get_first_active_index(svbool_t input) +{ + return get_first_active_index(input, svptrue_b8()); +} + +sonic_force_inline uint64_t predicate_to_bits_u64(svbool_t pred_lo, svbool_t pred_hi) +{ + uint64_t bits; + uint32_t *lo = reinterpret_cast(&bits); + __asm__ volatile( + "str %2, [%0]\n\t" + "str %3, [%1]" + : : "r"(lo), "r"(lo + 1), "Upl"(pred_lo), "Upl"(pred_hi) : "memory" + ); + return bits; +} + +template +sonic_force_inline void Xmemcpy(void* dst_, const void* src_, size_t chunks) { + std::memcpy(dst_, src_, chunks * ChunkSize); +} + +template <> +sonic_force_inline void Xmemcpy<32>(void* dst_, const void* src_, + size_t chunks) { + uint8_t* dst = reinterpret_cast(dst_); + const uint8_t* src = reinterpret_cast(src_); + size_t blocks = chunks / 2; + svbool_t ptrue = svptrue_b8(); + for (size_t i = 0; i < blocks; i++) { + svuint8x2_t s_input = svld2_u8(ptrue, src); + svst2_u8(ptrue, dst, s_input); + src += 64; + dst += 64; + } + if(chunks % 2) { + std::memcpy(dst, src, 32); + } +} + +template <> +sonic_force_inline void Xmemcpy<16>(void* dst_, const void* src_, + size_t chunks) { + uint8_t* dst = reinterpret_cast(dst_); + const uint8_t* src = reinterpret_cast(src_); + size_t blocks = chunks / 2; + svbool_t ptrue = svptrue_b8(); + for (size_t i = 0; i < blocks; i++) { + svuint8_t s_input = svld1_u8(ptrue, src); + svst1_u8(ptrue, dst, s_input); + src += 32; + dst += 32; + } + if(chunks % 2) { + std::memcpy(dst, src, 16); + } +} + } // namespace sve2_256 } // namespace internal } // namespace sonic_json diff --git a/include/sonic/internal/arch/sve2-256/quote.h b/include/sonic/internal/arch/sve2-256/quote.h index 0b8ec08a..a646a579 100644 --- a/include/sonic/internal/arch/sve2-256/quote.h +++ b/include/sonic/internal/arch/sve2-256/quote.h @@ -18,7 +18,8 @@ // Modifications are Copyright (c) 2026 Huawei Technologies Co., Ltd. #pragma once -#include "../neon/quote.h" +#include "../common/quote_common.h" +#include "../common/unicode_common.h" #include "base.h" #define MOVE_N_CHARS(src, N) \ @@ -32,7 +33,116 @@ namespace sonic_json { namespace internal { namespace sve2_256 { -using sonic_json::internal::neon::parseStringInplace; +constexpr size_t kScanWindowBytes = kBlockBytes * 2; + +enum class StringSpecialKind : uint8_t { + None = 0, + Quote, + Unescaped, + Backslash, +}; + +struct StringScanBlock { + size_t index; + StringSpecialKind kind; + + sonic_force_inline bool HasSpecial() const { + return kind != StringSpecialKind::None; + } + + sonic_force_inline svbool_t Prefix() const { + return svwhilelt_b8_u64(0, index); + } +}; + +struct StringScanWindow { + size_t index; + StringSpecialKind kind; + bool second; + + sonic_force_inline bool HasSpecial() const { + return kind != StringSpecialKind::None; + } + + sonic_force_inline size_t LocalIndex(size_t block_len) const { + return second ? index - block_len : index; + } + + sonic_force_inline svbool_t Prefix(size_t block_len) const { + return svwhilelt_b8_u64(0, LocalIndex(block_len)); + } +}; + +sonic_force_inline StringScanBlock ClassifyStringBlock(svuint8_t v, svbool_t ptrue) { + svbool_t quote_bits = svcmpeq_n_u8(ptrue, v, (uint8_t)('"')); + svbool_t bs_bits = svcmpeq_n_u8(ptrue, v, (uint8_t)('\\')); + bool has_quote = svptest_any(ptrue, quote_bits); + bool has_bs = svptest_any(ptrue, bs_bits); + + if (sonic_likely(has_quote && !has_bs)) { + svbool_t escape_bits = svcmple_n_u8(ptrue, v, (uint8_t)('\x1f')); + svbool_t quote_prefix = svbrkb_b_z(ptrue, quote_bits); + if (svptest_any(quote_prefix, escape_bits)) { + svbool_t escape_before_quote = svand_b_z(ptrue, quote_prefix, escape_bits); + return {static_cast(get_first_active_index(escape_before_quote, ptrue)), + StringSpecialKind::Unescaped}; + } + return {static_cast(svcntp_b8(ptrue, quote_prefix)), + StringSpecialKind::Quote}; + } + + svbool_t escape_bits = svcmple_n_u8(ptrue, v, (uint8_t)('\x1f')); + if (!has_quote && !has_bs) { + if (!svptest_any(ptrue, escape_bits)) { + return {0, StringSpecialKind::None}; + } + return {static_cast(get_first_active_index(escape_bits, ptrue)), + StringSpecialKind::Unescaped}; + } + + if (has_quote) { + svbool_t quote_prefix = svbrkb_b_z(ptrue, quote_bits); + svbool_t bs_escape = svorr_b_z(ptrue, bs_bits, escape_bits); + svbool_t special_before_quote = svand_b_z(ptrue, quote_prefix, bs_escape); + if (!svptest_any(ptrue, special_before_quote)) { + return {static_cast(svcntp_b8(ptrue, quote_prefix)), + StringSpecialKind::Quote}; + } + + size_t special_index = get_first_active_index(special_before_quote, ptrue); + if (svptest_any(ptrue, bs_bits) && + static_cast(get_first_active_index(bs_bits, ptrue)) == special_index) { + return {special_index, StringSpecialKind::Backslash}; + } + return {special_index, StringSpecialKind::Unescaped}; + } + + svbool_t bs_prefix = svbrkb_b_z(ptrue, bs_bits); + if (svptest_any(bs_prefix, escape_bits)) { + svbool_t escape_before_bs = svand_b_z(ptrue, bs_prefix, escape_bits); + return {static_cast(get_first_active_index(escape_before_bs, ptrue)), + StringSpecialKind::Unescaped}; + } + return {static_cast(svcntp_b8(ptrue, bs_prefix)), + StringSpecialKind::Backslash}; +} + +sonic_force_inline StringScanWindow ScanStringWindow2Blocks(const uint8_t *src, svbool_t ptrue, + svuint8_t &v0, svuint8_t &v1) { + v0 = svld1_u8(ptrue, reinterpret_cast(src)); + StringScanBlock block = ClassifyStringBlock(v0, ptrue); + if (block.HasSpecial()) { + return {block.index, block.kind, false}; + } + + v1 = svld1_u8(ptrue, reinterpret_cast(src + kBlockBytes)); + block = ClassifyStringBlock(v1, ptrue); + if (block.HasSpecial()) { + return {kBlockBytes + block.index, block.kind, true}; + } + // None means both fixed-size blocks are clean. + return {0, StringSpecialKind::None, false}; +} template sonic_force_inline char *Quote(const char *src, size_t nb, char *dst) @@ -69,6 +179,127 @@ sonic_force_inline char *Quote(const char *src, size_t nb, char *dst) return dst; } +template +sonic_force_inline size_t parseStringInplace(uint8_t *&src, SonicError &err) { + // GenericDocument::allocateStringBuffer() and + // allocateSchemaStringBuffer() reserve 64B trailing padding. The caller + // guarantees this contract, so these vector loads do not read out of bounds. + uint8_t *dst = src; + uint8_t *sdst = src; + svbool_t ptrue = svptrue_b8(); + constexpr size_t svelen = kBlockBytes; + while (1) { + find: + __asm__ volatile("prfm pldl1keep, [%0, %1]" : : "r"(src), "r"(256ull)); + svuint8_t v = svld1_u8(ptrue, reinterpret_cast(src)); + auto first_block = ClassifyStringBlock(v, ptrue); + bool first_clean = !first_block.HasSpecial(); + if (sonic_unlikely(first_clean)) { + v = svld1_u8(ptrue, reinterpret_cast(src + svelen)); + first_block = ClassifyStringBlock(v, ptrue); + } + if (!first_block.HasSpecial()) { + // Both 32B blocks are clean, so skip the whole 64B window. + src += kScanWindowBytes; + goto find; + } + if (first_clean) { + first_block.index += svelen; + } + + if (first_block.kind == StringSpecialKind::Quote) { + src += first_block.index; + *src++ = '\0'; + return src - sdst - 1; + } + + if (sonic_unlikely(first_block.kind == StringSpecialKind::Unescaped)) { + if constexpr ((parseFlags & ParseFlags::kParseAllowUnescapedControlChars) == 0) { + err = kParseErrorUnEscaped; + return 0; + } + // Before the first escape, the original bytes are already in place. + src += first_block.index + 1; + goto find; + } + + /* find out where the backspace is */ + src += first_block.index; + dst = src; + + cont: + uint8_t escape_char = src[1]; + if (sonic_unlikely(escape_char == 'u')) { + if (!common::handle_unicode_codepoint(const_cast(&src), &dst)) { + err = kParseErrorEscapedUnicode; + return 0; + } + } else { + *dst = kEscapedMap[escape_char]; + if (sonic_unlikely(*dst == 0u)) { + err = kParseErrorEscapedFormat; + return 0; + } + src += 2; + dst += 1; + } + // fast path for continous escaped chars + if (*src == '\\') { + goto cont; + } + + find_and_move: + // Copy the next n bytes, and find the backslash and quote in them. + svuint8_t v0, v1; + auto block = ScanStringWindow2Blocks(src, ptrue, v0, v1); + if (!block.HasSpecial()) { + // Both 32B blocks are clean, so copy and advance the whole 64B window. + /* they are the same. Since they can't co-occur, it means we + * encountered neither. */ + svst1_u8(ptrue, reinterpret_cast(dst), v0); + svst1_u8(ptrue, reinterpret_cast(dst + kBlockBytes), v1); + src += kScanWindowBytes; + dst += kScanWindowBytes; + goto find_and_move; + } + + svuint8_t active = v0; + if (block.second) { + svst1_u8(ptrue, reinterpret_cast(dst), v0); + src += svelen; + dst += svelen; + active = v1; + } + + svbool_t prefix = block.Prefix(svelen); + // If the next thing is the end quote, copy and return + if (block.kind == StringSpecialKind::Quote) { + svst1_u8(prefix, reinterpret_cast(dst), active); + dst += block.LocalIndex(svelen); + *dst = '\0'; + src += block.LocalIndex(svelen) + 1; + return dst - sdst; + } + + if (sonic_unlikely(block.kind == StringSpecialKind::Unescaped)) { + if constexpr ((parseFlags & ParseFlags::kParseAllowUnescapedControlChars) == 0) { + err = kParseErrorUnEscaped; + return 0; + } + svst1_u8(prefix, reinterpret_cast(dst), active); + src += block.LocalIndex(svelen); + dst += block.LocalIndex(svelen); + *dst++ = *src++; + goto find_and_move; + } + svst1_u8(prefix, reinterpret_cast(dst), active); + src += block.LocalIndex(svelen); + dst += block.LocalIndex(svelen); + goto cont; + } + sonic_assert(false); +} + } // namespace sve2_256 } // namespace internal } // namespace sonic_json diff --git a/include/sonic/internal/arch/sve2-256/skip.h b/include/sonic/internal/arch/sve2-256/skip.h index d1da3b6e..875f99e3 100644 --- a/include/sonic/internal/arch/sve2-256/skip.h +++ b/include/sonic/internal/arch/sve2-256/skip.h @@ -14,21 +14,256 @@ * limitations under the License. */ +// This file has been modified by Huawei authors. +// Modifications are Copyright (c) 2026 Huawei Technologies Co., Ltd. #pragma once -#include "../neon/skip.h" +#include +#include + +#include + +#include "base.h" +#include "../common/unicode_common.h" +#include "../neon/simd.h" +#include "sonic/dom/json_pointer.h" +#include "sonic/error.h" +#include "sonic/internal/utils.h" +#include "sonic/macro.h" namespace sonic_json { namespace internal { namespace sve2_256 { +using sonic_json::internal::common::EqBytes4; +using sonic_json::internal::common::SkipLiteral; + +constexpr size_t kSpaceNMatchBytes = kBlockBytes; +constexpr size_t kSpaceCachedBytes = kSpaceNMatchBytes * 2; + +// GetNextToken find the next characters in tokens and update the position to +// it. +template +sonic_force_inline uint8_t GetNextToken(const uint8_t *data, size_t &pos, size_t len, const char (&tokens)[N]) +{ + for (; pos < len; pos += kBlockBytes) { + svbool_t predicate = svwhilelt_b8_u64(pos, len); + svuint8_t v = svld1_u8(predicate, reinterpret_cast(data + pos)); + svbool_t vor = svpfalse_b(); + for (size_t i = 0; i < N - 1; i++) { + svbool_t vtmp = svcmpeq_n_u8(predicate, v, (uint8_t)(tokens[i])); + vor = svorr_b_z(predicate, vor, vtmp); + } + if (svptest_any(predicate, vor)) { + pos += get_first_active_index(vor, predicate); + return data[pos]; + } + } + pos = len; + return '\0'; +} + +// pos is the after the ending quote +sonic_force_inline int SkipString(const uint8_t *data, size_t &pos, size_t len) +{ + const static int kEscaped = 2; + const static int kNormal = 1; + const static int kUnclosed = 0; + svbool_t ptrue = svptrue_b8(); + int ret = kNormal; + while (len > kBlockBytes && pos < len - kBlockBytes) { + svuint8_t v = svld1_u8(ptrue, reinterpret_cast(data + pos)); + svbool_t bs_bits = svcmpeq_n_u8(ptrue, v, (uint8_t)('\\')); + svbool_t quote_bits = svcmpeq_n_u8(ptrue, v, (uint8_t)('"')); + + auto bs_first_bits = svbrkb_b_z(ptrue, bs_bits); + if (svptest_any(ptrue, svand_b_z(ptrue, quote_bits, bs_first_bits))) { + pos += get_first_active_index(quote_bits, ptrue) + 1; + return ret; + } + + if (svptest_any(ptrue, bs_bits)) { + ret = kEscaped; + pos += svcntp_b8(bs_first_bits, bs_first_bits) + 0x2; + while (pos < len) { + if (data[pos] == '\\') { + pos += 0x2; + } else { + break; + } + } + } else { + pos += kBlockBytes; + } + } + + while (pos < len) { + svbool_t predicate = svwhilelt_b8_u64(pos, len); + svuint8_t v = svld1_u8(predicate, reinterpret_cast(data + pos)); + svbool_t bs_bits = svcmpeq_n_u8(predicate, v, (uint8_t)('\\')); + svbool_t quote_bits = svcmpeq_n_u8(predicate, v, (uint8_t)('"')); + + auto bs_first_bits = svbrkb_b_z(ptrue, bs_bits); + if (svptest_any(predicate, svand_b_z(predicate, quote_bits, bs_first_bits))) { + pos += get_first_active_index(quote_bits, predicate) + 1; + return ret; + } + + if (svptest_any(predicate, bs_bits)) { + ret = kEscaped; + pos += svcntp_b8(bs_first_bits, bs_first_bits) + 0x2; + while (pos < len) { + if (data[pos] == '\\') { + pos += 0x2; + } else { + break; + } + } + } else { + pos += svcntp_b8(predicate, predicate); + } + } + + return kUnclosed; +} + +template +sonic_force_inline uint64_t GetStringBits(const uint8_t *data, uint64_t &prev_instring, uint64_t &prev_escaped) +{ + const T v(data); + uint64_t escaped = 0; + uint64_t bs_bits = v.eq('\\'); + if (bs_bits) { + escaped = common::GetEscaped<64>(prev_escaped, bs_bits); + } else { + escaped = prev_escaped; + prev_escaped = 0; + } + uint64_t quote_bits = v.eq('"') & ~escaped; + uint64_t in_string = PrefixXor(quote_bits) ^ prev_instring; + prev_instring = uint64_t(static_cast(in_string) >> 63); + return in_string; +} + +// return true if container is closed. +template +sonic_force_inline bool skip_container(const uint8_t *data, size_t &pos, size_t len, uint8_t left, uint8_t right) +{ + uint64_t prev_instring = 0, prev_escaped = 0, instring; + int rbrace_num = 0, lbrace_num = 0, last_lbrace_num; + const uint8_t *p; + while (pos + 64 <= len) { + p = data + pos; + + instring = GetStringBits(p, prev_instring, prev_escaped); + T v(p); + last_lbrace_num = lbrace_num; + uint64_t rbrace = v.eq(right) & ~instring; + uint64_t lbrace = v.eq(left) & ~instring; + /* traverse each '}' */ + while (rbrace > 0) { + rbrace_num++; + lbrace_num = last_lbrace_num + CountOnes((rbrace - 1) & lbrace); + bool is_closed = lbrace_num < rbrace_num; + if (is_closed) { + sonic_assert(rbrace_num == lbrace_num + 1); + pos += TrailingZeroes(rbrace) + 1; + return true; + } + rbrace &= (rbrace - 1); + } + lbrace_num = last_lbrace_num + CountOnes(lbrace); + + pos += 64; + } + uint8_t buf[64] = { 0 }; + std::memcpy(buf, data + pos, len - pos); + p = buf; + + instring = GetStringBits(p, prev_instring, prev_escaped); + T v(p); + last_lbrace_num = lbrace_num; + uint64_t rbrace = v.eq(right) & ~instring; + uint64_t lbrace = v.eq(left) & ~instring; + /* traverse each '}' */ + while (rbrace > 0) { + rbrace_num++; + lbrace_num = last_lbrace_num + CountOnes((rbrace - 1) & lbrace); + bool is_closed = lbrace_num < rbrace_num; + if (is_closed) { + sonic_assert(rbrace_num == lbrace_num + 1); + pos += TrailingZeroes(rbrace) + 1; + return true; + } + rbrace &= (rbrace - 1); + } + lbrace_num = last_lbrace_num + CountOnes(lbrace); + + return false; +} + +sonic_force_inline bool SkipContainer(const uint8_t *data, size_t &pos, size_t len, uint8_t left, uint8_t right) +{ + return skip_container>(data, pos, len, left, right); +} + +sonic_force_inline uint8_t skip_space(const uint8_t *data, size_t &pos, + size_t &nonspace_bits_end, + uint64_t &nonspace_bits) +{ + if (!IsSpace(data[pos++])) + return data[pos - 1]; + if (!IsSpace(data[pos++])) + return data[pos - 1]; + + // nonspace_bits caches [nonspace_bits_end - kSpaceCachedBytes, + // nonspace_bits_end), so pos == nonspace_bits_end is already out of the + // cached window and must rescan. + if (sonic_likely(pos < nonspace_bits_end)) { + size_t block_start = nonspace_bits_end - kSpaceCachedBytes; + size_t offset = pos - block_start; + uint64_t bits = nonspace_bits & (~0ull << offset); + if (bits != 0) { + pos = block_start + TrailingZeroes(bits); + return data[pos++]; + } + + pos = nonspace_bits_end; + } + + svbool_t ptrue = svptrue_b8(); + svuint8_t token = svreinterpret_u8_u32(svdup_n_u32(0x90a0d20U)); + + while (1) { + size_t block_start = pos; + svuint8_t v0 = svld1_u8(ptrue, data + block_start); + svuint8_t v1 = svld1_u8(ptrue, data + block_start + kSpaceNMatchBytes); + + svbool_t mask0 = svnmatch_u8(ptrue, v0, token); + svbool_t mask1 = svnmatch_u8(ptrue, v1, token); + + uint64_t combined_bits = predicate_to_bits_u64(mask0, mask1); + + if (combined_bits != 0) { + nonspace_bits = combined_bits; + nonspace_bits_end = block_start + kSpaceCachedBytes; + pos = block_start + TrailingZeroes(combined_bits); + return data[pos++]; + } + + pos = block_start + kSpaceCachedBytes; + } + + sonic_assert(false && "!should not happen"); +} + -using sonic_json::internal::neon::EqBytes4; -using sonic_json::internal::neon::SkipLiteral; -using sonic_json::internal::neon::GetNextToken; -using sonic_json::internal::neon::SkipString; -using sonic_json::internal::neon::SkipContainer; -using sonic_json::internal::neon::skip_space; -using sonic_json::internal::neon::skip_space_safe; +sonic_force_inline uint8_t skip_space_safe(const uint8_t *data, size_t &pos, size_t len, size_t &, uint64_t &) +{ + while (pos < len && IsSpace(data[pos++])) + ; + // if not found, still return the space chars + return data[pos - 1]; +} } // namespace sve2_256 } // namespace internal diff --git a/include/sonic/internal/arch/sve2-256/str2int.h b/include/sonic/internal/arch/sve2-256/str2int.h index 08fc9e27..519b622a 100644 --- a/include/sonic/internal/arch/sve2-256/str2int.h +++ b/include/sonic/internal/arch/sve2-256/str2int.h @@ -1,6 +1,10 @@ /* * Copyright 2022 ByteDance Inc. * + * SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. + * All rights reserved. SPDX-License-Identifier: Apache-2.0 Copyright 2022 + * ByteDance Inc. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,15 +18,134 @@ * limitations under the License. */ +// This file has been modified by Huawei authors. +// Modifications are Copyright (c) 2026 Huawei Technologies Co., Ltd. + #pragma once -#include "../common/arm_common/str2int.h" +#include + +#include + +#define STR2INT_LOWEST_VALUE_MUL_FACTOR 10 +#define STR2INT_HUNDRED_MUL_FACTOR 100 +#define STR2INT_THOUSAND_MUL_FACTOR 1000 namespace sonic_json { namespace internal { namespace sve2_256 { -using sonic_json::internal::arm_common::simd_str2int; +sonic_force_inline uint32_t low_half_simd_str2int(svuint16_t data, uint32_t *mulFactor) +{ + svuint32_t factor = svld1_u32(svptrue_pat_b32(SV_VL8), mulFactor); + svuint32_t res = svmul_u32_x(svptrue_pat_b32(SV_VL8), svunpklo_u32(data), factor); + return svaddv_u32(svptrue_pat_b32(SV_VL8), res); +} + +sonic_force_inline uint64_t dot_simd_str2int(uint64_t *scale, uint16_t *mulFactor, svuint16_t data) +{ + svuint16_t factor = svld1_u16(svptrue_pat_b16(SV_VL16), mulFactor); + svuint64_t res = svdot_u64(svdup_n_u64(0), data, factor); + svuint64_t vecSca = svld1_u64(svptrue_pat_b64(SV_VL4), scale); + return svaddv_u64(svptrue_pat_b64(SV_VL4), svmul_u64_x(svptrue_pat_b64(SV_VL4), res, vecSca)); +} + +sonic_force_inline uint64_t simd_str2int(const char *c, int &man_nd) +{ + svbool_t pgAll = svptrue_pat_b16(SV_VL16); + svuint16_t data = svld1sb_u16(pgAll, reinterpret_cast(&c[0])); + data = svsub_n_u16_x(pgAll, data, '0'); + svbool_t gt_nine = svcmpgt_n_u16(pgAll, data, 9); + int num_end_idx = 16; + if (svptest_any(pgAll, gt_nine)) { + num_end_idx = svcntp_b16(pgAll, svbrkb_z(pgAll, gt_nine)); + } + man_nd = man_nd < num_end_idx ? man_nd : num_end_idx; + + switch (man_nd) { + case 1: + return svlasta_u16(svpfalse_b(), data); + case 0X2: + return svlasta_u16(svpfalse_b(), data) * STR2INT_LOWEST_VALUE_MUL_FACTOR + + svlasta_u16(svptrue_pat_b16(SV_VL1), data); + case 0X3: + return svlasta_u16(svpfalse_b(), data) * STR2INT_HUNDRED_MUL_FACTOR + + svlasta_u16(svptrue_pat_b16(SV_VL1), data) * + STR2INT_LOWEST_VALUE_MUL_FACTOR + svlasta_u16(svptrue_pat_b16(SV_VL2), data); + case 0x4: { + static uint16_t mulFactor[4] = { 1000, 100, 10, 1 }; + svuint16_t factor = svld1_u16(svptrue_pat_b16(SV_VL4), mulFactor); + svuint64_t res = svdot_u64(svdup_n_u64(0), data, factor); + return svlasta_u64(svpfalse_b(), res); + } + case 0x5: { + static uint32_t mulFactor[8] = { 10000, 1000, 100, 10, 1, 0, 0, 0 }; + return low_half_simd_str2int(data, mulFactor); + } + case 0x6: { + static uint32_t mulFactor[8] = { 100000, 10000, 1000, 100, 10, 1, 0, 0 }; + return low_half_simd_str2int(data, mulFactor); + } + case 0x7: { + static uint32_t mulFactor[8] = { 1000000, 100000, 10000, 1000, 100, 10, 1, 0 }; + return low_half_simd_str2int(data, mulFactor); + } + case 0x8: { + static uint32_t mulFactor[8] = { 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1 }; + return low_half_simd_str2int(data, mulFactor); + } + case 0x9: { + static uint32_t mulFactor[8] = { 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1 }; + uint64_t low_value = low_half_simd_str2int(data, mulFactor); + return (uint64_t)svlastb_u16(svwhilelt_b16_u32(0, 0x9), data) + + (uint64_t)low_value * STR2INT_LOWEST_VALUE_MUL_FACTOR; + } + case 0xa: { + static uint32_t mulFactor[8] = { 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1 }; + uint64_t low_value = low_half_simd_str2int(data, mulFactor); + return (uint64_t)svlasta_u16(svwhilelt_b16_u32(0, 0x9), data) + + (uint64_t)svlastb_u16(svwhilelt_b16_u32(0, 0x9), data) * STR2INT_LOWEST_VALUE_MUL_FACTOR + + low_value * STR2INT_HUNDRED_MUL_FACTOR; + } + case 0xb: { + static uint32_t mulFactor[8] = { 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1 }; + uint64_t low_value = low_half_simd_str2int(data, mulFactor); + return (uint64_t)svlasta_u16(svwhilelt_b16_u32(0, 0xa), data) + + (uint64_t)svlasta_u16(svwhilelt_b16_u32(0, 0x9), data) * STR2INT_LOWEST_VALUE_MUL_FACTOR + + (uint64_t)svlastb_u16(svwhilelt_b16_u32(0, 0x9), data) * STR2INT_HUNDRED_MUL_FACTOR + + low_value * STR2INT_THOUSAND_MUL_FACTOR; + } + case 0xc: { + static uint16_t mulFactor[16] = { 1000, 100, 10, 1, 1000, 100, 10, 1, 1000, 100, 10, 1, 0, 0, 0, 0 }; + static uint64_t scale[4] = { 100000000, 10000, 1, 0 }; + return dot_simd_str2int(scale, mulFactor, data); + } + + case 0xd: { + static uint16_t mulFactor[16] = { 1000, 100, 10, 1, 1000, 100, 10, 1, 1000, 100, 10, 1, 1, 0, 0, 0 }; + static uint64_t scale[4] = { 1000000000, 100000, 10, 1 }; + return dot_simd_str2int(scale, mulFactor, data); + } + + case 0xe: { + static uint16_t mulFactor[16] = { 1000, 100, 10, 1, 1000, 100, 10, 1, 1000, 100, 10, 1, 10, 1, 0, 0 }; + static uint64_t scale[4] = { 10000000000, 1000000, 100, 1 }; + return dot_simd_str2int(scale, mulFactor, data); + } + case 0xf: { + static uint16_t mulFactor[16] = { 1000, 100, 10, 1, 1000, 100, 10, 1, 1000, 100, 10, 1, 100, 10, 1, 0 }; + static uint64_t scale[4] = { 100000000000, 10000000, 1000, 1 }; + return dot_simd_str2int(scale, mulFactor, data); + } + case 0x10: { + static uint16_t mulFactor[16] = { 1000, 100, 10, 1, 1000, 100, 10, 1, 1000, 100, 10, 1, 1000, 100, 10, 1 }; + static uint64_t scale[4] = { 1000000000000, 100000000, 10000, 1 }; + return dot_simd_str2int(scale, mulFactor, data); + } + default: + return 0; + } +} } // namespace sve2_256 } // namespace internal diff --git a/tests/parsenumber_test.cpp b/tests/parsenumber_test.cpp index ae295932..7849cffe 100644 --- a/tests/parsenumber_test.cpp +++ b/tests/parsenumber_test.cpp @@ -14,6 +14,9 @@ * limitations under the License. */ +// This file has been modified by Huawei authors. +// Modifications are Copyright (c) 2026 Huawei Technologies Co., Ltd. + #include #include #include @@ -106,6 +109,9 @@ void TestParseInval(size_t off, const std::string& input) { TEST(ParserTest, ParseNumber) { TestParseUnsigned(0ULL, "0"); TestParseUnsigned(123ULL, "123"); + TestParseUnsigned(123456789ULL, "123456789"); + TestParseUnsigned(1234567890ULL, "1234567890"); + TestParseUnsigned(12345678901ULL, "12345678901"); TestParseUnsigned(9223372036854775807ULL, "9223372036854775807"); // LLONG_MAX TestParseUnsigned(18446744073709551615ULL, @@ -209,6 +215,35 @@ TEST(ParserTest, AllowUnescapedControlChars) { } } +TEST(ParserTest, PreservesLongStringWithUnescapedControlChar) { + for (size_t prefix_size : {64u, 96u}) { + SCOPED_TRACE(prefix_size); + const std::string prefix(prefix_size, 'a'); + const std::string input = "\"" + prefix + "name\tvalue\\nend\""; + const std::string expected = prefix + "name\tvalue\nend"; + + Document doc; + doc.Parse(input.data(), + input.size()); + ASSERT_FALSE(doc.HasParseError()); + ASSERT_TRUE(doc.IsString()); + EXPECT_EQ(expected, doc.GetStringView()); + } +} + +TEST(ParserTest, RejectsControlCharacterAfterBackslash) { + std::string s; + s.push_back('"'); + s.push_back('\\'); + s.push_back('\x01'); + s.push_back('"'); + + Document doc; + doc.Parse(s.data(), s.size()); + EXPECT_TRUE(doc.HasParseError()); + EXPECT_EQ(doc.GetParseError(), kParseErrorEscapedFormat); +} + void ParseFloatInFiles() { std::vector files = {"./testdata/num/float-1.txt", "./testdata/num/float-8.txt"}; diff --git a/tests/skip_test.cpp b/tests/skip_test.cpp index 924d4ef8..59d0a493 100644 --- a/tests/skip_test.cpp +++ b/tests/skip_test.cpp @@ -14,6 +14,11 @@ * limitations under the License. */ +#if defined(__ARM_FEATURE_SVE2) && (__ARM_FEATURE_SVE_BITS == 256) +// Include the backend first to catch dependencies on the parser's include order. +#include "sonic/internal/arch/sve2-256/skip.h" +#endif + #include #include "gtest/gtest.h"