diff --git a/CMakeLists.txt b/CMakeLists.txt index e4cef5457..c67c529c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,24 +140,6 @@ if(SIMDJSON_MINUS_ZERO_AS_FLOAT) simdjson_add_props(target_compile_definitions PRIVATE SIMDJSON_MINUS_ZERO_AS_FLOAT=1) endif(SIMDJSON_MINUS_ZERO_AS_FLOAT) -if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(loongarch64)$") - option(SIMDJSON_PREFER_LSX "Prefer LoongArch SX" ON) - include(CheckCXXCompilerFlag) - check_cxx_compiler_flag(-mlasx COMPILER_SUPPORTS_LASX) - check_cxx_compiler_flag(-mlsx COMPILER_SUPPORTS_LSX) - if(COMPILER_SUPPORTS_LASX AND NOT SIMDJSON_PREFER_LSX) - simdjson_add_props( - target_compile_options PRIVATE - -mlasx - ) - elseif(COMPILER_SUPPORTS_LSX) - simdjson_add_props( - target_compile_options PRIVATE - -mlsx - ) - endif() -endif() - # GCC and Clang have horrendous Debug builds when using SIMD. # A common fix is to use '-Og' instead. # bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412 diff --git a/include/simdjson/builtin.h b/include/simdjson/builtin.h index 4788007f8..10ce60769 100644 --- a/include/simdjson/builtin.h +++ b/include/simdjson/builtin.h @@ -20,10 +20,10 @@ #include "simdjson/ppc64.h" #elif SIMDJSON_BUILTIN_IMPLEMENTATION_IS(westmere) #include "simdjson/westmere.h" -#elif SIMDJSON_BUILTIN_IMPLEMENTATION_IS(lsx) -#include "simdjson/lsx.h" #elif SIMDJSON_BUILTIN_IMPLEMENTATION_IS(lasx) #include "simdjson/lasx.h" +#elif SIMDJSON_BUILTIN_IMPLEMENTATION_IS(lsx) +#include "simdjson/lsx.h" #else #error Unknown SIMDJSON_BUILTIN_IMPLEMENTATION #endif diff --git a/include/simdjson/generic/base.h b/include/simdjson/generic/base.h index e81a716fa..4d14c0f59 100644 --- a/include/simdjson/generic/base.h +++ b/include/simdjson/generic/base.h @@ -17,10 +17,10 @@ #include "simdjson/arm64/begin.h" #elif SIMDJSON_IMPLEMENTATION_PPC64 #include "simdjson/ppc64/begin.h" -#elif SIMDJSON_IMPLEMENTATION_LSX -#include "simdjson/lsx/begin.h" #elif SIMDJSON_IMPLEMENTATION_LASX #include "simdjson/lasx/begin.h" +#elif SIMDJSON_IMPLEMENTATION_LSX +#include "simdjson/lsx/begin.h" #elif SIMDJSON_IMPLEMENTATION_FALLBACK #include "simdjson/fallback/begin.h" #else diff --git a/include/simdjson/implementation_detection.h b/include/simdjson/implementation_detection.h index 0ff315b7a..93f671630 100644 --- a/include/simdjson/implementation_detection.h +++ b/include/simdjson/implementation_detection.h @@ -113,15 +113,15 @@ #endif #ifndef SIMDJSON_IMPLEMENTATION_LASX -#define SIMDJSON_IMPLEMENTATION_LASX (SIMDJSON_IS_LOONGARCH64 && __loongarch_asx) +#define SIMDJSON_IMPLEMENTATION_LASX (SIMDJSON_IS_LSX) #endif -#define SIMDJSON_CAN_ALWAYS_RUN_LASX (SIMDJSON_IMPLEMENTATION_LASX) +#define SIMDJSON_CAN_ALWAYS_RUN_LASX (SIMDJSON_IS_LASX) #ifndef SIMDJSON_IMPLEMENTATION_LSX #if SIMDJSON_CAN_ALWAYS_RUN_LASX #define SIMDJSON_IMPLEMENTATION_LSX 0 #else -#define SIMDJSON_IMPLEMENTATION_LSX (SIMDJSON_IS_LOONGARCH64 && __loongarch_sx) +#define SIMDJSON_IMPLEMENTATION_LSX (SIMDJSON_IS_LSX) #endif #endif #define SIMDJSON_CAN_ALWAYS_RUN_LSX (SIMDJSON_IMPLEMENTATION_LSX) @@ -165,4 +165,4 @@ #define SIMDJSON_BUILTIN_IMPLEMENTATION_ID SIMDJSON_IMPLEMENTATION_ID_FOR(SIMDJSON_BUILTIN_IMPLEMENTATION) #define SIMDJSON_BUILTIN_IMPLEMENTATION_IS(IMPL) SIMDJSON_BUILTIN_IMPLEMENTATION_ID == SIMDJSON_IMPLEMENTATION_ID_FOR(IMPL) -#endif // SIMDJSON_IMPLEMENTATION_DETECTION_H \ No newline at end of file +#endif // SIMDJSON_IMPLEMENTATION_DETECTION_H diff --git a/include/simdjson/lasx/begin.h b/include/simdjson/lasx/begin.h index 560eba737..5154206aa 100644 --- a/include/simdjson/lasx/begin.h +++ b/include/simdjson/lasx/begin.h @@ -1,4 +1,11 @@ #define SIMDJSON_IMPLEMENTATION lasx +#include // This is a hack. We should not need to put this include here. +#if SIMDJSON_CAN_ALWAYS_RUN_LASX +// nothing needed. +#else +SIMDJSON_TARGET_REGION("lasx,lsx") +#endif + #include "simdjson/lasx/base.h" #include "simdjson/lasx/intrinsics.h" #include "simdjson/lasx/bitmanipulation.h" @@ -8,3 +15,5 @@ #include "simdjson/lasx/stringparsing_defs.h" #define SIMDJSON_SKIP_BACKSLASH_SHORT_CIRCUIT 1 + + diff --git a/include/simdjson/lasx/end.h b/include/simdjson/lasx/end.h index 2f5ec8079..bcb4d8966 100644 --- a/include/simdjson/lasx/end.h +++ b/include/simdjson/lasx/end.h @@ -4,3 +4,10 @@ #undef SIMDJSON_SKIP_BACKSLASH_SHORT_CIRCUIT #undef SIMDJSON_IMPLEMENTATION + + +#if SIMDJSON_CAN_ALWAYS_RUN_LASX +// nothing needed. +#else +SIMDJSON_UNTARGET_REGION +#endif \ No newline at end of file diff --git a/include/simdjson/lasx/intrinsics.h b/include/simdjson/lasx/intrinsics.h index 9c783ef5a..4b28b9e37 100644 --- a/include/simdjson/lasx/intrinsics.h +++ b/include/simdjson/lasx/intrinsics.h @@ -5,8 +5,7 @@ #include "simdjson/lasx/base.h" #endif // SIMDJSON_CONDITIONAL_INCLUDE -// This should be the correct header whether -// you use visual studio or other compilers. +#include #include static_assert(sizeof(__m256i) <= simdjson::SIMDJSON_PADDING, "insufficient padding for LoongArch ASX"); diff --git a/include/simdjson/lasx/stringparsing_defs.h b/include/simdjson/lasx/stringparsing_defs.h index e00b80752..287e2ce2d 100644 --- a/include/simdjson/lasx/stringparsing_defs.h +++ b/include/simdjson/lasx/stringparsing_defs.h @@ -61,7 +61,7 @@ simdjson_inline escaping escaping::copy_and_find(const uint8_t *src, uint8_t *ds simd8 is_backslash = (v == '\\'); simd8 is_control = (v < 32); return { - (is_backslash | is_quote | is_control).to_bitmask() + static_cast((is_backslash | is_quote | is_control).to_bitmask()) }; } diff --git a/include/simdjson/lsx/intrinsics.h b/include/simdjson/lsx/intrinsics.h index 0559d4ab2..d28ce7361 100644 --- a/include/simdjson/lsx/intrinsics.h +++ b/include/simdjson/lsx/intrinsics.h @@ -5,8 +5,6 @@ #include "simdjson/lsx/base.h" #endif // SIMDJSON_CONDITIONAL_INCLUDE -// This should be the correct header whether -// you use visual studio or other compilers. #include static_assert(sizeof(__m128i) <= simdjson::SIMDJSON_PADDING, "insufficient padding for LoongArch SX"); diff --git a/include/simdjson/portability.h b/include/simdjson/portability.h index cfe1c1357..846bd6653 100644 --- a/include/simdjson/portability.h +++ b/include/simdjson/portability.h @@ -63,6 +63,12 @@ using std::size_t; #endif #elif defined(__loongarch_lp64) #define SIMDJSON_IS_LOONGARCH64 1 +#if defined(__loongarch_sx) && defined(__loongarch_asx) + #define SIMDJSON_IS_LSX 1 + #define SIMDJSON_IS_LASX 1 // We can always run both +#elif defined(__loongarch_sx) + #define SIMDJSON_IS_LSX 1 +#endif #elif defined(__PPC64__) || defined(_M_PPC64) #define SIMDJSON_IS_PPC64 1 #if defined(__ALTIVEC__) @@ -118,7 +124,7 @@ using std::size_t; // // We are going to use runtime dispatch. -#if SIMDJSON_IS_X86_64 +#if defined(SIMDJSON_IS_X86_64) || defined(SIMDJSON_IS_LSX) #ifdef __clang__ // clang does not have GCC push pop // warning: clang attribute push can't be used within a namespace in clang up @@ -135,7 +141,7 @@ using std::size_t; #define SIMDJSON_UNTARGET_REGION _Pragma("GCC pop_options") #endif // clang then gcc -#endif // x86 +#endif // defined(SIMDJSON_IS_X86_64) || defined(SIMDJSON_IS_LSX) // Default target region macros don't do anything. #ifndef SIMDJSON_TARGET_REGION diff --git a/src/implementation.cpp b/src/implementation.cpp index 3dba4b605..09f5f369e 100644 --- a/src/implementation.cpp +++ b/src/implementation.cpp @@ -94,6 +94,18 @@ static const simdjson::westmere::implementation* get_westmere_singleton() { } // namespace simdjson #endif // SIMDJSON_IMPLEMENTATION_WESTMERE +#if SIMDJSON_IMPLEMENTATION_LASX +#include +namespace simdjson { +namespace internal { +static const simdjson::lasx::implementation* get_lasx_singleton() { + static const simdjson::lasx::implementation lasx_singleton{}; + return &lasx_singleton; +} +} // namespace internal +} // namespace simdjson +#endif // SIMDJSON_IMPLEMENTATION_LASX + #if SIMDJSON_IMPLEMENTATION_LSX #include namespace simdjson { @@ -106,17 +118,6 @@ static const simdjson::lsx::implementation* get_lsx_singleton() { } // namespace simdjson #endif // SIMDJSON_IMPLEMENTATION_LSX -#if SIMDJSON_IMPLEMENTATION_LASX -#include -namespace simdjson { -namespace internal { -static const simdjson::lasx::implementation* get_lasx_singleton() { - static const simdjson::lasx::implementation lasx_singleton{}; - return &lasx_singleton; -} -} // namespace internal -} // namespace simdjson -#endif // SIMDJSON_IMPLEMENTATION_LASX #undef SIMDJSON_CONDITIONAL_INCLUDE @@ -210,12 +211,12 @@ static const std::initializer_list& get_available_implem #if SIMDJSON_IMPLEMENTATION_PPC64 get_ppc64_singleton(), #endif -#if SIMDJSON_IMPLEMENTATION_LSX - get_lsx_singleton(), -#endif #if SIMDJSON_IMPLEMENTATION_LASX get_lasx_singleton(), #endif +#if SIMDJSON_IMPLEMENTATION_LSX + get_lsx_singleton(), +#endif #if SIMDJSON_IMPLEMENTATION_FALLBACK get_fallback_singleton(), #endif diff --git a/src/internal/isadetection.h b/src/internal/isadetection.h index c873f7b74..f8c4a5be9 100644 --- a/src/internal/isadetection.h +++ b/src/internal/isadetection.h @@ -55,6 +55,9 @@ POSSIBILITY OF SUCH DAMAGE. #elif defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID) #include #endif +#if defined(__loongarch__) && defined(__linux__) + #include +#endif namespace simdjson { namespace internal { @@ -219,16 +222,21 @@ static inline uint32_t detect_supported_architectures() { return host_isa; } -#elif defined(__loongarch_sx) && !defined(__loongarch_asx) +#elif defined(__loongarch__) static inline uint32_t detect_supported_architectures() { - return instruction_set::LSX; -} - -#elif defined(__loongarch_asx) - -static inline uint32_t detect_supported_architectures() { - return instruction_set::LASX; + uint32_t host_isa = instruction_set::DEFAULT; + #if defined(__linux__) + uint64_t hwcap = 0; + hwcap = getauxval(AT_HWCAP); + if (hwcap & HWCAP_LOONGARCH_LSX) { + host_isa |= instruction_set::LSX; + } + if (hwcap & HWCAP_LOONGARCH_LASX) { + host_isa |= instruction_set::LASX; + } + #endif + return host_isa; } #else // fallback diff --git a/src/simdjson.cpp b/src/simdjson.cpp index 3a22d948f..2ea7d46ad 100644 --- a/src/simdjson.cpp +++ b/src/simdjson.cpp @@ -35,12 +35,12 @@ SIMDJSON_PUSH_DISABLE_UNUSED_WARNINGS #if SIMDJSON_IMPLEMENTATION_WESTMERE #include #endif -#if SIMDJSON_IMPLEMENTATION_LSX -#include -#endif #if SIMDJSON_IMPLEMENTATION_LASX #include #endif +#if SIMDJSON_IMPLEMENTATION_LSX +#include +#endif #if SIMDJSON_IMPLEMENTATION_FALLBACK #include #endif