github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/internal/native/dispatch_amd64.go (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 package native 18 19 import ( 20 "unsafe" 21 22 "github.com/bytedance/sonic/loader" 23 "github.com/cloudwego/dynamicgo/internal/cpu" 24 "github.com/cloudwego/dynamicgo/internal/native/avx" 25 "github.com/cloudwego/dynamicgo/internal/native/avx2" 26 "github.com/cloudwego/dynamicgo/internal/native/sse" 27 "github.com/cloudwego/dynamicgo/internal/native/types" 28 "github.com/cloudwego/dynamicgo/internal/rt" 29 ) 30 31 const MaxFrameSize uintptr = 1024 32 33 var ( 34 __Quote func(s unsafe.Pointer, nb int, dp unsafe.Pointer, dn unsafe.Pointer, flags uint64) int 35 36 __I64toa func(out unsafe.Pointer, val int64) (ret int) 37 38 __F64toa func(out unsafe.Pointer, val float64) (ret int) 39 40 __j2t_fsm_exec func(fsm unsafe.Pointer, buf unsafe.Pointer, src unsafe.Pointer, flag uint64) (ret uint64) 41 42 __tb_skip func(st unsafe.Pointer, s unsafe.Pointer, n int, t uint8) (ret int) 43 ) 44 45 //go:nosplit 46 func Quote(s unsafe.Pointer, nb int, dp unsafe.Pointer, dn *int, flags uint64) int { 47 return __Quote(rt.NoEscape(unsafe.Pointer(s)), nb, rt.NoEscape(unsafe.Pointer(dp)), rt.NoEscape(unsafe.Pointer(dn)), flags) 48 } 49 50 //go:nosplit 51 func I64toa(out *byte, val int64) (ret int) { 52 return __I64toa(rt.NoEscape(unsafe.Pointer(out)), val) 53 } 54 55 //go:nosplit 56 func F64toa(out *byte, val float64) (ret int) { 57 return __F64toa(rt.NoEscape(unsafe.Pointer(out)), val) 58 } 59 60 //go:nosplit 61 func TBSkip(st *types.TStateMachine, s *byte, n int, t uint8) (ret int) { 62 return __tb_skip(rt.NoEscape(unsafe.Pointer(st)), rt.NoEscape(unsafe.Pointer(s)), n, t) 63 } 64 65 //go:nosplit 66 func J2T_FSM(fsm *types.J2TStateMachine, buf *[]byte, src *string, flag uint64) (ret uint64) { 67 return __j2t_fsm_exec(rt.NoEscape(unsafe.Pointer(fsm)), rt.NoEscape(unsafe.Pointer(buf)), rt.NoEscape(unsafe.Pointer(src)), flag) 68 } 69 70 var stubs = []loader.GoC{ 71 {"_j2t_fsm_exec", nil, &__j2t_fsm_exec}, 72 {"_tb_skip", nil, &__tb_skip}, 73 {"_quote", nil, &__Quote}, 74 {"_i64toa", nil, &__I64toa}, 75 {"_f64toa", nil, &__F64toa}, 76 } 77 78 func useAVX() { 79 loader.WrapGoC(avx.Text__native_entry__, avx.Funcs, stubs, "avx", "avx/native.c") 80 } 81 82 func useAVX2() { 83 loader.WrapGoC(avx2.Text__native_entry__, avx2.Funcs, stubs, "avx2", "avx2/native.c") 84 } 85 86 func useSSE() { 87 loader.WrapGoC(sse.Text__native_entry__, sse.Funcs, stubs, "sse", "sse/native.c") 88 } 89 90 func init() { 91 if cpu.HasAVX2 { 92 useAVX2() 93 } else if cpu.HasAVX { 94 useAVX() 95 } else if cpu.HasSSE { 96 useSSE() 97 } else { 98 panic("Unsupported CPU, maybe it's too old to run Sonic.") 99 } 100 }