github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/internal/native/dispatch_amd64.go (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  package native
    18  
    19  import (
    20      `unsafe`
    21  
    22      `github.com/bytedance/sonic/internal/cpu`
    23      `github.com/bytedance/sonic/internal/native/avx`
    24      `github.com/bytedance/sonic/internal/native/avx2`
    25      `github.com/bytedance/sonic/internal/native/sse`
    26      `github.com/bytedance/sonic/internal/native/types`
    27      `github.com/bytedance/sonic/internal/rt`
    28  )
    29  
    30  const MaxFrameSize   uintptr = 400
    31  
    32  var (
    33      S_f64toa uintptr
    34      S_f32toa uintptr
    35      S_i64toa uintptr
    36      S_u64toa uintptr
    37      S_lspace uintptr
    38  )
    39  
    40  var (
    41      S_quote   uintptr
    42      S_unquote uintptr
    43  )
    44  
    45  var (
    46      S_value     uintptr
    47      S_vstring   uintptr
    48      S_vnumber   uintptr
    49      S_vsigned   uintptr
    50      S_vunsigned uintptr
    51  )
    52  
    53  var (
    54      S_skip_one    uintptr
    55      S_skip_one_fast    uintptr
    56      S_get_by_path    uintptr
    57      S_skip_array  uintptr
    58      S_skip_object uintptr
    59      S_skip_number uintptr
    60  )
    61  
    62  var (
    63      __Quote func(s unsafe.Pointer, nb int, dp unsafe.Pointer, dn unsafe.Pointer, flags uint64) int
    64  
    65      __Unquote func(s unsafe.Pointer, nb int, dp unsafe.Pointer, ep unsafe.Pointer, flags uint64) int
    66  
    67      __HTMLEscape func(s unsafe.Pointer, nb int, dp unsafe.Pointer, dn unsafe.Pointer) int
    68  
    69      __Value func(s unsafe.Pointer, n int, p int, v unsafe.Pointer, flags uint64) int
    70  
    71      __SkipOne func(s unsafe.Pointer, p unsafe.Pointer, m unsafe.Pointer, flags uint64) int
    72  
    73      __SkipOneFast func(s unsafe.Pointer, p unsafe.Pointer) int
    74  
    75      __GetByPath func(s unsafe.Pointer, p unsafe.Pointer, path unsafe.Pointer, m unsafe.Pointer) int
    76  
    77      __ValidateOne func(s unsafe.Pointer, p unsafe.Pointer, m unsafe.Pointer, flags uint64) int
    78  
    79      __I64toa func(out unsafe.Pointer, val int64) (ret int)
    80  
    81      __U64toa func(out unsafe.Pointer, val uint64) (ret int)
    82  
    83      __F64toa func(out unsafe.Pointer, val float64) (ret int)
    84  
    85      __F32toa func(out unsafe.Pointer, val float32) (ret int)
    86  
    87      __ValidateUTF8 func(s unsafe.Pointer, p unsafe.Pointer, m unsafe.Pointer) (ret int)
    88  
    89      __ValidateUTF8Fast func(s unsafe.Pointer) (ret int)
    90  )
    91  
    92  //go:nosplit
    93  func Quote(s unsafe.Pointer, nb int, dp unsafe.Pointer, dn *int, flags uint64) int {
    94      return __Quote(rt.NoEscape(unsafe.Pointer(s)), nb, rt.NoEscape(unsafe.Pointer(dp)), rt.NoEscape(unsafe.Pointer(dn)), flags)
    95  }
    96  
    97  //go:nosplit
    98  func Unquote(s unsafe.Pointer, nb int, dp unsafe.Pointer, ep *int, flags uint64) int {
    99      return __Unquote(rt.NoEscape(unsafe.Pointer(s)), nb, rt.NoEscape(unsafe.Pointer(dp)), rt.NoEscape(unsafe.Pointer(ep)), flags)
   100  }
   101  
   102  //go:nosplit
   103  func HTMLEscape(s unsafe.Pointer, nb int, dp unsafe.Pointer, dn *int) int {
   104      return __HTMLEscape(rt.NoEscape(unsafe.Pointer(s)), nb, rt.NoEscape(unsafe.Pointer(dp)), rt.NoEscape(unsafe.Pointer(dn)))
   105  }
   106  
   107  //go:nosplit
   108  func Value(s unsafe.Pointer, n int, p int, v *types.JsonState, flags uint64) int {
   109      return __Value(rt.NoEscape(unsafe.Pointer(s)), n, p, rt.NoEscape(unsafe.Pointer(v)), flags)
   110  }
   111  
   112  //go:nosplit
   113  func SkipOne(s *string, p *int, m *types.StateMachine, flags uint64) int {
   114      return __SkipOne(rt.NoEscape(unsafe.Pointer(s)), rt.NoEscape(unsafe.Pointer(p)), rt.NoEscape(unsafe.Pointer(m)), flags)
   115  }
   116  
   117  //go:nosplit
   118  func SkipOneFast(s *string, p *int) int {
   119      return __SkipOneFast(rt.NoEscape(unsafe.Pointer(s)), rt.NoEscape(unsafe.Pointer(p)))
   120  }
   121  
   122  //go:nosplit
   123  func GetByPath(s *string, p *int, path *[]interface{}, m *types.StateMachine) int {
   124      return __GetByPath(rt.NoEscape(unsafe.Pointer(s)), rt.NoEscape(unsafe.Pointer(p)), rt.NoEscape(unsafe.Pointer(path)), rt.NoEscape(unsafe.Pointer(m)))
   125  }
   126  
   127  //go:nosplit
   128  func ValidateOne(s *string, p *int, m *types.StateMachine, flags uint64) int {
   129      return __ValidateOne(rt.NoEscape(unsafe.Pointer(s)), rt.NoEscape(unsafe.Pointer(p)), rt.NoEscape(unsafe.Pointer(m)), flags)
   130  }
   131  
   132  //go:nosplit
   133  func I64toa(out *byte, val int64) (ret int) {
   134      return __I64toa(rt.NoEscape(unsafe.Pointer(out)), val)
   135  }
   136  
   137  //go:nosplit
   138  func U64toa(out *byte, val uint64) (ret int) {
   139      return __U64toa(rt.NoEscape(unsafe.Pointer(out)), val)
   140  }
   141  
   142  //go:nosplit
   143  func F64toa(out *byte, val float64) (ret int) {
   144      return __F64toa(rt.NoEscape(unsafe.Pointer(out)), val)
   145  }
   146  
   147  //go:nosplit
   148  func F32toa(out *byte, val float32) (ret int) {
   149      return __F32toa(rt.NoEscape(unsafe.Pointer(out)), val)
   150  }
   151  
   152  //go:nosplit
   153  func ValidateUTF8(s *string, p *int, m *types.StateMachine) (ret int) {
   154      return __ValidateUTF8(rt.NoEscape(unsafe.Pointer(s)), rt.NoEscape(unsafe.Pointer(p)), rt.NoEscape(unsafe.Pointer(m)))
   155  }
   156  
   157  //go:nosplit
   158  func ValidateUTF8Fast(s *string) (ret int) {
   159      return __ValidateUTF8Fast(rt.NoEscape(unsafe.Pointer(s)))
   160  }
   161  
   162  func useSSE() {
   163      sse.Use()
   164      S_f64toa      = sse.S_f64toa
   165      __F64toa      = sse.F_f64toa
   166      S_f32toa      = sse.S_f32toa
   167      __F64toa      = sse.F_f64toa
   168      S_i64toa      = sse.S_i64toa
   169      __I64toa      = sse.F_i64toa
   170      S_u64toa      = sse.S_u64toa
   171      __U64toa      = sse.F_u64toa
   172      S_lspace      = sse.S_lspace
   173      S_quote       = sse.S_quote
   174      __Quote       = sse.F_quote
   175      S_unquote     = sse.S_unquote
   176      __Unquote     = sse.F_unquote
   177      S_value       = sse.S_value
   178      __Value       = sse.F_value
   179      S_vstring     = sse.S_vstring
   180      S_vnumber     = sse.S_vnumber
   181      S_vsigned     = sse.S_vsigned
   182      S_vunsigned   = sse.S_vunsigned
   183      S_skip_one    = sse.S_skip_one
   184      __SkipOne     = sse.F_skip_one
   185      __SkipOneFast = sse.F_skip_one_fast
   186      S_skip_array  = sse.S_skip_array
   187      S_skip_object = sse.S_skip_object
   188      S_skip_number = sse.S_skip_number
   189      S_get_by_path = sse.S_get_by_path
   190      __GetByPath   = sse.F_get_by_path
   191      __HTMLEscape  = sse.F_html_escape
   192      __ValidateOne = sse.F_validate_one
   193      __ValidateUTF8= sse.F_validate_utf8
   194      __ValidateUTF8Fast = sse.F_validate_utf8_fast
   195  }
   196  
   197  
   198  func useAVX() {
   199      avx.Use()
   200      S_f64toa      = avx.S_f64toa
   201      __F64toa      = avx.F_f64toa
   202      S_f32toa      = avx.S_f32toa
   203      __F64toa      = avx.F_f64toa
   204      S_i64toa      = avx.S_i64toa
   205      __I64toa      = avx.F_i64toa
   206      S_u64toa      = avx.S_u64toa
   207      __U64toa      = avx.F_u64toa
   208      S_lspace      = avx.S_lspace
   209      S_quote       = avx.S_quote
   210      __Quote       = avx.F_quote
   211      S_unquote     = avx.S_unquote
   212      __Unquote     = avx.F_unquote
   213      S_value       = avx.S_value
   214      __Value       = avx.F_value
   215      S_vstring     = avx.S_vstring
   216      S_vnumber     = avx.S_vnumber
   217      S_vsigned     = avx.S_vsigned
   218      S_vunsigned   = avx.S_vunsigned
   219      S_skip_one    = avx.S_skip_one
   220      __SkipOne     = avx.F_skip_one
   221      __SkipOneFast = avx.F_skip_one_fast
   222      S_skip_array  = avx.S_skip_array
   223      S_skip_object = avx.S_skip_object
   224      S_skip_number = avx.S_skip_number
   225      S_get_by_path = avx.S_get_by_path
   226      __GetByPath   = avx.F_get_by_path
   227      __HTMLEscape  = avx.F_html_escape
   228      __ValidateOne = avx.F_validate_one
   229      __ValidateUTF8= avx.F_validate_utf8
   230      __ValidateUTF8Fast = avx.F_validate_utf8_fast
   231  }
   232  
   233  func useAVX2() {
   234      avx2.Use()
   235      S_f64toa      = avx2.S_f64toa
   236      __F64toa      = avx2.F_f64toa
   237      S_f32toa      = avx2.S_f32toa
   238      __F64toa      = avx2.F_f64toa
   239      S_i64toa      = avx2.S_i64toa
   240      __I64toa      = avx2.F_i64toa
   241      S_u64toa      = avx2.S_u64toa
   242      __U64toa      = avx2.F_u64toa
   243      S_lspace      = avx2.S_lspace
   244      S_quote       = avx2.S_quote
   245      __Quote       = avx2.F_quote
   246      S_unquote     = avx2.S_unquote
   247      __Unquote     = avx2.F_unquote
   248      S_value       = avx2.S_value
   249      __Value       = avx2.F_value
   250      S_vstring     = avx2.S_vstring
   251      S_vnumber     = avx2.S_vnumber
   252      S_vsigned     = avx2.S_vsigned
   253      S_vunsigned   = avx2.S_vunsigned
   254      S_skip_one    = avx2.S_skip_one
   255      __SkipOne     = avx2.F_skip_one
   256      __SkipOneFast = avx2.F_skip_one_fast
   257      S_skip_array  = avx2.S_skip_array
   258      S_skip_object = avx2.S_skip_object
   259      S_skip_number = avx2.S_skip_number
   260      S_get_by_path = avx2.S_get_by_path
   261      __GetByPath   = avx2.F_get_by_path
   262      __HTMLEscape  = avx2.F_html_escape
   263      __ValidateOne = avx2.F_validate_one
   264      __ValidateUTF8= avx2.F_validate_utf8
   265      __ValidateUTF8Fast = avx2.F_validate_utf8_fast
   266  }
   267  
   268  
   269  func init() {
   270   if cpu.HasAVX2 {
   271      useAVX2()
   272   } else if cpu.HasAVX {
   273      useAVX()
   274   } else if cpu.HasSSE {
   275      useSSE()
   276   } else {
   277      panic("Unsupported CPU, maybe it's too old to run Sonic.")
   278   }
   279  }