github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/native/native.h (about) 1 /* 2 * Copyright 2021 ByteDance Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef NATIVE_H 18 #define NATIVE_H 19 20 #ifdef __aarch64__ 21 #define SIMDE_ENABLE_NATIVE_ALIASES 22 #include <x86/sse.h> 23 #include <x86/sse2.h> 24 #include <x86/sse4.1.h> 25 #include <x86/sse4.2.h> 26 #include <x86/ssse3.h> 27 #include <x86/clmul.h> 28 #include <x86/avx2.h> 29 #else 30 #include <immintrin.h> 31 #endif 32 33 #include <stdint.h> 34 #include <sys/types.h> 35 #include <stdbool.h> 36 37 #include "types.h" 38 39 #define likely(v) (__builtin_expect((v), 1)) 40 #define unlikely(v) (__builtin_expect((v), 0)) 41 #define always_inline inline __attribute__((always_inline)) 42 43 #define as_m128p(v) ((__m128i *)(v)) 44 #define as_m128c(v) ((const __m128i *)(v)) 45 #define as_m256c(v) ((const __m256i *)(v)) 46 #define as_m128v(v) (*(const __m128i *)(v)) 47 #define as_uint64v(p) (*(uint64_t *)(p)) 48 #define is_infinity(v) ((as_uint64v(&v) << 1) == 0xFFE0000000000000) 49 50 typedef struct 51 { 52 void *buf; 53 size_t len; 54 size_t cap; 55 } GoSlice; 56 57 static const uint8_t GO_KIND_MASK = (1 << 5) - 1; 58 typedef enum 59 { 60 Invalid = 0, 61 Bool, 62 Int, 63 Int8, 64 Int16, 65 Int32, 66 Int64, 67 Uint, 68 Uint8, 69 Uint16, 70 Uint32, 71 Uint64, 72 Uintptr, 73 Float32, 74 Float64, 75 Complex64, 76 Complex128, 77 Array, 78 Chan, 79 Func, 80 Interface, 81 Map, 82 Pointer, 83 Slice, 84 String, 85 Struct, 86 UnsafePointer, 87 } GoKind; 88 89 typedef struct 90 { 91 uint64_t size; 92 uint64_t ptr_data; 93 uint32_t hash; 94 uint8_t flags; 95 uint8_t align; 96 uint8_t filed_align; 97 uint8_t kind_flags; 98 uint64_t traits; 99 void *gc_data; 100 int32_t str; 101 int32_t ptr_to_self; 102 } GoType; 103 104 typedef struct 105 { 106 GoType *type; 107 void *value; 108 } GoIface; 109 110 typedef struct 111 { 112 const char *buf; 113 size_t len; 114 } GoString; 115 116 typedef struct 117 { 118 long t; 119 double d; 120 int64_t i; 121 } JsonNumber; 122 123 typedef struct 124 { 125 long vt; 126 double dv; 127 int64_t iv; 128 int64_t ep; 129 char *dbuf; 130 ssize_t dcap; 131 } JsonState; 132 133 typedef struct 134 { 135 int64_t sp; 136 int64_t vt[MAX_RECURSE]; 137 } StateMachine; 138 139 int f32toa(char *out, float val); 140 int f64toa(char *out, double val); 141 int i64toa(char *out, int64_t val); 142 int u64toa(char *out, uint64_t val); 143 144 size_t lspace(const char *sp, size_t nb, size_t p); 145 146 ssize_t quote(const char *sp, ssize_t nb, char *dp, ssize_t *dn, uint64_t flags); 147 ssize_t unquote(const char *sp, ssize_t nb, char *dp, ssize_t *ep, uint64_t flags); 148 ssize_t html_escape(const char *sp, ssize_t nb, char *dp, ssize_t *dn); 149 150 long value(const char *s, size_t n, long p, JsonState *ret, uint64_t flags); 151 void vstring(const GoString *src, long *p, JsonState *ret, uint64_t flags); 152 void vnumber(const GoString *src, long *p, JsonState *ret); 153 void vsigned(const GoString *src, long *p, JsonState *ret); 154 void vunsigned(const GoString *src, long *p, JsonState *ret); 155 156 long skip_one(const GoString *src, long *p, StateMachine *m, uint64_t flags); 157 long skip_array(const GoString *src, long *p, StateMachine *m, uint64_t flags); 158 long skip_object(const GoString *src, long *p, StateMachine *m, uint64_t flags); 159 160 long skip_string(const GoString *src, long *p, uint64_t flags); 161 long skip_negative(const GoString *src, long *p); 162 long skip_positive(const GoString *src, long *p); 163 long skip_number(const GoString *src, long *p); 164 long fsm_exec(StateMachine *self, const GoString *src, long *p, uint64_t flags); 165 166 long validate_string(const GoString *src, long *p); 167 long validate_one(const GoString *src, long *p, StateMachine *m, uint64_t flags); 168 long validate_utf8(const GoString *src, long *p, StateMachine *m); 169 long validate_utf8_fast(const GoString *src); 170 171 long skip_one_fast(const GoString *src, long *p); 172 long get_by_path(const GoString *src, long *p, const GoSlice *path, StateMachine *sm); 173 #endif