Skip to content

Commit

Permalink
Use math and internal/cpu packages from Go 1.9.7
Browse files Browse the repository at this point in the history
  • Loading branch information
olljanat committed Jan 22, 2023
1 parent e766bf7 commit deebaa8
Show file tree
Hide file tree
Showing 306 changed files with 41,458 additions and 64 deletions.
2 changes: 1 addition & 1 deletion compressible.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package compress

import "math"
import "github.com/klauspost/compress/math"

// Estimate returns a normalized compressibility estimate of block b.
// Values close to zero are likely uncompressible.
Expand Down
2 changes: 2 additions & 0 deletions flate/_gen/gen_inflate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package main
import (
"os"
"strings"

"github.com/klauspost/compress/math/bits"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion flate/deflate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"encoding/binary"
"fmt"
"io"
"math"

"github.com/klauspost/compress/math"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion flate/fast_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ package flate
import (
"encoding/binary"
"fmt"
"math/bits"

"github.com/klauspost/compress/math/bits"
)

type fastEnc interface {
Expand Down
3 changes: 2 additions & 1 deletion flate/huffman_bit_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"encoding/binary"
"fmt"
"io"
"math"

"github.com/klauspost/compress/math"
)

const (
Expand Down
8 changes: 5 additions & 3 deletions flate/huffman_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package flate

import (
"math"
"math/bits"
"github.com/klauspost/compress/math"
"github.com/klauspost/compress/math/bits"
)

const (
Expand Down Expand Up @@ -78,7 +78,9 @@ func reverseBits(number uint16, bitLength byte) uint16 {
return bits.Reverse16(number << ((16 - bitLength) & 15))
}

func maxNode() literalNode { return literalNode{math.MaxUint16, math.MaxUint16} }
func maxNode() literalNode {
return literalNode{math.MaxUint16, math.MaxUint16}
}

func newHuffmanEncoder(size int) *huffmanEncoder {
// Make capacity to next power of two.
Expand Down
3 changes: 2 additions & 1 deletion flate/inflate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
"compress/flate"
"fmt"
"io"
"math/bits"
"sync"

"github.com/klauspost/compress/math/bits"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion flate/inflate_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion flate/level1.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package flate
import (
"encoding/binary"
"fmt"
"math/bits"

"github.com/klauspost/compress/math/bits"
)

// fastGen maintains the table for matches,
Expand Down
3 changes: 2 additions & 1 deletion flate/stateless.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package flate

import (
"io"
"math"
"sync"

"github.com/klauspost/compress/math"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion flate/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"encoding/binary"
"fmt"
"io"
"math"

"github.com/klauspost/compress/math"
)

const (
Expand Down
5 changes: 3 additions & 2 deletions flate/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
"compress/flate"
"fmt"
"io"
"math"
"math/rand"
"os"
"runtime"
"strconv"
"strings"
"testing"

"github.com/klauspost/compress/math"
"github.com/klauspost/compress/math/rand"
)

func TestWriterMemUsage(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion fse/fse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ package fse
import (
"errors"
"fmt"
"math/bits"

"github.com/klauspost/compress/math/bits"
)

const (
Expand Down
12 changes: 6 additions & 6 deletions gzhttp/testdata/benchmark.json
Original file line number Diff line number Diff line change
Expand Up @@ -1928,10 +1928,10 @@
{
"_id": "55d2fc861608b965b2283827",
"age": 38,
"name": "Hines Mathews",
"name": "Hines github.com/klauspost/compress/mathews",
"gender": "male",
"company": "INTRADISK",
"email": "hinesmathews@intradisk.com",
"email": "hinesgithub.com/klauspost/compress/mathews@intradisk.com",
"phone": "+1 (932) 420-2236"
},
{
Expand Down Expand Up @@ -2225,10 +2225,10 @@
{
"_id": "55d2fc8618c10cef39c48f97",
"age": 38,
"name": "Mathis Walsh",
"name": "github.com/klauspost/compress/mathis Walsh",
"gender": "male",
"company": "VURBO",
"email": "mathiswalsh@vurbo.com",
"email": "github.com/klauspost/compress/mathiswalsh@vurbo.com",
"phone": "+1 (837) 459-2909"
},
{
Expand Down Expand Up @@ -2693,10 +2693,10 @@
{
"_id": "55d2fc863a038b06c712b4b9",
"age": 30,
"name": "Bean Mathis",
"name": "Bean github.com/klauspost/compress/mathis",
"gender": "male",
"company": "QUILK",
"email": "beanmathis@quilk.com",
"email": "beangithub.com/klauspost/compress/mathis@quilk.com",
"phone": "+1 (974) 596-2868"
},
{
Expand Down
3 changes: 2 additions & 1 deletion gzip/gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"bufio"
"bytes"
"io"
"math/rand"
"os"
"strconv"
"strings"
"testing"
"time"

"github.com/klauspost/compress/math/rand"
)

// TestEmpty tests that an empty payload still forms a valid GZIP stream.
Expand Down
8 changes: 4 additions & 4 deletions gzip/testdata/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -1190,10 +1190,10 @@
"eyeColor": "green",
"name": {
"first": "Powers",
"last": "Mathews"
"last": "github.com/klauspost/compress/mathews"
},
"company": "ANDRYX",
"email": "powers.mathews@andryx.ca",
"email": "powers.github.com/klauspost/compress/mathews@andryx.ca",
"phone": "+1 (914) 559-2596",
"address": "545 Nolans Lane, Brenton, Arkansas, 7607",
"about": "In irure et tempor ad commodo culpa reprehenderit excepteur tempor ex. Exercitation eiusmod consequat anim incididunt veniam duis sunt velit sunt aliquip esse adipisicing do. Elit ea incididunt id amet mollit ea in ad ea cupidatat duis minim consectetur incididunt.\r\n",
Expand Down Expand Up @@ -1645,7 +1645,7 @@
},
{
"id": 2,
"name": "Mathews Fischer"
"name": "github.com/klauspost/compress/mathews Fischer"
}
],
"greeting": "Hello, Jill! You have 10 unread messages.",
Expand Down Expand Up @@ -5207,7 +5207,7 @@
"company": "NORALEX",
"email": "mckay.velasquez@noralex.tv",
"phone": "+1 (973) 599-3463",
"address": "152 Roebling Street, Mathews, Delaware, 7958",
"address": "152 Roebling Street, github.com/klauspost/compress/mathews, Delaware, 7958",
"about": "Nostrud esse dolor excepteur cillum aliqua. Ea nulla elit minim sint non culpa id. Et ullamco aute laborum incididunt sint quis. Tempor tempor aliqua in sunt. Minim elit dolor quis excepteur exercitation adipisicing. Pariatur incididunt tempor irure proident exercitation deserunt sint.\r\n",
"registered": "Monday, August 4, 2014 5:00 AM",
"latitude": 81.463276,
Expand Down
3 changes: 2 additions & 1 deletion huff0/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package huff0

import (
"fmt"
"math"
"runtime"
"sync"

"github.com/klauspost/compress/math"
)

// Compress1X will compress the input.
Expand Down
3 changes: 2 additions & 1 deletion huff0/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"bytes"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/klauspost/compress/math/rand"

"github.com/klauspost/compress/flate"
"github.com/klauspost/compress/zip"
)
Expand Down
5 changes: 3 additions & 2 deletions huff0/huff0.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ package huff0
import (
"errors"
"fmt"
"math"
"math/bits"
"sync"

"github.com/klauspost/compress/math"
"github.com/klauspost/compress/math/bits"

"github.com/klauspost/compress/fse"
)

Expand Down
32 changes: 32 additions & 0 deletions internal/cpu/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package cpu implements processor feature detection
// used by the Go standard libary.
package cpu

var X86 x86

// The booleans in x86 contain the correspondingly named cpuid feature bit.
// HasAVX and HasAVX2 are only set if the OS does support XMM and YMM registers
// in addition to the cpuid feature bit being set.
// The struct is padded to avoid false sharing.
type x86 struct {
_ [CacheLineSize]byte
HasAES bool
HasAVX bool
HasAVX2 bool
HasBMI1 bool
HasBMI2 bool
HasERMS bool
HasOSXSAVE bool
HasPCLMULQDQ bool
HasPOPCNT bool
HasSSE2 bool
HasSSE3 bool
HasSSSE3 bool
HasSSE41 bool
HasSSE42 bool
_ [CacheLineSize]byte
}
27 changes: 27 additions & 0 deletions internal/cpu/cpu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cpu_test

import (
"internal/cpu"
"runtime"
"testing"
)

func TestAMD64minimalFeatures(t *testing.T) {
if runtime.GOARCH == "amd64" {
if !cpu.X86.HasSSE2 {
t.Fatalf("HasSSE2 expected true, got false")
}
}
}

func TestAVX2hasAVX(t *testing.T) {
if runtime.GOARCH == "amd64" {
if cpu.X86.HasAVX2 && !cpu.X86.HasAVX {
t.Fatalf("HasAVX expected true, got false")
}
}
}
59 changes: 59 additions & 0 deletions internal/cpu/cpu_x86.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build 386 amd64 amd64p32

package cpu

const CacheLineSize = 64

// cpuid is implemented in cpu_x86.s.
func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32)

// xgetbv with ecx = 0 is implemented in cpu_x86.s.
func xgetbv() (eax, edx uint32)

func init() {
maxId, _, _, _ := cpuid(0, 0)

if maxId < 1 {
return
}

_, _, ecx1, edx1 := cpuid(1, 0)
X86.HasSSE2 = isSet(26, edx1)

X86.HasSSE3 = isSet(0, ecx1)
X86.HasPCLMULQDQ = isSet(1, ecx1)
X86.HasSSSE3 = isSet(9, ecx1)
X86.HasSSE41 = isSet(19, ecx1)
X86.HasSSE42 = isSet(20, ecx1)
X86.HasPOPCNT = isSet(23, ecx1)
X86.HasAES = isSet(25, ecx1)
X86.HasOSXSAVE = isSet(27, ecx1)

osSupportsAVX := false
// For XGETBV, OSXSAVE bit is required and sufficient.
if X86.HasOSXSAVE {
eax, _ := xgetbv()
// Check if XMM and YMM registers have OS support.
osSupportsAVX = isSet(1, eax) && isSet(2, eax)
}

X86.HasAVX = isSet(28, ecx1) && osSupportsAVX

if maxId < 7 {
return
}

_, ebx7, _, _ := cpuid(7, 0)
X86.HasBMI1 = isSet(3, ebx7)
X86.HasAVX2 = isSet(5, ebx7) && osSupportsAVX
X86.HasBMI2 = isSet(8, ebx7)
X86.HasERMS = isSet(9, ebx7)
}

func isSet(bitpos uint, value uint32) bool {
return value&(1<<bitpos) != 0
}

0 comments on commit deebaa8

Please sign in to comment.