aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/hash
diff options
context:
space:
mode:
authorIan Lance Taylor <ian@gcc.gnu.org>2012-10-23 04:31:11 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2012-10-23 04:31:11 +0000
commit4ccad563d2a3559f0557bfb177bcf45144219bdf (patch)
tree46bb86f514fbf6bad82da48e69a18fb09d878834 /libgo/go/hash
parent0b7463235f0e23c624d1911c9b15f531108cc5a6 (diff)
downloadgcc-4ccad563d2a3559f0557bfb177bcf45144219bdf.zip
gcc-4ccad563d2a3559f0557bfb177bcf45144219bdf.tar.gz
gcc-4ccad563d2a3559f0557bfb177bcf45144219bdf.tar.bz2
libgo: Update to current sources.
From-SVN: r192704
Diffstat (limited to 'libgo/go/hash')
-rw-r--r--libgo/go/hash/adler32/adler32.go68
-rw-r--r--libgo/go/hash/adler32/adler32_test.go90
-rw-r--r--libgo/go/hash/crc32/crc32.go6
-rw-r--r--libgo/go/hash/crc32/crc32_amd64.go2
-rw-r--r--libgo/go/hash/crc32/crc32_test.go18
-rw-r--r--libgo/go/hash/crc64/crc64.go10
-rw-r--r--libgo/go/hash/crc64/crc64_test.go17
-rw-r--r--libgo/go/hash/fnv/fnv.go32
-rw-r--r--libgo/go/hash/fnv/fnv_test.go34
9 files changed, 132 insertions, 145 deletions
diff --git a/libgo/go/hash/adler32/adler32.go b/libgo/go/hash/adler32/adler32.go
index 64fe68c..7c80796 100644
--- a/libgo/go/hash/adler32/adler32.go
+++ b/libgo/go/hash/adler32/adler32.go
@@ -3,7 +3,8 @@
// license that can be found in the LICENSE file.
// Package adler32 implements the Adler-32 checksum.
-// Defined in RFC 1950:
+//
+// It is defined in RFC 1950:
// Adler-32 is composed of two sums accumulated per byte: s1 is
// the sum of all bytes, s2 is the sum of all s1 values. Both sums
// are done modulo 65521. s1 is initialized to 1, s2 to zero. The
@@ -14,20 +15,22 @@ package adler32
import "hash"
const (
+ // mod is the largest prime that is less than 65536.
mod = 65521
+ // nmax is the largest n such that
+ // 255 * n * (n+1) / 2 + (n+1) * (mod-1) <= 2^32-1.
+ // It is mentioned in RFC 1950 (search for "5552").
+ nmax = 5552
)
// The size of an Adler-32 checksum in bytes.
const Size = 4
// digest represents the partial evaluation of a checksum.
-type digest struct {
- // invariant: (a < mod && b < mod) || a <= b
- // invariant: a + b + 255 <= 0xffffffff
- a, b uint32
-}
+// The low 16 bits are s1, the high 16 bits are s2.
+type digest uint32
-func (d *digest) Reset() { d.a, d.b = 1, 0 }
+func (d *digest) Reset() { *d = 1 }
// New returns a new hash.Hash32 computing the Adler-32 checksum.
func New() hash.Hash32 {
@@ -40,47 +43,36 @@ func (d *digest) Size() int { return Size }
func (d *digest) BlockSize() int { return 1 }
-// Add p to the running checksum a, b.
-func update(a, b uint32, p []byte) (aa, bb uint32) {
- for _, pi := range p {
- a += uint32(pi)
- b += a
- // invariant: a <= b
- if b > (0xffffffff-255)/2 {
- a %= mod
- b %= mod
- // invariant: a < mod && b < mod
- } else {
- // invariant: a + b + 255 <= 2 * b + 255 <= 0xffffffff
+// Add p to the running checksum d.
+func update(d digest, p []byte) digest {
+ s1, s2 := uint32(d&0xffff), uint32(d>>16)
+ for len(p) > 0 {
+ var q []byte
+ if len(p) > nmax {
+ p, q = p[:nmax], p[nmax:]
}
+ for _, x := range p {
+ s1 += uint32(x)
+ s2 += s1
+ }
+ s1 %= mod
+ s2 %= mod
+ p = q
}
- return a, b
-}
-
-// Return the 32-bit checksum corresponding to a, b.
-func finish(a, b uint32) uint32 {
- if b >= mod {
- a %= mod
- b %= mod
- }
- return b<<16 | a
+ return digest(s2<<16 | s1)
}
func (d *digest) Write(p []byte) (nn int, err error) {
- d.a, d.b = update(d.a, d.b, p)
+ *d = update(*d, p)
return len(p), nil
}
-func (d *digest) Sum32() uint32 { return finish(d.a, d.b) }
+func (d *digest) Sum32() uint32 { return uint32(*d) }
func (d *digest) Sum(in []byte) []byte {
- s := d.Sum32()
- in = append(in, byte(s>>24))
- in = append(in, byte(s>>16))
- in = append(in, byte(s>>8))
- in = append(in, byte(s))
- return in
+ s := uint32(*d)
+ return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
}
// Checksum returns the Adler-32 checksum of data.
-func Checksum(data []byte) uint32 { return finish(update(1, 0, data)) }
+func Checksum(data []byte) uint32 { return uint32(update(1, data)) }
diff --git a/libgo/go/hash/adler32/adler32_test.go b/libgo/go/hash/adler32/adler32_test.go
index 01f931c..0e9c938 100644
--- a/libgo/go/hash/adler32/adler32_test.go
+++ b/libgo/go/hash/adler32/adler32_test.go
@@ -5,26 +5,23 @@
package adler32
import (
- "bytes"
- "io"
+ "strings"
"testing"
)
-type _Adler32Test struct {
+var golden = []struct {
out uint32
in string
-}
-
-var golden = []_Adler32Test{
- {0x1, ""},
- {0x620062, "a"},
- {0x12600c4, "ab"},
- {0x24d0127, "abc"},
- {0x3d8018b, "abcd"},
- {0x5c801f0, "abcde"},
- {0x81e0256, "abcdef"},
- {0xadb02bd, "abcdefg"},
- {0xe000325, "abcdefgh"},
+}{
+ {0x00000001, ""},
+ {0x00620062, "a"},
+ {0x012600c4, "ab"},
+ {0x024d0127, "abc"},
+ {0x03d8018b, "abcd"},
+ {0x05c801f0, "abcde"},
+ {0x081e0256, "abcdef"},
+ {0x0adb02bd, "abcdefg"},
+ {0x0e000325, "abcdefgh"},
{0x118e038e, "abcdefghi"},
{0x158603f8, "abcdefghij"},
{0x3f090f02, "Discard medicine more than two years old."},
@@ -48,30 +45,61 @@ var golden = []_Adler32Test{
{0x91dd304f, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule"},
{0x2e5d1316, "How can you write a big system without C++? -Paul Glick"},
{0xd0201df6, "'Invariant assertions' is the most elegant programming technique! -Tom Szymanski"},
+ {0x211297c8, strings.Repeat("\xff", 5548) + "8"},
+ {0xbaa198c8, strings.Repeat("\xff", 5549) + "9"},
+ {0x553499be, strings.Repeat("\xff", 5550) + "0"},
+ {0xf0c19abe, strings.Repeat("\xff", 5551) + "1"},
+ {0x8d5c9bbe, strings.Repeat("\xff", 5552) + "2"},
+ {0x2af69cbe, strings.Repeat("\xff", 5553) + "3"},
+ {0xc9809dbe, strings.Repeat("\xff", 5554) + "4"},
+ {0x69189ebe, strings.Repeat("\xff", 5555) + "5"},
+ {0x86af0001, strings.Repeat("\x00", 1e5)},
+ {0x79660b4d, strings.Repeat("a", 1e5)},
+ {0x110588ee, strings.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1e4)},
+}
+
+// checksum is a slow but simple implementation of the Adler-32 checksum.
+// It is a straight port of the sample code in RFC 1950 section 9.
+func checksum(p []byte) uint32 {
+ s1, s2 := uint32(1), uint32(0)
+ for _, x := range p {
+ s1 = (s1 + uint32(x)) % mod
+ s2 = (s2 + s1) % mod
+ }
+ return s2<<16 | s1
}
func TestGolden(t *testing.T) {
- for i := 0; i < len(golden); i++ {
- g := golden[i]
- c := New()
- io.WriteString(c, g.in)
- s := c.Sum32()
- if s != g.out {
- t.Errorf("adler32(%s) = 0x%x want 0x%x", g.in, s, g.out)
- t.FailNow()
+ for _, g := range golden {
+ in := g.in
+ if len(in) > 220 {
+ in = in[:100] + "..." + in[len(in)-100:]
+ }
+ p := []byte(g.in)
+ if got := checksum(p); got != g.out {
+ t.Errorf("simple implementation: checksum(%q) = 0x%x want 0x%x", in, got, g.out)
+ continue
+ }
+ if got := Checksum(p); got != g.out {
+ t.Errorf("optimized implementation: Checksum(%q) = 0x%x want 0x%x", in, got, g.out)
+ continue
}
}
}
-func BenchmarkGolden(b *testing.B) {
- b.StopTimer()
- c := New()
- var buf bytes.Buffer
- for _, g := range golden {
- buf.Write([]byte(g.in))
+func BenchmarkAdler32KB(b *testing.B) {
+ b.SetBytes(1024)
+ data := make([]byte, 1024)
+ for i := range data {
+ data[i] = byte(i)
}
- b.StartTimer()
+ h := New()
+ in := make([]byte, 0, h.Size())
+
+ b.ResetTimer()
for i := 0; i < b.N; i++ {
- c.Write(buf.Bytes())
+ h.Reset()
+ h.Write(data)
+ h.Sum(in)
}
}
diff --git a/libgo/go/hash/crc32/crc32.go b/libgo/go/hash/crc32/crc32.go
index 236d778..a2a21a0 100644
--- a/libgo/go/hash/crc32/crc32.go
+++ b/libgo/go/hash/crc32/crc32.go
@@ -123,11 +123,7 @@ func (d *digest) Sum32() uint32 { return d.crc }
func (d *digest) Sum(in []byte) []byte {
s := d.Sum32()
- in = append(in, byte(s>>24))
- in = append(in, byte(s>>16))
- in = append(in, byte(s>>8))
- in = append(in, byte(s))
- return in
+ return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
}
// Checksum returns the CRC-32 checksum of data
diff --git a/libgo/go/hash/crc32/crc32_amd64.go b/libgo/go/hash/crc32/crc32_amd64.go
index 83349bc..b5bc6d3 100644
--- a/libgo/go/hash/crc32/crc32_amd64.go
+++ b/libgo/go/hash/crc32/crc32_amd64.go
@@ -13,7 +13,7 @@ func haveSSE42() bool
// castagnoliSSE42 is defined in crc_amd64.s and uses the SSE4.2 CRC32
// instruction.
-func castagnoliSSE42(uint32, []byte) uint32
+func castagnoliSSE42(crc uint32, p []byte) uint32
var sse42 = haveSSE42()
diff --git a/libgo/go/hash/crc32/crc32_test.go b/libgo/go/hash/crc32/crc32_test.go
index 7e82dd7..75dc26e 100644
--- a/libgo/go/hash/crc32/crc32_test.go
+++ b/libgo/go/hash/crc32/crc32_test.go
@@ -82,16 +82,18 @@ func TestGolden(t *testing.T) {
}
func BenchmarkCrc32KB(b *testing.B) {
- b.StopTimer()
- data := make([]uint8, 1024)
- for i := 0; i < 1024; i++ {
- data[i] = uint8(i)
+ b.SetBytes(1024)
+ data := make([]byte, 1024)
+ for i := range data {
+ data[i] = byte(i)
}
- c := NewIEEE()
- b.StartTimer()
- b.SetBytes(int64(len(data)))
+ h := NewIEEE()
+ in := make([]byte, 0, h.Size())
+ b.ResetTimer()
for i := 0; i < b.N; i++ {
- c.Write(data)
+ h.Reset()
+ h.Write(data)
+ h.Sum(in)
}
}
diff --git a/libgo/go/hash/crc64/crc64.go b/libgo/go/hash/crc64/crc64.go
index 5b64390..6925867 100644
--- a/libgo/go/hash/crc64/crc64.go
+++ b/libgo/go/hash/crc64/crc64.go
@@ -79,15 +79,7 @@ func (d *digest) Sum64() uint64 { return d.crc }
func (d *digest) Sum(in []byte) []byte {
s := d.Sum64()
- in = append(in, byte(s>>56))
- in = append(in, byte(s>>48))
- in = append(in, byte(s>>40))
- in = append(in, byte(s>>32))
- in = append(in, byte(s>>24))
- in = append(in, byte(s>>16))
- in = append(in, byte(s>>8))
- in = append(in, byte(s))
- return in
+ return append(in, byte(s>>56), byte(s>>48), byte(s>>40), byte(s>>32), byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
}
// Checksum returns the CRC-64 checksum of data
diff --git a/libgo/go/hash/crc64/crc64_test.go b/libgo/go/hash/crc64/crc64_test.go
index e932524..81a87b5 100644
--- a/libgo/go/hash/crc64/crc64_test.go
+++ b/libgo/go/hash/crc64/crc64_test.go
@@ -64,15 +64,18 @@ func TestGolden(t *testing.T) {
}
func BenchmarkCrc64KB(b *testing.B) {
- b.StopTimer()
- data := make([]uint8, 1024)
- for i := 0; i < 1024; i++ {
- data[i] = uint8(i)
+ b.SetBytes(1024)
+ data := make([]byte, 1024)
+ for i := range data {
+ data[i] = byte(i)
}
- c := New(tab)
- b.StartTimer()
+ h := New(tab)
+ in := make([]byte, 0, h.Size())
+ b.ResetTimer()
for i := 0; i < b.N; i++ {
- c.Write(data)
+ h.Reset()
+ h.Write(data)
+ h.Sum(in)
}
}
diff --git a/libgo/go/hash/fnv/fnv.go b/libgo/go/hash/fnv/fnv.go
index ea50198..b5ecd4a 100644
--- a/libgo/go/hash/fnv/fnv.go
+++ b/libgo/go/hash/fnv/fnv.go
@@ -111,44 +111,20 @@ func (s *sum64a) BlockSize() int { return 1 }
func (s *sum32) Sum(in []byte) []byte {
v := uint32(*s)
- in = append(in, byte(v>>24))
- in = append(in, byte(v>>16))
- in = append(in, byte(v>>8))
- in = append(in, byte(v))
- return in
+ return append(in, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
func (s *sum32a) Sum(in []byte) []byte {
v := uint32(*s)
- in = append(in, byte(v>>24))
- in = append(in, byte(v>>16))
- in = append(in, byte(v>>8))
- in = append(in, byte(v))
- return in
+ return append(in, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
func (s *sum64) Sum(in []byte) []byte {
v := uint64(*s)
- in = append(in, byte(v>>56))
- in = append(in, byte(v>>48))
- in = append(in, byte(v>>40))
- in = append(in, byte(v>>32))
- in = append(in, byte(v>>24))
- in = append(in, byte(v>>16))
- in = append(in, byte(v>>8))
- in = append(in, byte(v))
- return in
+ return append(in, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
func (s *sum64a) Sum(in []byte) []byte {
v := uint64(*s)
- in = append(in, byte(v>>56))
- in = append(in, byte(v>>48))
- in = append(in, byte(v>>40))
- in = append(in, byte(v>>32))
- in = append(in, byte(v>>24))
- in = append(in, byte(v>>16))
- in = append(in, byte(v>>8))
- in = append(in, byte(v))
- return in
+ return append(in, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
diff --git a/libgo/go/hash/fnv/fnv_test.go b/libgo/go/hash/fnv/fnv_test.go
index 17454de..89d39b3 100644
--- a/libgo/go/hash/fnv/fnv_test.go
+++ b/libgo/go/hash/fnv/fnv_test.go
@@ -11,8 +11,6 @@ import (
"testing"
)
-const testDataSize = 40
-
type golden struct {
sum []byte
text string
@@ -134,34 +132,34 @@ func testIntegrity(t *testing.T, h hash.Hash) {
}
}
-func Benchmark32(b *testing.B) {
- benchmark(b, New32())
+func BenchmarkFnv32KB(b *testing.B) {
+ benchmarkKB(b, New32())
}
-func Benchmark32a(b *testing.B) {
- benchmark(b, New32a())
+func BenchmarkFnv32aKB(b *testing.B) {
+ benchmarkKB(b, New32a())
}
-func Benchmark64(b *testing.B) {
- benchmark(b, New64())
+func BenchmarkFnv64KB(b *testing.B) {
+ benchmarkKB(b, New64())
}
-func Benchmark64a(b *testing.B) {
- benchmark(b, New64a())
+func BenchmarkFnv64aKB(b *testing.B) {
+ benchmarkKB(b, New64a())
}
-func benchmark(b *testing.B, h hash.Hash) {
- b.ResetTimer()
- b.SetBytes(testDataSize)
- data := make([]byte, testDataSize)
+func benchmarkKB(b *testing.B, h hash.Hash) {
+ b.SetBytes(1024)
+ data := make([]byte, 1024)
for i := range data {
- data[i] = byte(i + 'a')
+ data[i] = byte(i)
}
+ in := make([]byte, 0, h.Size())
- b.StartTimer()
- for todo := b.N; todo != 0; todo-- {
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
h.Reset()
h.Write(data)
- h.Sum(nil)
+ h.Sum(in)
}
}