From d7e21c169e766409958a3487c32084a74ea1ea5d Mon Sep 17 00:00:00 2001 From: hazyhaar Date: Mon, 2 Nov 2026 11:34:31 +0100 Subject: [PATCH] feat: add static Go 1.27 simd/archsimd backend Add a static Go 1.27 simd/archsimd AVX2 backend under go1.27 && goexperiment.simd so amd64 builds with GOEXPERIMENT=simd on Go 1.27+ encode and decode without runtime executable page mapping (W^X / MemoryDenyWriteExecute=yes) or sonic/loader. - Gate the static backend on go1.27 && goexperiment.simd and guard internal/native (including avx2/sse subpackages and templates) with !go1.27 || !goexperiment.simd so Go <= 1.26 stays on internal/native (preserving _subr__b64decode for sonic's !go1.27 JIT) while Go 1.27+ passes GOEXPERIMENT=simd go test ./... across all root-module packages. - Match native/native.h JSON mode unescaping (\r, \n, \/, ASCII \uXXXX), raw CRLF acceptance, zero-alloc in-place decoding, and src-indexed base64.CorruptInputError offsets. - Document that _subr__b64decode and _subr__b64encode remain 0 under go1.27 && goexperiment.simd, matching -tags noasm and appengine builds. - Place Apache-2.0 license headers before build constraints and ignore generated internal/simd/*.go files in .licenserc.yaml. Part of #17 --- .licenserc.yaml | 1 + base64x_error_native_amd64_test.go | 7 +- base64x_fallback_test.go | 32 +- base64x_native.go | 35 ++- base64x_simd_amd64.go | 278 +++++++++++++++++ base64x_simd_amd64_test.go | 125 ++++++++ base64x_subr_amd64.go | 5 +- base64x_subr_native_amd64.go | 7 +- internal/native/avx2/b64decode.go | 7 +- internal/native/avx2/b64decode_subr.go | 7 +- internal/native/avx2/b64decode_text_amd64.go | 7 +- internal/native/avx2/b64encode.go | 7 +- internal/native/avx2/b64encode_subr.go | 7 +- internal/native/avx2/b64encode_text_amd64.go | 7 +- internal/native/avx2/native_export.go | 7 +- internal/native/b64decode.tmpl | 7 +- internal/native/b64encode.tmpl | 7 +- internal/native/dispatch.go | 7 +- internal/native/native_export.tmpl | 7 +- internal/native/native_test.go | 35 ++- internal/native/sse/b64decode.go | 7 +- internal/native/sse/b64decode_subr.go | 7 +- internal/native/sse/b64decode_text_amd64.go | 7 +- internal/native/sse/b64encode.go | 7 +- internal/native/sse/b64encode_subr.go | 7 +- internal/native/sse/b64encode_text_amd64.go | 7 +- internal/native/sse/native_export.go | 7 +- internal/simd/b64_gen.go | 311 +++++++++++++++++++ internal/simd/b64_nosimd_gen.go | 227 ++++++++++++++ 29 files changed, 1097 insertions(+), 92 deletions(-) create mode 100644 base64x_simd_amd64.go create mode 100644 base64x_simd_amd64_test.go create mode 100644 internal/simd/b64_gen.go create mode 100644 internal/simd/b64_nosimd_gen.go diff --git a/.licenserc.yaml b/.licenserc.yaml index 8f65d8f..e85c02b 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -10,5 +10,6 @@ header: paths-ignore: - 'internal/native/avx2/*.go' - 'internal/native/sse/*.go' + - 'internal/simd/*.go' comment: on-failure diff --git a/base64x_error_native_amd64_test.go b/base64x_error_native_amd64_test.go index 8b62c03..ca0f9dc 100644 --- a/base64x_error_native_amd64_test.go +++ b/base64x_error_native_amd64_test.go @@ -12,8 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine package base64x diff --git a/base64x_fallback_test.go b/base64x_fallback_test.go index 1447270..917b65f 100644 --- a/base64x_fallback_test.go +++ b/base64x_fallback_test.go @@ -1,21 +1,19 @@ -//go:build !amd64 || noasm || appengine -// +build !amd64 noasm appengine +// Copyright 2024 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. -/* - * Copyright 2024 CloudWeGo Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +//go:build (go1.27 && goexperiment.simd) || !amd64 || noasm || appengine +// +build go1.27,goexperiment.simd !amd64 noasm appengine package base64x diff --git a/base64x_native.go b/base64x_native.go index 5643729..bed3649 100644 --- a/base64x_native.go +++ b/base64x_native.go @@ -1,21 +1,22 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +// Copyright 2024 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. -/* - * Copyright 2024 CloudWeGo Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine package base64x diff --git a/base64x_simd_amd64.go b/base64x_simd_amd64.go new file mode 100644 index 0000000..95363b3 --- /dev/null +++ b/base64x_simd_amd64.go @@ -0,0 +1,278 @@ +// Copyright 2026 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build go1.27 && goexperiment.simd && amd64 && !noasm && !appengine +// +build go1.27,goexperiment.simd,amd64,!noasm,!appengine + +package base64x + +import ( + "bytes" + "encoding/base64" + "errors" + "simd/archsimd" + + "github.com/cloudwego/base64x/internal/simd" +) + +// Note: under go1.27 && goexperiment.simd, this file replaces base64x_native.go +// and base64x_subr_native_amd64.go with static Go simd/archsimd kernels (no +// runtime executable page loader). On Go <= 1.26 (even if GOEXPERIMENT=simd is +// set), base64x remains on internal/native so _subr__b64decode stays populated +// for bytedance/sonic's JIT (which is gated by !go1.27 in sonic). On Go 1.27+ +// with goexperiment.simd, _subr__b64decode and _subr__b64encode remain 0, +// matching -tags noasm and appengine builds. + +func encode(out *[]byte, src []byte, mode int) { + if !archsimd.X86.AVX2() { + encodeStdlib(out, src, mode) + return + } + url := 0 + if mode&_MODE_URL != 0 { + url = 1 + } + pad := 1 + if mode&_MODE_RAW != 0 { + pad = 0 + } + enc := encodingForModeSimd(mode) + dst := *out + start := len(dst) + need := enc.EncodedLen(len(src)) + dst = dst[:start+need] + simd.B64_encode(src, uint64(len(src)), dst[start:start+need], uint64(need), url, pad) + *out = dst +} + +func decode(out *[]byte, src []byte, mode int) (int, error) { + input := src + var escBuf [256]byte + var escMapBuf [256]int + var posMap []int + if mode&_MODE_JSON != 0 { + var err error + input, posMap, err = unescapeJSONBase64(src, escBuf[:0], escMapBuf[:0]) + if err != nil { + return 0, err + } + } + if !archsimd.X86.AVX2() { + n, err := decodeStdlib(out, input, mode) + if err != nil { + return 0, remapDecodeError(err, posMap, len(src)) + } + return n, nil + } + + url := 0 + if mode&_MODE_URL != 0 { + url = 1 + } + pad := 1 + if mode&_MODE_RAW != 0 { + pad = 0 + } + enc := encodingForModeSimd(mode) + dst := *out + start := len(dst) + availCap := cap(dst) - start + need := enc.DecodedLen(len(input)) + + // simd.B64_decode bounds 32-byte vector stores to (dst_cap - offset) >= 32 + // and writes any remaining tail bytes individually up to the decoded count. + // Use a scratch buffer only when the caller of DecodeUnsafe supplied a slice + // whose remaining capacity is smaller than DecodedLen(len(input)). + if availCap < need { + var tmp [256]byte + var buf []byte + if need <= len(tmp) { + buf = tmp[:need] + } else { + buf = make([]byte, need) + } + var posErr uint64 + w := simd.B64_decode(input, uint64(len(input)), buf, uint64(len(buf)), url, pad, &posErr) + if posErr != ^uint64(0) { + return 0, mapCorruptPos(posErr, posMap, len(src)) + } + dst = dst[:start+int(w)] + copy(dst[start:], buf[:w]) + *out = dst + return int(w), nil + } + + var posErr uint64 + w := simd.B64_decode(input, uint64(len(input)), dst[start:cap(dst)], uint64(availCap), url, pad, &posErr) + if posErr != ^uint64(0) { + return 0, mapCorruptPos(posErr, posMap, len(src)) + } + *out = dst[:start+int(w)] + return int(w), nil +} + +func encodingForModeSimd(mode int) *base64.Encoding { + switch mode & (_MODE_URL | _MODE_RAW) { + case _MODE_URL: + return base64.URLEncoding + case _MODE_RAW: + return base64.RawStdEncoding + case _MODE_URL | _MODE_RAW: + return base64.RawURLEncoding + default: + return base64.StdEncoding + } +} + +func encodeStdlib(out *[]byte, src []byte, mode int) { + enc := encodingForModeSimd(mode) + dst := *out + start := len(dst) + need := enc.EncodedLen(len(src)) + dst = dst[:start+need] + enc.Encode(dst[start:], src) + *out = dst +} + +func decodeStdlib(out *[]byte, input []byte, mode int) (int, error) { + enc := encodingForModeSimd(mode) + dst := *out + start := len(dst) + buf := dst[start:cap(dst)] + n, err := enc.Decode(buf, input) + if err != nil { + return 0, err + } + *out = dst[:start+n] + return n, nil +} + +func unquoteJSONBase64(src []byte) ([]byte, error) { + out, _, err := unescapeJSONBase64(src, nil, nil) + return out, err +} + +// unescapeJSONBase64 matches the JSON escape subset of native/native.h +// (unescape_asc): \r, \n, \/, and ASCII \uXXXX (< 0x80). Raw \r and \n remain +// untouched for the base64 decoder to skip, and any invalid escape returns +// base64.CorruptInputError at the backslash index in src. +func unescapeJSONBase64(src []byte, dstBuf []byte, mapBuf []int) ([]byte, []int, error) { + first := bytes.IndexByte(src, '\\') + if first < 0 { + return src, nil, nil + } + + var out []byte + var posMap []int + if cap(dstBuf) >= len(src) { + out = dstBuf[:0] + } else { + out = make([]byte, 0, len(src)) + } + if cap(mapBuf) >= len(src) { + posMap = mapBuf[:0] + } else { + posMap = make([]int, 0, len(src)) + } + + out = append(out, src[:first]...) + for i := 0; i < first; i++ { + posMap = append(posMap, i) + } + + for i := first; i < len(src); { + b := src[i] + if b != '\\' { + out = append(out, b) + posMap = append(posMap, i) + i++ + continue + } + if i+1 >= len(src) { + return nil, nil, base64.CorruptInputError(i) + } + switch src[i+1] { + case 'r': + out = append(out, '\r') + posMap = append(posMap, i) + i += 2 + case 'n': + out = append(out, '\n') + posMap = append(posMap, i) + i += 2 + case '/': + out = append(out, '/') + posMap = append(posMap, i) + i += 2 + case 'u': + if i+6 > len(src) { + return nil, nil, base64.CorruptInputError(i) + } + v, ok := parseHex4ASCII(src[i+2 : i+6]) + if !ok { + return nil, nil, base64.CorruptInputError(i) + } + out = append(out, v) + posMap = append(posMap, i) + i += 6 + default: + return nil, nil, base64.CorruptInputError(i) + } + } + return out, posMap, nil +} + +func parseHex4ASCII(s []byte) (byte, bool) { + var v uint16 + for i := 0; i < 4; i++ { + c := s[i] + var d uint16 + switch { + case c >= '0' && c <= '9': + d = uint16(c - '0') + case c >= 'a' && c <= 'f': + d = uint16(c - 'a' + 10) + case c >= 'A' && c <= 'F': + d = uint16(c - 'A' + 10) + default: + return 0, false + } + v = (v << 4) | d + } + if v >= 0x80 { + return 0, false + } + return byte(v), true +} + +func mapCorruptPos(pos uint64, posMap []int, srcLen int) error { + if posMap == nil { + return base64.CorruptInputError(pos) + } + if pos < uint64(len(posMap)) { + return base64.CorruptInputError(posMap[pos]) + } + return base64.CorruptInputError(srcLen) +} + +func remapDecodeError(err error, posMap []int, srcLen int) error { + if posMap == nil { + return err + } + var ce base64.CorruptInputError + if errors.As(err, &ce) && ce >= 0 { + return mapCorruptPos(uint64(ce), posMap, srcLen) + } + return err +} diff --git a/base64x_simd_amd64_test.go b/base64x_simd_amd64_test.go new file mode 100644 index 0000000..d9605da --- /dev/null +++ b/base64x_simd_amd64_test.go @@ -0,0 +1,125 @@ +// Copyright 2026 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build go1.27 && goexperiment.simd && amd64 && !noasm && !appengine +// +build go1.27,goexperiment.simd,amd64,!noasm,!appengine + +package base64x + +import ( + "bytes" + "encoding/base64" + "errors" + "testing" +) + +func TestSimdJSONModeSemanticsAndErrorOffsets(t *testing.T) { + // Raw CRLF is accepted in JSONStdEncoding, matching native/native.h. + got, err := JSONStdEncoding.DecodeString("Zm9v\r\nYmFy") + if err != nil { + t.Fatalf("JSONStdEncoding.DecodeString(CRLF) error = %v", err) + } + if string(got) != "foobar" { + t.Fatalf("JSONStdEncoding.DecodeString(CRLF) = %q, want %q", got, "foobar") + } + + // Invalid escapes and raw quotes always return base64.CorruptInputError + // (never *json.SyntaxError) with offsets mapped back to src. + errCases := []struct { + in string + wantPos base64.CorruptInputError + }{ + {`Zm9v\x`, 4}, + {`Zm9v\`, 4}, + {`Zm9v\\YmFy`, 4}, + {`FPuc\u00809l+`, 4}, + {`QUJ"D`, 3}, + {`\/\/\/!`, 6}, + {`\u0051!`, 6}, + {`1234\/`, 4}, + } + for _, tc := range errCases { + _, err := JSONStdEncoding.DecodeString(tc.in) + var ce base64.CorruptInputError + if !errors.As(err, &ce) { + t.Fatalf("JSONStdEncoding.DecodeString(%q) error = %T (%v), want base64.CorruptInputError", tc.in, err, err) + } + if ce != tc.wantPos { + t.Fatalf("JSONStdEncoding.DecodeString(%q) error pos = %d, want %d", tc.in, int64(ce), int64(tc.wantPos)) + } + } + + // Verify decodeStdlib error remapping on the non-AVX2 helper path as well. + input, posMap, err := unescapeJSONBase64([]byte(`\u0051!`), nil, nil) + if err != nil { + t.Fatalf("unescapeJSONBase64() unexpected error = %v", err) + } + out := make([]byte, 0, 8) + _, err = decodeStdlib(&out, input, _MODE_JSON) + remapped := remapDecodeError(err, posMap, len(`\u0051!`)) + if remapped != base64.CorruptInputError(6) { + t.Fatalf("remapDecodeError() = %T (%v), want CorruptInputError(6)", remapped, remapped) + } +} + +func TestSimdZeroAllocDecodeAndJSON(t *testing.T) { + raw := bytes.Repeat([]byte("abc"), 1365) // 4095 bytes (> 224 bytes threshold) + encoded := []byte(StdEncoding.EncodeToString(raw)) + dst := make([]byte, StdEncoding.DecodedLen(len(encoded))) + + allocs := testing.AllocsPerRun(100, func() { + n, err := StdEncoding.Decode(dst, encoded) + if err != nil || n != len(raw) { + t.Fatalf("Decode() = (%d, %v)", n, err) + } + }) + if allocs != 0 { + t.Fatalf("StdEncoding.Decode(4kB) allocs = %v, want 0", allocs) + } + + jsonEsc := []byte(`FPu\rcA9l+\n`) + jsonDst := make([]byte, JSONStdEncoding.DecodedLen(len(jsonEsc))) + jsonAllocs := testing.AllocsPerRun(100, func() { + n, err := JSONStdEncoding.Decode(jsonDst, jsonEsc) + if err != nil || n != 6 { + t.Fatalf("JSONStdEncoding.Decode() = (%d, %v)", n, err) + } + }) + if jsonAllocs != 0 { + t.Fatalf("JSONStdEncoding.Decode(escaped <=256B) allocs = %v, want 0", jsonAllocs) + } +} + +func BenchmarkSimdEncode_4kB(b *testing.B) { + src := bytes.Repeat([]byte("abc"), 1365) // 4095 bytes + dst := make([]byte, StdEncoding.EncodedLen(len(src))) + b.SetBytes(int64(len(src))) + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + StdEncoding.Encode(dst, src) + } +} + +func BenchmarkSimdDecode_4kB(b *testing.B) { + raw := bytes.Repeat([]byte("abc"), 1365) // 4095 bytes + src := []byte(StdEncoding.EncodeToString(raw)) + dst := make([]byte, StdEncoding.DecodedLen(len(src))) + b.SetBytes(int64(len(src))) + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, _ = StdEncoding.Decode(dst, src) + } +} diff --git a/base64x_subr_amd64.go b/base64x_subr_amd64.go index 06ae945..0dfe8a8 100644 --- a/base64x_subr_amd64.go +++ b/base64x_subr_amd64.go @@ -17,7 +17,10 @@ package base64x -// HACK: maintain these only to prevent breakchange, because sonic-go linkname these +// HACK: maintain these only to prevent breakchange, because sonic-go linkname these. +// They are populated by base64x_subr_native_amd64.go when the native loader is active +// (including all Go <= 1.26 default builds where sonic's JIT is enabled), and remain 0 +// under -tags noasm, appengine, and go1.27 && goexperiment.simd. var ( _subr__b64decode uintptr _subr__b64encode uintptr diff --git a/base64x_subr_native_amd64.go b/base64x_subr_native_amd64.go index 094ad84..417b861 100644 --- a/base64x_subr_native_amd64.go +++ b/base64x_subr_native_amd64.go @@ -12,8 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine package base64x diff --git a/internal/native/avx2/b64decode.go b/internal/native/avx2/b64decode.go index cb5243c..30b8392 100644 --- a/internal/native/avx2/b64decode.go +++ b/internal/native/avx2/b64decode.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by Bash, DO NOT EDIT. diff --git a/internal/native/avx2/b64decode_subr.go b/internal/native/avx2/b64decode_subr.go index 3d82251..67aa482 100644 --- a/internal/native/avx2/b64decode_subr.go +++ b/internal/native/avx2/b64decode_subr.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by asm2asm, DO NOT EDIT. diff --git a/internal/native/avx2/b64decode_text_amd64.go b/internal/native/avx2/b64decode_text_amd64.go index 9173dd2..3786998 100644 --- a/internal/native/avx2/b64decode_text_amd64.go +++ b/internal/native/avx2/b64decode_text_amd64.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by asm2asm, DO NOT EDIT. diff --git a/internal/native/avx2/b64encode.go b/internal/native/avx2/b64encode.go index eb17d88..5e3db6d 100644 --- a/internal/native/avx2/b64encode.go +++ b/internal/native/avx2/b64encode.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by Bash, DO NOT EDIT. diff --git a/internal/native/avx2/b64encode_subr.go b/internal/native/avx2/b64encode_subr.go index 736ef22..b688b8b 100644 --- a/internal/native/avx2/b64encode_subr.go +++ b/internal/native/avx2/b64encode_subr.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by asm2asm, DO NOT EDIT. diff --git a/internal/native/avx2/b64encode_text_amd64.go b/internal/native/avx2/b64encode_text_amd64.go index 97a2f28..14f1e02 100644 --- a/internal/native/avx2/b64encode_text_amd64.go +++ b/internal/native/avx2/b64encode_text_amd64.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by asm2asm, DO NOT EDIT. diff --git a/internal/native/avx2/native_export.go b/internal/native/avx2/native_export.go index 39a7cba..e63e5eb 100644 --- a/internal/native/avx2/native_export.go +++ b/internal/native/avx2/native_export.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by Bash, DO NOT EDIT. diff --git a/internal/native/b64decode.tmpl b/internal/native/b64decode.tmpl index cd61808..dfd1476 100644 --- a/internal/native/b64decode.tmpl +++ b/internal/native/b64decode.tmpl @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by Bash, DO NOT EDIT. diff --git a/internal/native/b64encode.tmpl b/internal/native/b64encode.tmpl index 5cb3513..019369c 100644 --- a/internal/native/b64encode.tmpl +++ b/internal/native/b64encode.tmpl @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by Bash, DO NOT EDIT. diff --git a/internal/native/dispatch.go b/internal/native/dispatch.go index 73a9a18..acfcb35 100644 --- a/internal/native/dispatch.go +++ b/internal/native/dispatch.go @@ -12,8 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine package native diff --git a/internal/native/native_export.tmpl b/internal/native/native_export.tmpl index 984034f..8078577 100644 --- a/internal/native/native_export.tmpl +++ b/internal/native/native_export.tmpl @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by Bash, DO NOT EDIT. diff --git a/internal/native/native_test.go b/internal/native/native_test.go index fc53a0d..4b2e284 100644 --- a/internal/native/native_test.go +++ b/internal/native/native_test.go @@ -1,21 +1,22 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +// Copyright 2025 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. -/* - * Copyright 2025 CloudWeGo Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine package native diff --git a/internal/native/sse/b64decode.go b/internal/native/sse/b64decode.go index 7c01375..20fe8c3 100644 --- a/internal/native/sse/b64decode.go +++ b/internal/native/sse/b64decode.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by Bash, DO NOT EDIT. diff --git a/internal/native/sse/b64decode_subr.go b/internal/native/sse/b64decode_subr.go index b0774ee..4c32b87 100644 --- a/internal/native/sse/b64decode_subr.go +++ b/internal/native/sse/b64decode_subr.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by asm2asm, DO NOT EDIT. diff --git a/internal/native/sse/b64decode_text_amd64.go b/internal/native/sse/b64decode_text_amd64.go index b7fe722..d1b2ff1 100644 --- a/internal/native/sse/b64decode_text_amd64.go +++ b/internal/native/sse/b64decode_text_amd64.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by asm2asm, DO NOT EDIT. diff --git a/internal/native/sse/b64encode.go b/internal/native/sse/b64encode.go index 9609ec9..3140e62 100644 --- a/internal/native/sse/b64encode.go +++ b/internal/native/sse/b64encode.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by Bash, DO NOT EDIT. diff --git a/internal/native/sse/b64encode_subr.go b/internal/native/sse/b64encode_subr.go index deb12c0..73b1b7f 100644 --- a/internal/native/sse/b64encode_subr.go +++ b/internal/native/sse/b64encode_subr.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by asm2asm, DO NOT EDIT. diff --git a/internal/native/sse/b64encode_text_amd64.go b/internal/native/sse/b64encode_text_amd64.go index 84cd54d..0263094 100644 --- a/internal/native/sse/b64encode_text_amd64.go +++ b/internal/native/sse/b64encode_text_amd64.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by asm2asm, DO NOT EDIT. diff --git a/internal/native/sse/native_export.go b/internal/native/sse/native_export.go index 6a9d9e7..23829d3 100644 --- a/internal/native/sse/native_export.go +++ b/internal/native/sse/native_export.go @@ -1,5 +1,8 @@ -//go:build amd64 && !noasm && !appengine -// +build amd64,!noasm,!appengine +//go:build (!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine +// +build !go1.27 !goexperiment.simd +// +build amd64 +// +build !noasm +// +build !appengine // Code generated by Bash, DO NOT EDIT. diff --git a/internal/simd/b64_gen.go b/internal/simd/b64_gen.go new file mode 100644 index 0000000..2c87e37 --- /dev/null +++ b/internal/simd/b64_gen.go @@ -0,0 +1,311 @@ +//go:build goexperiment.simd && amd64 + +// Code generated by sgoiter emit go127; DO NOT EDIT. +// sgoiter-stamp: sha256=8c4229c7b8ba80231b22b3f6f7e3bf620480aace1357e583e0881d0026d0bfa4 +package simd + +import ( + "math/bits" + "simd/archsimd" +) + +var ( + TabEncodeCharsetStd = [64]byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'} + TabEncodeCharsetURL = [64]byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'} + VecEncodeShuffles = [32]byte{1, 0, 2, 1, 4, 3, 5, 4, 7, 6, 8, 7, 10, 9, 11, 10, 1, 0, 2, 1, 4, 3, 5, 4, 7, 6, 8, 7, 10, 9, 11, 10} + VecEncodeCharsetStd = [32]byte{71, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 237, 240, 'A', 0, 0, 71, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 237, 240, 'A', 0, 0} + VecEncodeCharsetURL = [32]byte{71, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 239, 32, 'A', 0, 0, 71, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 239, 32, 'A', 0, 0} + VecPacking = [32]byte{2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, 128, 128, 128, 128, 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, 128, 128, 128, 128} + VecDecodeBits = [32]byte{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} + VecDecodeTableStd = [128]byte{0x00, 0x00, 0x13, 0x04, 0xbf, 0xbf, 0xb9, 0xb9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x04, 0xbf, 0xbf, 0xb9, 0xb9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0x54, 0x50, 0x50, 0x50, 0x54, 0xa8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0x54, 0x50, 0x50, 0x50, 0x54, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10} + VecDecodeTableURL = [128]byte{0x00, 0x00, 0x11, 0x04, 0xbf, 0xbf, 0xb9, 0xb9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x04, 0xbf, 0xbf, 0xb9, 0xb9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0x50, 0x50, 0x54, 0x50, 0x70, 0xa8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0x50, 0x50, 0x54, 0x50, 0x70, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0} + TabDecodeCharsetStd = [256]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 62, 0xff, 0xff, 0xff, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} + TabDecodeCharsetURL = [256]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 62, 0xff, 0xff, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xff, 0xff, 0xff, 0xff, 63, 0xff, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} +) + +func __avx2() int { + v4 := func() int { + if archsimd.X86.AVX2() { + return 1 + } + return 0 + }() + return v4 +} + +func b64_encode_tail(src []byte, src_len uint64, i uint64, dst []byte, o uint64, url_mode int, pad_mode int) uint64 { + var v9 uint32 + var v11 []byte + var v91 uint64 + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + v11 = TabEncodeCharsetStd[:] + if url_mode != 0 { + v11 = TabEncodeCharsetURL[:] + } + for (src_len - i) >= 3 { + v9 = uint32((uint64(uint64(uint32(src[i]))<<16) | uint64(uint64(uint32(src[i+1]))<<8)) | uint64(uint32(src[i+2]))) + v34 := uint64(uint64(v9)>>18) & 63 + dst[o] = v11[v34] + v39 := uint64(uint64(v9)>>12) & 63 + dst[o+1] = v11[v39] + v46 := uint64(uint64(v9)>>6) & 63 + dst[o+2] = v11[v46] + v51 := v9 & 63 + dst[o+3] = v11[v51] + i += 3 + o += 4 + } + v59 := src_len - i + if v59 == 0 { + return o + } + v9 = uint32(uint64(uint64(uint32(src[i])) << 16)) + if v59 == 2 { + v9 = uint32(uint64(v9) | uint64(uint64(uint32(src[i+1]))<<8)) + } + v76 := uint64(uint64(v9)>>18) & 63 + dst[o] = v11[v76] + v81 := uint64(uint64(v9)>>12) & 63 + dst[o+1] = v11[v81] + o += 2 + if v59 == 2 { + v91 = uint64(uint64(v9)>>6) & 63 + dst[o] = v11[v91] + o++ + if pad_mode != 0 { + dst[o] = '=' + o++ + } + } else { + if pad_mode != 0 { + dst[o] = '=' + dst[o+1] = '=' + o += 2 + } + } + return o +} + +func B64_encode_scalar(src []byte, src_len uint64, dst []byte, dst_cap uint64, url_mode int, pad_mode int) uint64 { + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + return uint64(b64_encode_tail(src[:], src_len, uint64(0), dst[:], uint64(0), url_mode, pad_mode)) +} + +func B64_encode_avx2(src []byte, src_len uint64, dst []byte, dst_cap uint64, url_mode int, pad_mode int) uint64 { + var v9 uint64 + var v11 uint64 + var v13 []byte + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + v9 = 0 + v11 = v9 + v13 = VecEncodeCharsetStd[:] + if url_mode != 0 { + v13 = VecEncodeCharsetURL[:] + } + for { + v21 := ((src_len - v9) >= 28) && ((dst_cap - v11) >= 32) + if !v21 { + break + } + v41 := archsimd.BroadcastUint8x32(0).SetLo(archsimd.LoadUint8x16(src[v9:][:16])).SetHi(archsimd.LoadUint8x16(src[int(v9+12):][:16])).PermuteOrZeroGrouped(archsimd.LoadUint8x32(VecEncodeShuffles[0:32]).BitsToInt8()) + v59 := v41.And(archsimd.BroadcastUint32x8(uint32(264305664)).ReshapeToUint8s()).ReshapeToUint16s().MulHigh(archsimd.BroadcastUint32x8(uint32(67108928)).ReshapeToUint8s().ReshapeToUint16s()).ReshapeToUint8s().Or(v41.And(archsimd.BroadcastUint32x8(uint32(4129776)).ReshapeToUint8s()).ReshapeToUint16s().BitsToInt16().Mul(archsimd.BroadcastUint32x8(uint32(16777232)).ReshapeToUint8s().ReshapeToUint16s().BitsToInt16()).ToBits().ReshapeToUint8s()) + v59.Add(archsimd.LoadUint8x32(v13[0:32]).PermuteOrZeroGrouped(v59.SubSaturated(archsimd.BroadcastUint8x32(byte(51))).Or(archsimd.BroadcastUint8x32(byte(13)).And(archsimd.BroadcastUint8x32(byte(26)).BitsToInt8().Greater(v59.BitsToInt8()).ToInt8x32().ToBits())).BitsToInt8())).Store(dst[v11:][:32]) + v9 += 24 + v11 += 32 + } + return uint64(b64_encode_tail(src[:], src_len, v9, dst[:], v11, url_mode, pad_mode)) +} + +func B64_encode(src []byte, src_len uint64, dst []byte, dst_cap uint64, url_mode int, pad_mode int) uint64 { + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + if archsimd.X86.AVX2() { + return uint64(B64_encode_avx2(src[:], src_len, dst[:], dst_cap, url_mode, pad_mode)) + } + return uint64(B64_encode_scalar(src[:], src_len, dst[:], dst_cap, url_mode, pad_mode)) +} + +func b64_decode_tail(src []byte, src_len uint64, si uint64, dst []byte, o uint64, url_mode int, pad_mode int, pos_err *uint64) uint64 { + var v10 []byte + var v11 uint32 + var v14 int + var v15 int + var v16 uint64 + var v24 int + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + v10 = TabDecodeCharsetStd[:] + if url_mode != 0 { + v10 = TabDecodeCharsetURL[:] + } + for si < src_len { + v11 = 0 + v15 = 4 + v16 = 0xffffffffffffffff + v14 = 0 + for v14 < 4 { + if si == src_len { + if v14 == 0 { + *pos_err = 0xffffffffffffffff + return o + } + if (v14 == 1) || (pad_mode != 0) { + *pos_err = si - uint64(v14) + return o + } + v15 = v14 + break + } + v34 := src[si] + si++ + v37 := v10[v34] + if v37 != 0xff { + v11 = uint32(uint64(v11) | uint64(uint64(uint32(v37))<> 16)) + if v15 >= 3 { + dst[o+1] = byte(uint64(uint64(v11) >> 8)) + } + if v15 == 4 { + dst[o+2] = byte(v11) + } + o += uint64(v15 - 1) + if v16 != 0xffffffffffffffff { + *pos_err = v16 + return o + } + } + *pos_err = 0xffffffffffffffff + return o +} + +func B64_decode_scalar(src []byte, src_len uint64, dst []byte, dst_cap uint64, url_mode int, pad_mode int, pos_err *uint64) uint64 { + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + return uint64(b64_decode_tail(src[:], src_len, uint64(0), dst[:], uint64(0), url_mode, pad_mode, pos_err)) +} + +func B64_decode_avx2(src []byte, src_len uint64, dst []byte, dst_cap uint64, url_mode int, pad_mode int, pos_err *uint64) uint64 { + var v11 uint64 + var v13 uint64 + var v17 []byte + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + v11 = 0 + v13 = v11 + v17 = VecDecodeTableStd[:] + if url_mode != 0 { + v17 = VecDecodeTableURL[:] + } + for { + v25 := ((src_len - v11) >= 32) && ((dst_cap - v13) >= 32) + if !v25 { + break + } + v30 := archsimd.LoadUint8x32(src[v11:][:32]) + v36 := archsimd.BroadcastUint8x32(byte(15)) + v41 := v30.ReshapeToUint32s().ShiftAllRight(4).ReshapeToUint8s().And(v36) + v109 := archsimd.LoadUint32x8Array(&[8]uint32{uint32(0), uint32(1), uint32(2), uint32(4), uint32(5), uint32(6), uint32(3), uint32(7)}).ReshapeToUint8s() + v110 := v30.Add(archsimd.LoadUint8x32(v17[96:128]).IfElse(v30.Equal(archsimd.LoadUint8x32(v17[64:96])).ToInt8x32().ToBits().BitsToInt8().Less(archsimd.BroadcastInt8x32(0)), archsimd.LoadUint8x32(v17[0:32]).PermuteOrZeroGrouped(v41.BitsToInt8()))).And(archsimd.BroadcastUint8x32(byte(63))).DotProductPairsSaturated(archsimd.BroadcastUint32x8(uint32(20971840)).ReshapeToUint8s().BitsToInt8()).ToBits().ReshapeToUint8s().ReshapeToUint16s().BitsToInt16().DotProductPairs(archsimd.BroadcastUint32x8(uint32(69632)).ReshapeToUint8s().ReshapeToUint16s().BitsToInt16()).ToBits().ReshapeToUint8s().PermuteOrZeroGrouped(archsimd.LoadUint8x32(VecPacking[0:32]).BitsToInt8()).ReshapeToUint32s().Permute(v109.ReshapeToUint32s()).ReshapeToUint8s() + if int32(bits.TrailingZeros64(uint64(int64(int(int32(archsimd.LoadUint8x32(v17[32:64]).PermuteOrZeroGrouped(v30.And(v36).BitsToInt8()).And(archsimd.LoadUint8x32(VecDecodeBits[0:32]).PermuteOrZeroGrouped(v41.BitsToInt8())).Equal(archsimd.BroadcastUint8x32(byte(0))).ToBits())))|int64(-4294967296)))) < 32 { + break + } + v110.Store(dst[v13:][:32]) + v11 += 32 + v13 += 24 + } + return uint64(b64_decode_tail(src[:], src_len, v11, dst[:], v13, url_mode, pad_mode, pos_err)) +} + +func B64_decode(src []byte, src_len uint64, dst []byte, dst_cap uint64, url_mode int, pad_mode int, pos_err *uint64) uint64 { + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + if archsimd.X86.AVX2() { + return uint64(B64_decode_avx2(src[:], src_len, dst[:], dst_cap, url_mode, pad_mode, pos_err)) + } + return uint64(B64_decode_scalar(src[:], src_len, dst[:], dst_cap, url_mode, pad_mode, pos_err)) +} + +// IRCosts counted from IR opcodes via rodata.IROpCost; not a kernel-name table. +const ( + __avx2_IRCost = uint64(6) + B64_encode_tail_IRCost = uint64(122) + B64_encode_scalar_IRCost = uint64(4) + B64_encode_avx2_IRCost = uint64(88) + B64_encode_IRCost = uint64(5) + B64_decode_tail_IRCost = uint64(144) + B64_decode_scalar_IRCost = uint64(4) + B64_decode_avx2_IRCost = uint64(125) + B64_decode_IRCost = uint64(5) +) diff --git a/internal/simd/b64_nosimd_gen.go b/internal/simd/b64_nosimd_gen.go new file mode 100644 index 0000000..acd21ac --- /dev/null +++ b/internal/simd/b64_nosimd_gen.go @@ -0,0 +1,227 @@ +//go:build !goexperiment.simd || !amd64 + +// Code generated by sgoiter emit go127; DO NOT EDIT. +// sgoiter-stamp: sha256=047ae148fa4614d63ab852dbd74f52171de727ab3e8e0fd127dfe8d2b6611cfb +package simd + +var ( + TabEncodeCharsetStd = [64]byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'} + TabEncodeCharsetURL = [64]byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'} + VecEncodeShuffles = [32]byte{1, 0, 2, 1, 4, 3, 5, 4, 7, 6, 8, 7, 10, 9, 11, 10, 1, 0, 2, 1, 4, 3, 5, 4, 7, 6, 8, 7, 10, 9, 11, 10} + VecEncodeCharsetStd = [32]byte{71, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 237, 240, 'A', 0, 0, 71, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 237, 240, 'A', 0, 0} + VecEncodeCharsetURL = [32]byte{71, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 239, 32, 'A', 0, 0, 71, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 239, 32, 'A', 0, 0} + VecPacking = [32]byte{2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, 128, 128, 128, 128, 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, 128, 128, 128, 128} + VecDecodeBits = [32]byte{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} + VecDecodeTableStd = [128]byte{0x00, 0x00, 0x13, 0x04, 0xbf, 0xbf, 0xb9, 0xb9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x04, 0xbf, 0xbf, 0xb9, 0xb9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0x54, 0x50, 0x50, 0x50, 0x54, 0xa8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0x54, 0x50, 0x50, 0x50, 0x54, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10} + VecDecodeTableURL = [128]byte{0x00, 0x00, 0x11, 0x04, 0xbf, 0xbf, 0xb9, 0xb9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x04, 0xbf, 0xbf, 0xb9, 0xb9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0x50, 0x50, 0x54, 0x50, 0x70, 0xa8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0x50, 0x50, 0x54, 0x50, 0x70, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0} + TabDecodeCharsetStd = [256]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 62, 0xff, 0xff, 0xff, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} + TabDecodeCharsetURL = [256]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 62, 0xff, 0xff, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xff, 0xff, 0xff, 0xff, 63, 0xff, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} +) + +func b64_encode_tail(src []byte, src_len uint64, i uint64, dst []byte, o uint64, url_mode int, pad_mode int) uint64 { + var v9 uint32 + var v11 []byte + var v91 uint64 + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + v11 = TabEncodeCharsetStd[:] + if url_mode != 0 { + v11 = TabEncodeCharsetURL[:] + } + for (src_len - i) >= 3 { + v9 = uint32((uint64(uint64(uint32(src[i]))<<16) | uint64(uint64(uint32(src[i+1]))<<8)) | uint64(uint32(src[i+2]))) + v34 := uint64(uint64(v9)>>18) & 63 + dst[o] = v11[v34] + v39 := uint64(uint64(v9)>>12) & 63 + dst[o+1] = v11[v39] + v46 := uint64(uint64(v9)>>6) & 63 + dst[o+2] = v11[v46] + v51 := v9 & 63 + dst[o+3] = v11[v51] + i += 3 + o += 4 + } + v59 := src_len - i + if v59 == 0 { + return o + } + v9 = uint32(uint64(uint64(uint32(src[i])) << 16)) + if v59 == 2 { + v9 = uint32(uint64(v9) | uint64(uint64(uint32(src[i+1]))<<8)) + } + v76 := uint64(uint64(v9)>>18) & 63 + dst[o] = v11[v76] + v81 := uint64(uint64(v9)>>12) & 63 + dst[o+1] = v11[v81] + o += 2 + if v59 == 2 { + v91 = uint64(uint64(v9)>>6) & 63 + dst[o] = v11[v91] + o++ + if pad_mode != 0 { + dst[o] = '=' + o++ + } + } else { + if pad_mode != 0 { + dst[o] = '=' + dst[o+1] = '=' + o += 2 + } + } + return o +} + +func B64_encode_scalar(src []byte, src_len uint64, dst []byte, dst_cap uint64, url_mode int, pad_mode int) uint64 { + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + return uint64(b64_encode_tail(src[:], src_len, uint64(0), dst[:], uint64(0), url_mode, pad_mode)) +} + +func B64_encode(src []byte, src_len uint64, dst []byte, dst_cap uint64, url_mode int, pad_mode int) uint64 { + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + return uint64(B64_encode_scalar(src[:], src_len, dst[:], dst_cap, url_mode, pad_mode)) +} + +func b64_decode_tail(src []byte, src_len uint64, si uint64, dst []byte, o uint64, url_mode int, pad_mode int, pos_err *uint64) uint64 { + var v10 []byte + var v11 uint32 + var v14 int + var v15 int + var v16 uint64 + var v24 int + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + v10 = TabDecodeCharsetStd[:] + if url_mode != 0 { + v10 = TabDecodeCharsetURL[:] + } + for si < src_len { + v11 = 0 + v15 = 4 + v16 = 0xffffffffffffffff + v14 = 0 + for v14 < 4 { + if si == src_len { + if v14 == 0 { + *pos_err = 0xffffffffffffffff + return o + } + if (v14 == 1) || (pad_mode != 0) { + *pos_err = si - uint64(v14) + return o + } + v15 = v14 + break + } + v34 := src[si] + si++ + v37 := v10[v34] + if v37 != 0xff { + v11 = uint32(uint64(v11) | uint64(uint64(uint32(v37))<> 16)) + if v15 >= 3 { + dst[o+1] = byte(uint64(uint64(v11) >> 8)) + } + if v15 == 4 { + dst[o+2] = byte(v11) + } + o += uint64(v15 - 1) + if v16 != 0xffffffffffffffff { + *pos_err = v16 + return o + } + } + *pos_err = 0xffffffffffffffff + return o +} + +func B64_decode_scalar(src []byte, src_len uint64, dst []byte, dst_cap uint64, url_mode int, pad_mode int, pos_err *uint64) uint64 { + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + return uint64(b64_decode_tail(src[:], src_len, uint64(0), dst[:], uint64(0), url_mode, pad_mode, pos_err)) +} + +func B64_decode(src []byte, src_len uint64, dst []byte, dst_cap uint64, url_mode int, pad_mode int, pos_err *uint64) uint64 { + if uint64(len(src)) < src_len { + src_len = uint64(len(src)) + } + return uint64(B64_decode_scalar(src[:], src_len, dst[:], dst_cap, url_mode, pad_mode, pos_err)) +} + +// IRCosts counted from IR opcodes via rodata.IROpCost; not a kernel-name table. +const ( + B64_encode_tail_IRCost = uint64(122) + B64_encode_scalar_IRCost = uint64(4) + B64_encode_IRCost = uint64(2) + B64_decode_tail_IRCost = uint64(144) + B64_decode_scalar_IRCost = uint64(4) + B64_decode_IRCost = uint64(2) +)