Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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",
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cmake/set_arch_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion include/sonic/dom/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ class Parser {
}

template <typename SAX>
void parsePrimitives(SAX &sax) {
sonic_force_inline void parsePrimitives(SAX &sax) {
bool ok = true;
switch (json_buf_[pos_ - 1]) {
case '0':
Expand Down
3 changes: 3 additions & 0 deletions include/sonic/internal/arch/simd_dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions include/sonic/internal/arch/sonic_cpu_feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
125 changes: 125 additions & 0 deletions include/sonic/internal/arch/sve2-256/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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.

// This file has been modified by Huawei authors.
// Modifications are Copyright (c) 2026 Huawei Technologies Co., Ltd.

#pragma once

#include <arm_neon.h>
#include <arm_sve.h>
#include "sonic/macro.h"
#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;
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;

template <bool EscapeEmoji>
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<const uint8_t *>(src));
svst1_u8(pg, reinterpret_cast<uint8_t *>(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));
}

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<uint32_t *>(&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 <size_t ChunkSize>
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<uint8_t*>(dst_);
const uint8_t* src = reinterpret_cast<const uint8_t*>(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<uint8_t*>(dst_);
const uint8_t* src = reinterpret_cast<const uint8_t*>(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
110 changes: 110 additions & 0 deletions include/sonic/internal/arch/sve2-256/itoa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.
*/

// This file has been modified by Huawei authors.
// Modifications are Copyright (c) 2026 Huawei Technologies Co., Ltd.

#pragma once

#include <arm_sve.h>

namespace sonic_json {
namespace internal {
namespace sve2_256 {

#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
} // namespace sonic_json
Loading