mirror of
https://github.com/fumiama/go-base16384.git
synced 2026-06-25 06:09:55 +08:00
fix generic panic, add movbe check
This commit is contained in:
46
base14_amd64.go
Normal file
46
base14_amd64.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
//go:build amd64
|
||||||
|
// +build amd64
|
||||||
|
|
||||||
|
package base14
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:noescape
|
||||||
|
//go:nosplit
|
||||||
|
func _encode(offset, outlen int, b, encd []byte) (sum uint64, n uint64)
|
||||||
|
|
||||||
|
//go:noescape
|
||||||
|
//go:nosplit
|
||||||
|
func _decode(offset, outlen int, b, decd []byte)
|
||||||
|
|
||||||
|
func encode(offset, outlen int, b, encd []byte) {
|
||||||
|
if movbe {
|
||||||
|
if len(b) == 7 {
|
||||||
|
b = append(b, 0)
|
||||||
|
}
|
||||||
|
sum, n := _encode(offset, outlen, b, encd)
|
||||||
|
if offset == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var tmp [8]byte
|
||||||
|
binary.LittleEndian.PutUint64(tmp[:], sum)
|
||||||
|
copy(encd[n:], tmp[:])
|
||||||
|
encd[outlen-2] = '='
|
||||||
|
encd[outlen-1] = byte(offset)
|
||||||
|
} else {
|
||||||
|
encodeGeneric(offset, outlen, b, encd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decode(offset, outlen int, b, decd []byte) {
|
||||||
|
if movbe {
|
||||||
|
if offset != 0 && cap(b) == len(b) {
|
||||||
|
b = append(b, make([]byte, 8)...)
|
||||||
|
}
|
||||||
|
_decode(offset, outlen, b, decd)
|
||||||
|
} else {
|
||||||
|
decodeGeneric(offset, outlen, b, decd)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
//go:build amd64
|
|
||||||
// +build amd64
|
|
||||||
|
|
||||||
package base14
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/binary"
|
|
||||||
)
|
|
||||||
|
|
||||||
//go:noescape
|
|
||||||
//go:nosplit
|
|
||||||
func _encode(offset, outlen int, b, encd []byte) (sum uint64, n uint64)
|
|
||||||
|
|
||||||
//go:noescape
|
|
||||||
//go:nosplit
|
|
||||||
func _decode(offset, outlen int, b, decd []byte)
|
|
||||||
|
|
||||||
func encode(offset, outlen int, b, encd []byte) {
|
|
||||||
if len(b) == 7 {
|
|
||||||
b = append(b, 0)
|
|
||||||
}
|
|
||||||
sum, n := _encode(offset, outlen, b, encd)
|
|
||||||
if offset == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var tmp [8]byte
|
|
||||||
binary.LittleEndian.PutUint64(tmp[:], sum)
|
|
||||||
copy(encd[n:], tmp[:])
|
|
||||||
encd[outlen-2] = '='
|
|
||||||
encd[outlen-1] = byte(offset)
|
|
||||||
}
|
|
||||||
|
|
||||||
func decode(offset, outlen int, b, decd []byte) {
|
|
||||||
if offset != 0 && cap(b) == len(b) {
|
|
||||||
b = append(b, make([]byte, 8)...)
|
|
||||||
}
|
|
||||||
_decode(offset, outlen, b, decd)
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,9 @@
|
|||||||
//go:build !amd64
|
|
||||||
// +build !amd64
|
|
||||||
|
|
||||||
package base14
|
package base14
|
||||||
|
|
||||||
import "encoding/binary"
|
import "encoding/binary"
|
||||||
|
|
||||||
//go:nosplit
|
//go:nosplit
|
||||||
func encode(offset, outlen int, b, encd []byte) {
|
func encodeGeneric(offset, outlen int, b, encd []byte) {
|
||||||
var n int
|
var n int
|
||||||
i := 0
|
i := 0
|
||||||
if len(b) == 7 {
|
if len(b) == 7 {
|
||||||
@@ -23,6 +20,9 @@ func encode(offset, outlen int, b, encd []byte) {
|
|||||||
sum += 0x4e004e004e004e00
|
sum += 0x4e004e004e004e00
|
||||||
binary.BigEndian.PutUint64(encd[n:], sum)
|
binary.BigEndian.PutUint64(encd[n:], sum)
|
||||||
} else {
|
} else {
|
||||||
|
if len(b)%7 == 0 {
|
||||||
|
b = append(b, 0)
|
||||||
|
}
|
||||||
for ; i <= len(b)-7; i += 7 {
|
for ; i <= len(b)-7; i += 7 {
|
||||||
shift := binary.BigEndian.Uint64(b[i:]) >> 2
|
shift := binary.BigEndian.Uint64(b[i:]) >> 2
|
||||||
sum := shift
|
sum := shift
|
||||||
@@ -71,7 +71,7 @@ func encode(offset, outlen int, b, encd []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//go:nosplit
|
//go:nosplit
|
||||||
func decode(offset, outlen int, b, decd []byte) {
|
func decodeGeneric(offset, outlen int, b, decd []byte) {
|
||||||
var n uintptr
|
var n uintptr
|
||||||
i := 0
|
i := 0
|
||||||
for ; i <= outlen-7; n += 8 {
|
for ; i <= outlen-7; n += 8 {
|
||||||
|
|||||||
12
base14_noasm.go
Normal file
12
base14_noasm.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
//go:build !amd64
|
||||||
|
// +build !amd64
|
||||||
|
|
||||||
|
package base14
|
||||||
|
|
||||||
|
import _ "unsafe"
|
||||||
|
|
||||||
|
//go:linkname encode github.com/fumiama/go-base16384.encodeGeneric
|
||||||
|
func encode(offset, outlen int, b, encd []byte)
|
||||||
|
|
||||||
|
//go:linkname decode github.com/fumiama/go-base16384.decodeGeneric
|
||||||
|
func decode(offset, outlen int, b, decd []byte)
|
||||||
12
cpuid.go
Normal file
12
cpuid.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
//go:build amd64
|
||||||
|
// +build amd64
|
||||||
|
|
||||||
|
package base14
|
||||||
|
|
||||||
|
func cpuid(op uint32) (eax, ebx, ecx, edx uint32)
|
||||||
|
|
||||||
|
// True when MOVBEx instructions are available.
|
||||||
|
var movbe = func() bool {
|
||||||
|
_, _, c, _ := cpuid(1)
|
||||||
|
return c&(1<<22) > 0
|
||||||
|
}()
|
||||||
15
cpuid_amd64.s
Normal file
15
cpuid_amd64.s
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//go:build amd64
|
||||||
|
// +build amd64
|
||||||
|
|
||||||
|
// Copyright (c) 2015 Klaus Post, released under MIT License. See LICENSE file.
|
||||||
|
|
||||||
|
// func cpuid(op uint32) (eax, ebx, ecx, edx uint32)
|
||||||
|
TEXT ·cpuid(SB), 7, $0
|
||||||
|
XORQ CX, CX
|
||||||
|
MOVL op+0(FP), AX
|
||||||
|
CPUID
|
||||||
|
MOVL AX, eax+8(FP)
|
||||||
|
MOVL BX, ebx+12(FP)
|
||||||
|
MOVL CX, ecx+16(FP)
|
||||||
|
MOVL DX, edx+20(FP)
|
||||||
|
RET
|
||||||
Reference in New Issue
Block a user