From 8ca6a66d70e5d58e580b2fc1e4086b92b4d4baf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Mu=C5=82a?= Date: Thu, 17 Mar 2022 13:16:24 +0100 Subject: [PATCH] Add runtime selection between plain x86 and BMI2 routines --- zstd/seqdec_amd64.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/zstd/seqdec_amd64.go b/zstd/seqdec_amd64.go index bccf926b53..b6832ee257 100644 --- a/zstd/seqdec_amd64.go +++ b/zstd/seqdec_amd64.go @@ -5,6 +5,8 @@ package zstd import ( "fmt" + + "github.com/klauspost/compress/internal/cpuinfo" ) type decodeAsmContext struct { @@ -30,6 +32,21 @@ const errorMatchLenTooBig = 2 // Please refer to seqdec_generic.go for the reference implementation. func sequenceDecs_decode_amd64(s *sequenceDecs, br *bitReader, ctx *decodeAsmContext) int +// sequenceDecs_decode implements the main loop of sequenceDecs in x86 asm with BMI2 extensions. +func sequenceDecs_decode_bmi2(s *sequenceDecs, br *bitReader, ctx *decodeAsmContext) int + +type sequenceDecs_decode_function = func(s *sequenceDecs, br *bitReader, ctx *decodeAsmContext) int + +var sequenceDecs_decode sequenceDecs_decode_function + +func init() { + if cpuinfo.HasBMI2() { + sequenceDecs_decode = sequenceDecs_decode_bmi2 + } else { + sequenceDecs_decode = sequenceDecs_decode_amd64 + } +} + // decode sequences from the stream without the provided history. func (s *sequenceDecs) decode(seqs []seqVals) error { br := s.br @@ -53,7 +70,7 @@ func (s *sequenceDecs) decode(seqs []seqVals) error { s.seqSize = 0 - errCode := sequenceDecs_decode_amd64(s, br, &ctx) + errCode := sequenceDecs_decode(s, br, &ctx) if errCode != 0 { i := len(seqs) - ctx.iteration switch errCode {