github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/native/native.h (about)

     1  /*
     2   * Copyright 2023 CloudWeGo Authors.
     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  #include <stdint.h>
    21  #include <sys/types.h>
    22  #include <immintrin.h>
    23  #include <stdbool.h>
    24  
    25  #define V_EOF 1
    26  #define V_NULL 2
    27  #define V_TRUE 3
    28  #define V_FALSE 4
    29  #define V_ARRAY 5
    30  #define V_OBJECT 6
    31  #define V_STRING 7
    32  #define V_DOUBLE 8
    33  #define V_INTEGER 9
    34  #define V_KEY_SEP 10
    35  #define V_ELEM_SEP 11
    36  #define V_ARRAY_END 12
    37  #define V_OBJECT_END 13
    38  #define V_ATOF_NEED_FALLBACK 14
    39  
    40  #define F_DBLUNQ (1 << 0)
    41  #define F_UNIREP (1 << 1)
    42  
    43  #define VS_NULL 0x6c6c756e // 'null' in little endian
    44  #define VS_TRUE 0x65757274 // 'true' in little endian
    45  #define VS_ALSE 0x65736c61 // 'alse' in little endian ('false' without the 'f')
    46  
    47  #define ERR_EOF 1
    48  #define ERR_INVAL 2
    49  #define ERR_ESCAPE 3
    50  #define ERR_UNICODE 4
    51  #define ERR_OVERFLOW 5
    52  #define ERR_NUMBER_FMT 6
    53  #define ERR_RECURSE_MAX 7
    54  #define ERR_FLOAT_INF 8
    55  #define ERR_DISMATCH_TYPE 9
    56  #define ERR_NULL_REQUIRED 10
    57  #define ERR_UNSUPPORT_THRIFT_TYPE 11
    58  #define ERR_UNKNOWN_FIELD 12
    59  #define ERR_DISMATCH_TYPE2 13
    60  #define ERR_DECODE_BASE64 14
    61  #define ERR_STATE 15
    62  #define ERR_OOM_BM 16
    63  #define ERR_OOM_BUF 17
    64  #define ERR_OOM_KEY 18
    65  #define ERR_OOM_FIELD 22
    66  #define ERR_OOM_FVAL 23
    67  #define ERR_HM 19
    68  #define ERR_HM_END 21
    69  #define ERR_UNSUPPORT_VM_TYPE 20
    70  #define ERR_VM_END 24
    71  
    72  #define MAX_RECURSE 4096
    73  
    74  #define likely(v) (__builtin_expect((v), 1))
    75  #define unlikely(v) (__builtin_expect((v), 0))
    76  #define always_inline inline __attribute__((always_inline))
    77  
    78  #define as_m128p(v) ((__m128i *)(v))
    79  #define as_m128c(v) ((const __m128i *)(v))
    80  #define as_m256c(v) ((const __m256i *)(v))
    81  #define as_m128v(v) (*(const __m128i *)(v))
    82  #define as_uint64v(p) (*(uint64_t *)(p))
    83  #define is_infinity(v) ((as_uint64v(&v) << 1) == 0xFFE0000000000000)
    84  
    85  typedef struct
    86  {
    87      char *buf;
    88      size_t len;
    89      size_t cap;
    90  } GoSlice;
    91  
    92  typedef struct
    93  {
    94      const char *buf;
    95      size_t len;
    96  } GoString;
    97  
    98  typedef struct
    99  {
   100      void *rtyp;
   101      void *val;
   102  } GoEface;
   103  
   104  typedef struct
   105  {
   106      void *itab;
   107      void *val;
   108  } GoIface;
   109  
   110  typedef struct
   111  {
   112      long t;
   113      double d;
   114      int64_t i;
   115  } JsonNumber;
   116  
   117  typedef struct
   118  {
   119      int64_t vt;
   120      double dv;
   121      int64_t iv;
   122      int64_t ep;
   123      char *dbuf;
   124      size_t dcap;
   125  } JsonState;
   126  
   127  typedef struct
   128  {
   129      size_t sp;
   130      int64_t vt[MAX_RECURSE];
   131  } StateMachine;
   132  
   133  int f64toa(char *out, double val);
   134  int i64toa(char *out, int64_t val);
   135  int u64toa(char *out, uint64_t val);
   136  
   137  size_t lspace(const char *sp, size_t nb, size_t p);
   138  
   139  ssize_t quote(const char *sp, ssize_t nb, char *dp, ssize_t *dn, uint64_t flags);
   140  ssize_t unquote(const char *sp, ssize_t nb, char *dp, int64_t *ep, uint64_t flags);
   141  ssize_t html_escape(const char *sp, ssize_t nb, char *dp, ssize_t *dn);
   142  
   143  long value(const char *s, size_t n, long p, JsonState *ret, int allow_control);
   144  void vstring(const GoString *src, long *p, JsonState *ret);
   145  void vnumber(const GoString *src, long *p, JsonState *ret);
   146  void vsigned(const GoString *src, long *p, JsonState *ret);
   147  void vunsigned(const GoString *src, long *p, JsonState *ret);
   148  
   149  long skip_one(const GoString *src, long *p, StateMachine *m);
   150  long skip_array(const GoString *src, long *p, StateMachine *m);
   151  long skip_object(const GoString *src, long *p, StateMachine *m);
   152  
   153  long skip_string(const GoString *src, long *p);
   154  long skip_negative(const GoString *src, long *p);
   155  long skip_positive(const GoString *src, long *p);
   156  
   157  bool atof_eisel_lemire64(uint64_t mant, int exp10, int sgn, double *val);
   158  double atof_native(const char *sp, ssize_t nb, char *dbuf, ssize_t cap);
   159  
   160  ssize_t utf8_validate(const char *sp, ssize_t nb);
   161  long validate_string(const GoString *src, long *p);
   162  long validate_one(const GoString *src, long *p, StateMachine *m);
   163  
   164  #endif