pax_global_header00006660000000000000000000000064134273251110014511gustar00rootroot0000000000000052 comment=456a93f5dffea9f90d34d67b03b144f12a8125cc xxhash-1.2.4/000077500000000000000000000000001342732511100130205ustar00rootroot00000000000000xxhash-1.2.4/.gitignore000066400000000000000000000000341342732511100150050ustar00rootroot00000000000000*.txt *.pprof cmap2/ cache/ xxhash-1.2.4/.travis.yml000066400000000000000000000001741342732511100151330ustar00rootroot00000000000000language: go sudo: false go: - 1.8 - 1.9 - "1.10" - master script: - go test -tags safe ./... - go test ./... xxhash-1.2.4/.vscode/000077500000000000000000000000001342732511100143615ustar00rootroot00000000000000xxhash-1.2.4/.vscode/launch.json000066400000000000000000000006221342732511100165260ustar00rootroot00000000000000{ "version": "0.2.0", "configurations": [ { "name": "Test", "type": "go", "request": "launch", "mode": "test", "program": "${workspaceRoot}", "env": {}, "args": ["-test.v", "./..."] }, { "name": "Bench", "type": "go", "request": "launch", "mode": "test", "program": "${workspaceRoot}", "env": {}, "args": ["-test.v", "-test.bench=XX", "./..."] } ] }xxhash-1.2.4/LICENSE000066400000000000000000000250061342732511100140300ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. xxhash-1.2.4/README.md000066400000000000000000000054521342732511100143050ustar00rootroot00000000000000# xxhash [![GoDoc](https://godoc.org/github.com/OneOfOne/xxhash?status.svg)](https://godoc.org/github.com/OneOfOne/xxhash) [![Build Status](https://travis-ci.org/OneOfOne/xxhash.svg?branch=master)](https://travis-ci.org/OneOfOne/xxhash) [![Coverage](https://gocover.io/_badge/github.com/OneOfOne/xxhash)](https://gocover.io/github.com/OneOfOne/xxhash) This is a native Go implementation of the excellent [xxhash](https://github.com/Cyan4973/xxHash)* algorithm, an extremely fast non-cryptographic Hash algorithm, working at speeds close to RAM limits. * The C implementation is ([Copyright](https://github.com/Cyan4973/xxHash/blob/master/LICENSE) (c) 2012-2014, Yann Collet) ## Install go get github.com/OneOfOne/xxhash ## Features * On Go 1.7+ the pure go version is faster than CGO for all inputs. * Supports ChecksumString{32,64} xxhash{32,64}.WriteString, which uses no copies when it can, falls back to copy on appengine. * The native version falls back to a less optimized version on appengine due to the lack of unsafe. * Almost as fast as the mostly pure assembly version written by the brilliant [cespare](https://github.com/cespare/xxhash), while also supporting seeds. * To manually toggle the appengine version build with `-tags safe`. ## Benchmark ### Core i7-4790 @ 3.60GHz, Linux 4.12.6-1-ARCH (64bit), Go tip (+ff90f4af66 2017-08-19) ```bash ➤ go test -bench '64' -count 5 -tags cespare | benchstat /dev/stdin name time/op # https://github.com/cespare/xxhash XXSum64Cespare/Func-8 160ns ± 2% XXSum64Cespare/Struct-8 173ns ± 1% XXSum64ShortCespare/Func-8 6.78ns ± 1% XXSum64ShortCespare/Struct-8 19.6ns ± 2% # this package (default mode, using unsafe) XXSum64/Func-8 170ns ± 1% XXSum64/Struct-8 182ns ± 1% XXSum64Short/Func-8 13.5ns ± 3% XXSum64Short/Struct-8 20.4ns ± 0% # this package (appengine, *not* using unsafe) XXSum64/Func-8 241ns ± 5% XXSum64/Struct-8 243ns ± 6% XXSum64Short/Func-8 15.2ns ± 2% XXSum64Short/Struct-8 23.7ns ± 5% CRC64ISO-8 1.23µs ± 1% CRC64ISOString-8 2.71µs ± 4% CRC64ISOShort-8 22.2ns ± 3% Fnv64-8 2.34µs ± 1% Fnv64Short-8 74.7ns ± 8% # ``` ## Usage ```go h := xxhash.New64() // r, err := os.Open("......") // defer f.Close() r := strings.NewReader(F) io.Copy(h, r) fmt.Println("xxhash.Backend:", xxhash.Backend) fmt.Println("File checksum:", h.Sum64()) ``` [playground](http://play.golang.org/p/rhRN3RdQyd) ## TODO * Rewrite the 32bit version to be more optimized. * General cleanup as the Go inliner gets smarter. ## License This project is released under the Apache v2. licence. See [LICENCE](LICENCE) for more details. xxhash-1.2.4/bugs_test.go000066400000000000000000000051601342732511100153500ustar00rootroot00000000000000// +build ignore package xxhash_test import ( "bytes" "math/rand" "reflect" "testing" "time" "testing/quick" N "github.com/OneOfOne/xxhash" ) func TestReset64(t *testing.T) { h := N.New64() // p1 := "http" p2 := "://" p3 := "www.marmiton.org" p4 := "/recettes/recherche.aspx" p5 := "?st=2&aqt=gateau&" url := p1 + p2 + p3 + p4 + p5 // compute hash by parts h.Write([]byte(p1)) h.Write([]byte(p2)) h.Write([]byte(p3)) h.Write([]byte(p4)) h.Write([]byte(p5)) s1 := h.Sum64() h.Reset() h.Write([]byte(url)) s2 := h.Sum64() // should be the same, right ? if s1 != s2 { t.Errorf("s1 != s2 %x %x", s1, s2) } } func TestReset32(t *testing.T) { h := N.New32() // p1 := "http" p2 := "://" p3 := "www.marmiton.org" p4 := "/recettes/recherche.aspx" p5 := "?st=2&aqt=gateau&" url := p1 + p2 + p3 + p4 + p5 // compute hash by parts h.Write([]byte(p1)) h.Write([]byte(p2)) h.Write([]byte(p3)) h.Write([]byte(p4)) h.Write([]byte(p5)) s1 := h.Sum32() h.Reset() h.Write([]byte(url)) s2 := h.Sum32() // should be the same, right ? if s1 != s2 { t.Errorf("s1 != s2 %x %x", s1, s2) } } // issue 8 func TestDataLen(t *testing.T) { for i := 4; i <= 8096; i += 4 { testEquality(t, bytes.Repeat([]byte("www."), i/4)) } } func testEquality(t *testing.T, v []byte) { ch64, ch32 := cxx.Checksum64(v), cxx.Checksum32(v) if h := N.Checksum64(v); ch64 != h { t.Fatalf("Checksum64 doesn't match, len = %d, expected 0x%X, got 0x%X", len(v), ch64, h) } if h := N.Checksum32(v); ch32 != h { t.Fatalf("Checksum32 doesn't match, len = %d, expected 0x%X, got 0x%X", len(v), ch32, h) } h64 := N.New64() h64.Write(v) if h := h64.Sum64(); ch64 != h { t.Fatalf("Sum64() doesn't match, len = %d, expected 0x%X, got 0x%X", len(v), ch64, h) } h32 := N.New32() h32.Write(v) if h := h32.Sum32(); ch32 != h { t.Fatalf("Sum32() doesn't match, len = %d, expected 0x%X, got 0x%X", len(v), ch32, h) } } func TestHulkSmash(t *testing.T) { const C = 10000 rnd, typ := rand.New(rand.NewSource(time.Now().UnixNano())), reflect.TypeOf([]byte(nil)) for i := 0; i < C; i++ { v, ok := quick.Value(typ, rnd) if !ok { t.Fatal("!ok") } vb := v.Bytes() seed := uint64(rnd.Int63()) x64 := N.NewS64(seed) x64.Write(vb) if s1, s2 := x64.Sum64(), N.Checksum64S(vb, seed); s1 != s2 { t.Fatalf("len(v) = %d: %d != %d, should be %d", len(vb), s1, s2, cxx.Checksum64S(vb, seed)) } x32 := N.NewS32(uint32(seed)) x32.Write(vb) if s1, s2 := x32.Sum32(), N.Checksum32S(vb, uint32(seed)); s1 != s2 { t.Fatalf("len(v) = %d: %d != %d, should be %d", len(vb), s1, s2, cxx.Checksum32S(vb, uint32(seed))) } } } xxhash-1.2.4/cmd/000077500000000000000000000000001342732511100135635ustar00rootroot00000000000000xxhash-1.2.4/cmd/xxhsum/000077500000000000000000000000001342732511100151175ustar00rootroot00000000000000xxhash-1.2.4/cmd/xxhsum/main.go000066400000000000000000000063541342732511100164020ustar00rootroot00000000000000package main import ( "bufio" "flag" "fmt" "io" "os" "runtime" "strconv" "strings" "sync" "github.com/OneOfOne/xxhash" ) var ( mux sync.Mutex errored bool use32 = flag.Bool("32", false, "use 32bit hash instead of 64bit") checkArg = flag.Bool("c", false, "read and check the sums of input files") seedArg = flag.Uint64("s", 0, "use `seed` to seed the hasher") ) func init() { flag.Parse() flag.Usage = func() { errorf("Usage of %s: [-32] [-c] [-s seed] files...\t%s *.go > sums.xx\t%s -c sums.xx", os.Args[0], os.Args[0], os.Args[0]) flag.PrintDefaults() os.Exit(1) } } func main() { args := flag.Args() st, _ := os.Stdin.Stat() if st.Mode()&os.ModeCharDevice == 0 { args = append(args, "-") } if len(args) == 0 { flag.Usage() } sema := newSema(runtime.NumCPU()) var wg sync.WaitGroup wg.Add(len(args)) if !*checkArg { printf("# seed %d", *seedArg) if *use32 { printf("# 32bit") } else { printf("# 64bit") } } for _, fn := range args { if *checkArg { check(newSema(runtime.NumCPU()), fn) } else { sema.Run(func() { printHash(fn) }) } } sema.WaitAndClose() if errored { os.Exit(1) } } func check(sema *sema, fn string) { defer sema.WaitAndClose() var err error var f *os.File if fn == "-" { f = os.Stdin } else { if f, err = os.Open(fn); err != nil { errorf("error opening %s: %v", fn, err) return } defer f.Close() } buf := bufio.NewScanner(f) for buf.Scan() { ln := strings.TrimSpace(buf.Text()) if len(ln) == 0 { continue } if strings.HasPrefix(ln, "# seed ") { *seedArg, _ = strconv.ParseUint(strings.TrimSpace(ln[7:]), 10, 64) continue } if strings.HasPrefix(ln, "# 32bit") { *use32 = true continue } if strings.HasPrefix(ln, "# 64bit") { *use32 = false continue } if ln[0] == '#' { continue } parts := strings.SplitN(ln, "\t", 2) if len(parts) != 2 || len(parts[0]) < 1 { continue } oh, err := strconv.ParseUint(strings.TrimSpace(parts[0]), 10, 64) if err != nil { errorf("error: %v", err) continue } sema.Run(func() { fn := strings.TrimSpace(parts[1]) nh, err := hashFile(fn) if err != nil { errorf("error hashing %s: %v", fn, err) } if oh != nh { errorf("hash mismatch %q 0x%X 0x%X", fn, oh, nh) } }) } } func printHash(fn string) { h, err := hashFile(fn) if err != nil { errorf("error hashing %s: %v", fn, err) return } if h == 0 { return } if *use32 { printf("%-10d\t%s", h, fn) } else { printf("%-20d\t%s", h, fn) } } func hashFile(fn string) (h uint64, err error) { var f *os.File if fn == "-" { f = os.Stdin } else { if f, err = os.Open(fn); err != nil { return } defer f.Close() if st, _ := f.Stat(); st.IsDir() || st.Size() == 0 { return } } if *use32 { xx := xxhash.NewS32(uint32(*seedArg)) if _, err = io.Copy(xx, f); err != nil { return } return uint64(xx.Sum32()), nil } xx := xxhash.NewS64(*seedArg) if _, err = io.Copy(xx, f); err != nil { return } return xx.Sum64(), nil } func printf(f string, args ...interface{}) { mux.Lock() fmt.Fprintf(os.Stdout, f+"\n", args...) mux.Unlock() } func errorf(f string, args ...interface{}) { mux.Lock() fmt.Fprintf(os.Stderr, f+"\n", args...) errored = true mux.Unlock() } xxhash-1.2.4/cmd/xxhsum/sema.go000066400000000000000000000006451342732511100164000ustar00rootroot00000000000000package main import "sync" type sema struct { wg sync.WaitGroup ch chan func() } func newSema(n int) *sema { s := &sema{ ch: make(chan func(), n), } for ; n > 0; n-- { go s.handler() } return s } func (s *sema) handler() { for fn := range s.ch { fn() s.wg.Done() } } func (s *sema) Run(fn func()) { s.wg.Add(1) s.ch <- fn } func (s *sema) WaitAndClose() { s.wg.Wait() close(s.ch) s.ch = nil } xxhash-1.2.4/go.mod000066400000000000000000000000421342732511100141220ustar00rootroot00000000000000module github.com/OneOfOne/xxhash xxhash-1.2.4/xxhash.go000066400000000000000000000124141342732511100146540ustar00rootroot00000000000000package xxhash const ( prime32x1 uint32 = 2654435761 prime32x2 uint32 = 2246822519 prime32x3 uint32 = 3266489917 prime32x4 uint32 = 668265263 prime32x5 uint32 = 374761393 prime64x1 uint64 = 11400714785074694791 prime64x2 uint64 = 14029467366897019727 prime64x3 uint64 = 1609587929392839161 prime64x4 uint64 = 9650029242287828579 prime64x5 uint64 = 2870177450012600261 maxInt32 int32 = (1<<31 - 1) // precomputed zero Vs for seed 0 zero64x1 = 0x60ea27eeadc0b5d6 zero64x2 = 0xc2b2ae3d27d4eb4f zero64x3 = 0x0 zero64x4 = 0x61c8864e7a143579 ) // Checksum32 returns the checksum of the input data with the seed set to 0. func Checksum32(in []byte) uint32 { return Checksum32S(in, 0) } // ChecksumString32 returns the checksum of the input data, without creating a copy, with the seed set to 0. func ChecksumString32(s string) uint32 { return ChecksumString32S(s, 0) } type XXHash32 struct { mem [16]byte ln, memIdx int32 v1, v2, v3, v4 uint32 seed uint32 } // Size returns the number of bytes Sum will return. func (xx *XXHash32) Size() int { return 4 } // BlockSize returns the hash's underlying block size. // The Write method must be able to accept any amount // of data, but it may operate more efficiently if all writes // are a multiple of the block size. func (xx *XXHash32) BlockSize() int { return 16 } // NewS32 creates a new hash.Hash32 computing the 32bit xxHash checksum starting with the specific seed. func NewS32(seed uint32) (xx *XXHash32) { xx = &XXHash32{ seed: seed, } xx.Reset() return } // New32 creates a new hash.Hash32 computing the 32bit xxHash checksum starting with the seed set to 0. func New32() *XXHash32 { return NewS32(0) } func (xx *XXHash32) Reset() { xx.v1 = xx.seed + prime32x1 + prime32x2 xx.v2 = xx.seed + prime32x2 xx.v3 = xx.seed xx.v4 = xx.seed - prime32x1 xx.ln, xx.memIdx = 0, 0 } // Sum appends the current hash to b and returns the resulting slice. // It does not change the underlying hash state. func (xx *XXHash32) Sum(in []byte) []byte { s := xx.Sum32() return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s)) } // Checksum64 an alias for Checksum64S(in, 0) func Checksum64(in []byte) uint64 { return Checksum64S(in, 0) } // ChecksumString64 returns the checksum of the input data, without creating a copy, with the seed set to 0. func ChecksumString64(s string) uint64 { return ChecksumString64S(s, 0) } type XXHash64 struct { v1, v2, v3, v4 uint64 seed uint64 ln uint64 mem [32]byte memIdx int8 } // Size returns the number of bytes Sum will return. func (xx *XXHash64) Size() int { return 8 } // BlockSize returns the hash's underlying block size. // The Write method must be able to accept any amount // of data, but it may operate more efficiently if all writes // are a multiple of the block size. func (xx *XXHash64) BlockSize() int { return 32 } // NewS64 creates a new hash.Hash64 computing the 64bit xxHash checksum starting with the specific seed. func NewS64(seed uint64) (xx *XXHash64) { xx = &XXHash64{ seed: seed, } xx.Reset() return } // New64 creates a new hash.Hash64 computing the 64bit xxHash checksum starting with the seed set to 0x0. func New64() *XXHash64 { return NewS64(0) } func (xx *XXHash64) Reset() { xx.ln, xx.memIdx = 0, 0 xx.v1, xx.v2, xx.v3, xx.v4 = resetVs64(xx.seed) } // Sum appends the current hash to b and returns the resulting slice. // It does not change the underlying hash state. func (xx *XXHash64) Sum(in []byte) []byte { s := xx.Sum64() 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)) } // force the compiler to use ROTL instructions func rotl32_1(x uint32) uint32 { return (x << 1) | (x >> (32 - 1)) } func rotl32_7(x uint32) uint32 { return (x << 7) | (x >> (32 - 7)) } func rotl32_11(x uint32) uint32 { return (x << 11) | (x >> (32 - 11)) } func rotl32_12(x uint32) uint32 { return (x << 12) | (x >> (32 - 12)) } func rotl32_13(x uint32) uint32 { return (x << 13) | (x >> (32 - 13)) } func rotl32_17(x uint32) uint32 { return (x << 17) | (x >> (32 - 17)) } func rotl32_18(x uint32) uint32 { return (x << 18) | (x >> (32 - 18)) } func rotl64_1(x uint64) uint64 { return (x << 1) | (x >> (64 - 1)) } func rotl64_7(x uint64) uint64 { return (x << 7) | (x >> (64 - 7)) } func rotl64_11(x uint64) uint64 { return (x << 11) | (x >> (64 - 11)) } func rotl64_12(x uint64) uint64 { return (x << 12) | (x >> (64 - 12)) } func rotl64_18(x uint64) uint64 { return (x << 18) | (x >> (64 - 18)) } func rotl64_23(x uint64) uint64 { return (x << 23) | (x >> (64 - 23)) } func rotl64_27(x uint64) uint64 { return (x << 27) | (x >> (64 - 27)) } func rotl64_31(x uint64) uint64 { return (x << 31) | (x >> (64 - 31)) } func mix64(h uint64) uint64 { h ^= h >> 33 h *= prime64x2 h ^= h >> 29 h *= prime64x3 h ^= h >> 32 return h } func resetVs64(seed uint64) (v1, v2, v3, v4 uint64) { if seed == 0 { return zero64x1, zero64x2, zero64x3, zero64x4 } return (seed + prime64x1 + prime64x2), (seed + prime64x2), (seed), (seed - prime64x1) } // borrowed from cespare func round64(h, v uint64) uint64 { h += v * prime64x2 h = rotl64_31(h) h *= prime64x1 return h } func mergeRound64(h, v uint64) uint64 { v = round64(0, v) h ^= v h = h*prime64x1 + prime64x4 return h } xxhash-1.2.4/xxhash_cespare_test.go000066400000000000000000000013341342732511100174140ustar00rootroot00000000000000// +build cespare package xxhash_test import ( "testing" "github.com/cespare/xxhash" ) func BenchmarkXXSum64Cespare(b *testing.B) { var bv uint64 b.Run("Func", func(b *testing.B) { for i := 0; i < b.N; i++ { bv += xxhash.Sum64(in) } }) b.Run("Struct", func(b *testing.B) { h := xxhash.New() for i := 0; i < b.N; i++ { h.Write(in) bv += h.Sum64() h.Reset() } }) } func BenchmarkXXSum64ShortCespare(b *testing.B) { var bv uint64 k := []byte("Test-key-100") b.Run("Func", func(b *testing.B) { for i := 0; i < b.N; i++ { bv += xxhash.Sum64(k) } }) b.Run("Struct", func(b *testing.B) { h := xxhash.New() for i := 0; i < b.N; i++ { h.Write(k) bv += h.Sum64() h.Reset() } }) } xxhash-1.2.4/xxhash_go17.go000066400000000000000000000070531342732511100155140ustar00rootroot00000000000000package xxhash func u32(in []byte) uint32 { return uint32(in[0]) | uint32(in[1])<<8 | uint32(in[2])<<16 | uint32(in[3])<<24 } func u64(in []byte) uint64 { return uint64(in[0]) | uint64(in[1])<<8 | uint64(in[2])<<16 | uint64(in[3])<<24 | uint64(in[4])<<32 | uint64(in[5])<<40 | uint64(in[6])<<48 | uint64(in[7])<<56 } // Checksum32S returns the checksum of the input bytes with the specific seed. func Checksum32S(in []byte, seed uint32) (h uint32) { var i int if len(in) > 15 { var ( v1 = seed + prime32x1 + prime32x2 v2 = seed + prime32x2 v3 = seed + 0 v4 = seed - prime32x1 ) for ; i < len(in)-15; i += 16 { in := in[i : i+16 : len(in)] v1 += u32(in[0:4:len(in)]) * prime32x2 v1 = rotl32_13(v1) * prime32x1 v2 += u32(in[4:8:len(in)]) * prime32x2 v2 = rotl32_13(v2) * prime32x1 v3 += u32(in[8:12:len(in)]) * prime32x2 v3 = rotl32_13(v3) * prime32x1 v4 += u32(in[12:16:len(in)]) * prime32x2 v4 = rotl32_13(v4) * prime32x1 } h = rotl32_1(v1) + rotl32_7(v2) + rotl32_12(v3) + rotl32_18(v4) } else { h = seed + prime32x5 } h += uint32(len(in)) for ; i <= len(in)-4; i += 4 { in := in[i : i+4 : len(in)] h += u32(in[0:4:len(in)]) * prime32x3 h = rotl32_17(h) * prime32x4 } for ; i < len(in); i++ { h += uint32(in[i]) * prime32x5 h = rotl32_11(h) * prime32x1 } h ^= h >> 15 h *= prime32x2 h ^= h >> 13 h *= prime32x3 h ^= h >> 16 return } func (xx *XXHash32) Write(in []byte) (n int, err error) { i, ml := 0, int(xx.memIdx) n = len(in) xx.ln += int32(n) if d := 16 - ml; ml > 0 && ml+len(in) > 16 { xx.memIdx += int32(copy(xx.mem[xx.memIdx:], in[:d])) ml, in = 16, in[d:len(in):len(in)] } else if ml+len(in) < 16 { xx.memIdx += int32(copy(xx.mem[xx.memIdx:], in)) return } if ml > 0 { i += 16 - ml xx.memIdx += int32(copy(xx.mem[xx.memIdx:len(xx.mem):len(xx.mem)], in)) in := xx.mem[:16:len(xx.mem)] xx.v1 += u32(in[0:4:len(in)]) * prime32x2 xx.v1 = rotl32_13(xx.v1) * prime32x1 xx.v2 += u32(in[4:8:len(in)]) * prime32x2 xx.v2 = rotl32_13(xx.v2) * prime32x1 xx.v3 += u32(in[8:12:len(in)]) * prime32x2 xx.v3 = rotl32_13(xx.v3) * prime32x1 xx.v4 += u32(in[12:16:len(in)]) * prime32x2 xx.v4 = rotl32_13(xx.v4) * prime32x1 xx.memIdx = 0 } for ; i <= len(in)-16; i += 16 { in := in[i : i+16 : len(in)] xx.v1 += u32(in[0:4:len(in)]) * prime32x2 xx.v1 = rotl32_13(xx.v1) * prime32x1 xx.v2 += u32(in[4:8:len(in)]) * prime32x2 xx.v2 = rotl32_13(xx.v2) * prime32x1 xx.v3 += u32(in[8:12:len(in)]) * prime32x2 xx.v3 = rotl32_13(xx.v3) * prime32x1 xx.v4 += u32(in[12:16:len(in)]) * prime32x2 xx.v4 = rotl32_13(xx.v4) * prime32x1 } if len(in)-i != 0 { xx.memIdx += int32(copy(xx.mem[xx.memIdx:], in[i:len(in):len(in)])) } return } func (xx *XXHash32) Sum32() (h uint32) { var i int32 if xx.ln > 15 { h = rotl32_1(xx.v1) + rotl32_7(xx.v2) + rotl32_12(xx.v3) + rotl32_18(xx.v4) } else { h = xx.seed + prime32x5 } h += uint32(xx.ln) if xx.memIdx > 0 { for ; i < xx.memIdx-3; i += 4 { in := xx.mem[i : i+4 : len(xx.mem)] h += u32(in[0:4:len(in)]) * prime32x3 h = rotl32_17(h) * prime32x4 } for ; i < xx.memIdx; i++ { h += uint32(xx.mem[i]) * prime32x5 h = rotl32_11(h) * prime32x1 } } h ^= h >> 15 h *= prime32x2 h ^= h >> 13 h *= prime32x3 h ^= h >> 16 return } // Checksum64S returns the 64bit xxhash checksum for a single input func Checksum64S(in []byte, seed uint64) uint64 { if len(in) == 0 && seed == 0 { return 0xef46db3751d8e999 } if len(in) > 31 { return checksum64(in, seed) } return checksum64Short(in, seed) } xxhash-1.2.4/xxhash_safe.go000066400000000000000000000075531342732511100156620ustar00rootroot00000000000000// +build appengine safe ppc64le ppc64be mipsle mips s390x package xxhash // Backend returns the current version of xxhash being used. const Backend = "GoSafe" func ChecksumString32S(s string, seed uint32) uint32 { return Checksum32S([]byte(s), seed) } func (xx *XXHash32) WriteString(s string) (int, error) { if len(s) == 0 { return 0, nil } return xx.Write([]byte(s)) } func ChecksumString64S(s string, seed uint64) uint64 { return Checksum64S([]byte(s), seed) } func (xx *XXHash64) WriteString(s string) (int, error) { if len(s) == 0 { return 0, nil } return xx.Write([]byte(s)) } func checksum64(in []byte, seed uint64) (h uint64) { var ( v1, v2, v3, v4 = resetVs64(seed) i int ) for ; i < len(in)-31; i += 32 { in := in[i : i+32 : len(in)] v1 = round64(v1, u64(in[0:8:len(in)])) v2 = round64(v2, u64(in[8:16:len(in)])) v3 = round64(v3, u64(in[16:24:len(in)])) v4 = round64(v4, u64(in[24:32:len(in)])) } h = rotl64_1(v1) + rotl64_7(v2) + rotl64_12(v3) + rotl64_18(v4) h = mergeRound64(h, v1) h = mergeRound64(h, v2) h = mergeRound64(h, v3) h = mergeRound64(h, v4) h += uint64(len(in)) for ; i < len(in)-7; i += 8 { h ^= round64(0, u64(in[i:len(in):len(in)])) h = rotl64_27(h)*prime64x1 + prime64x4 } for ; i < len(in)-3; i += 4 { h ^= uint64(u32(in[i:len(in):len(in)])) * prime64x1 h = rotl64_23(h)*prime64x2 + prime64x3 } for ; i < len(in); i++ { h ^= uint64(in[i]) * prime64x5 h = rotl64_11(h) * prime64x1 } return mix64(h) } func checksum64Short(in []byte, seed uint64) uint64 { var ( h = seed + prime64x5 + uint64(len(in)) i int ) for ; i < len(in)-7; i += 8 { k := u64(in[i : i+8 : len(in)]) h ^= round64(0, k) h = rotl64_27(h)*prime64x1 + prime64x4 } for ; i < len(in)-3; i += 4 { h ^= uint64(u32(in[i:i+4:len(in)])) * prime64x1 h = rotl64_23(h)*prime64x2 + prime64x3 } for ; i < len(in); i++ { h ^= uint64(in[i]) * prime64x5 h = rotl64_11(h) * prime64x1 } return mix64(h) } func (xx *XXHash64) Write(in []byte) (n int, err error) { var ( ml = int(xx.memIdx) d = 32 - ml ) n = len(in) xx.ln += uint64(n) if ml+len(in) < 32 { xx.memIdx += int8(copy(xx.mem[xx.memIdx:len(xx.mem):len(xx.mem)], in)) return } i, v1, v2, v3, v4 := 0, xx.v1, xx.v2, xx.v3, xx.v4 if ml > 0 && ml+len(in) > 32 { xx.memIdx += int8(copy(xx.mem[xx.memIdx:len(xx.mem):len(xx.mem)], in[:d:len(in)])) in = in[d:len(in):len(in)] in := xx.mem[0:32:len(xx.mem)] v1 = round64(v1, u64(in[0:8:len(in)])) v2 = round64(v2, u64(in[8:16:len(in)])) v3 = round64(v3, u64(in[16:24:len(in)])) v4 = round64(v4, u64(in[24:32:len(in)])) xx.memIdx = 0 } for ; i < len(in)-31; i += 32 { in := in[i : i+32 : len(in)] v1 = round64(v1, u64(in[0:8:len(in)])) v2 = round64(v2, u64(in[8:16:len(in)])) v3 = round64(v3, u64(in[16:24:len(in)])) v4 = round64(v4, u64(in[24:32:len(in)])) } if len(in)-i != 0 { xx.memIdx += int8(copy(xx.mem[xx.memIdx:], in[i:len(in):len(in)])) } xx.v1, xx.v2, xx.v3, xx.v4 = v1, v2, v3, v4 return } func (xx *XXHash64) Sum64() (h uint64) { var i int if xx.ln > 31 { v1, v2, v3, v4 := xx.v1, xx.v2, xx.v3, xx.v4 h = rotl64_1(v1) + rotl64_7(v2) + rotl64_12(v3) + rotl64_18(v4) h = mergeRound64(h, v1) h = mergeRound64(h, v2) h = mergeRound64(h, v3) h = mergeRound64(h, v4) } else { h = xx.seed + prime64x5 } h += uint64(xx.ln) if xx.memIdx > 0 { in := xx.mem[:xx.memIdx] for ; i < int(xx.memIdx)-7; i += 8 { in := in[i : i+8 : len(in)] k := u64(in[0:8:len(in)]) k *= prime64x2 k = rotl64_31(k) k *= prime64x1 h ^= k h = rotl64_27(h)*prime64x1 + prime64x4 } for ; i < int(xx.memIdx)-3; i += 4 { in := in[i : i+4 : len(in)] h ^= uint64(u32(in[0:4:len(in)])) * prime64x1 h = rotl64_23(h)*prime64x2 + prime64x3 } for ; i < int(xx.memIdx); i++ { h ^= uint64(in[i]) * prime64x5 h = rotl64_11(h) * prime64x1 } } return mix64(h) } xxhash-1.2.4/xxhash_test.go000066400000000000000000000165031342732511100157160ustar00rootroot00000000000000package xxhash_test import ( "bytes" "encoding/binary" "hash/adler32" "hash/crc32" "hash/crc64" "hash/fnv" "os" "strconv" "testing" "github.com/OneOfOne/xxhash" ) const inS = `Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ` var ( in = []byte(inS) ) const ( expected32 uint32 = 0x6101218F expected64 uint64 = 0xFFAE31BEBFED7652 ) func Test(t *testing.T) { t.Log("xxhash backend:", xxhash.Backend) t.Log("Benchmark string len:", len(inS)) } func TestHash32(t *testing.T) { h := xxhash.New32() h.Write(in) r := h.Sum32() if r != expected32 { t.Errorf("expected 0x%x, got 0x%x.", expected32, r) } } func TestHash32Short(t *testing.T) { r := xxhash.Checksum32(in) if r != expected32 { t.Errorf("expected 0x%x, got 0x%x.", expected32, r) } } func TestWriteStringNil(t *testing.T) { h32, h64 := xxhash.New32(), xxhash.New64() for i := 0; i < 1e6; i++ { h32.WriteString("") h64.WriteString("") } _, _ = h32.Sum32(), h64.Sum64() } var testsTable = []struct { input string want uint64 }{ {"", 0xef46db3751d8e999}, {"a", 0xd24ec4f1a98c6e5b}, {"as", 0x1c330fb2d66be179}, {"asd", 0x631c37ce72a97393}, {"asdf", 0x415872f599cea71e}, { // Exactly 63 characters, which exercises all code paths. "Call me Ishmael. Some years ago--never mind how long precisely-", 0x02a2e85470d6fd96, }, {"The quick brown fox jumps over the lazy dog http://i.imgur.com/VHQXScB.gif", 0x93267f9820452ead}, {string(in), expected64}, } // Shamelessly copied from https://github.com/cespare/xxhash/blob/5c37fe3735342a2e0d01c87a907579987c8936cc/xxhash_test.go#L28 func TestSum64(t *testing.T) { for i, tt := range testsTable { for chunkSize := 1; chunkSize <= len(tt.input); chunkSize++ { x := xxhash.New64() for j := 0; j < len(tt.input); j += chunkSize { end := j + chunkSize if end > len(tt.input) { end = len(tt.input) } chunk := tt.input[j:end] n, err := x.WriteString(chunk) if err != nil || n != len(chunk) { t.Fatalf("[i=%d,chunkSize=%d] Write: got (%d, %v); want (%d, nil)", i, chunkSize, n, err, len(chunk)) } } if got := x.Sum64(); got != tt.want { t.Fatalf("[i=%d,chunkSize=%d] got 0x%x; want 0x%x", i, chunkSize, got, tt.want) } if got := x.Sum64(); got != tt.want { t.Fatalf("[i=%d,chunkSize=%d] got 0x%x; want 0x%x (called .Sum64 twice)", i, chunkSize, got, tt.want) } var b [8]byte binary.BigEndian.PutUint64(b[:], tt.want) if got := x.Sum(nil); !bytes.Equal(got, b[:]) { t.Fatalf("[i=%d,chunkSize=%d] Sum: got %v; want %v", i, chunkSize, got, b[:]) } } if got := xxhash.ChecksumString64(tt.input); got != tt.want { t.Fatalf("[i=%d] ChecksumString64: got 0x%x; want 0x%x", i, got, tt.want) } } } func BenchmarkXXChecksum32(b *testing.B) { for i := 0; i < b.N; i++ { xxhash.Checksum32(in) } } func BenchmarkXXChecksumString32(b *testing.B) { for i := 0; i < b.N; i++ { xxhash.ChecksumString32(inS) } } func BenchmarkXXSum64(b *testing.B) { b.Run("Func", func(b *testing.B) { for i := 0; i < b.N; i++ { xxhash.Checksum64(in) } }) b.Run("Struct", func(b *testing.B) { h := xxhash.New64() for i := 0; i < b.N; i++ { h.Write(in) h.Sum64() h.Reset() } }) } func BenchmarkXXSum64Short(b *testing.B) { k := []byte("Test-key-1000") b.Run("Func", func(b *testing.B) { for i := 0; i < b.N; i++ { xxhash.Checksum64(k) } }) b.Run("Struct", func(b *testing.B) { h := xxhash.New64() for i := 0; i < b.N; i++ { h.Write(k) h.Sum64() h.Reset() } }) } func BenchmarkXXSum64EvenPoint(b *testing.B) { if os.Getenv("EP") == "" { b.SkipNow() } for i := 256; i < len(in); i += 256 { block := in[:i] b.Run("Func/"+strconv.Itoa(i), func(b *testing.B) { for i := 0; i < b.N; i++ { xxhash.Checksum64(block) } }) b.Run("Struct/"+strconv.Itoa(i), func(b *testing.B) { h := xxhash.New64() for i := 0; i < b.N; i++ { h.Write(block) h.Sum64() h.Reset() } }) } } func BenchmarkFnv32(b *testing.B) { var bv []byte h := fnv.New32() for i := 0; i < b.N; i++ { h.Write(in) bv = h.Sum(nil) h.Reset() } _ = bv } func BenchmarkAdler32(b *testing.B) { for i := 0; i < b.N; i++ { adler32.Checksum(in) } } func BenchmarkCRC32IEEE(b *testing.B) { for i := 0; i < b.N; i++ { crc32.ChecksumIEEE(in) } } func BenchmarkCRC32IEEEString(b *testing.B) { for i := 0; i < b.N; i++ { crc32.ChecksumIEEE([]byte(inS)) } } var crc64ISO = crc64.MakeTable(crc64.ISO) func BenchmarkCRC64ISO(b *testing.B) { for i := 0; i < b.N; i++ { crc64.Checksum(in, crc64ISO) } } func BenchmarkCRC64ISOString(b *testing.B) { for i := 0; i < b.N; i++ { crc64.Checksum([]byte(inS), crc64ISO) } } func BenchmarkCRC32IEEEShort(b *testing.B) { k := []byte("Test-key-100") for i := 0; i < b.N; i++ { crc32.ChecksumIEEE(k) } } func BenchmarkCRC64ISOShort(b *testing.B) { k := []byte("Test-key-100") for i := 0; i < b.N; i++ { crc64.Checksum(k, crc64ISO) } } func BenchmarkFnv64(b *testing.B) { h := fnv.New64() for i := 0; i < b.N; i++ { h.Write(in) h.Sum(nil) h.Reset() } } func BenchmarkFnv64Short(b *testing.B) { var bv []byte k := []byte("Test-key-100") for i := 0; i < b.N; i++ { h := fnv.New64() h.Write(k) bv = h.Sum(nil) } _ = bv } xxhash-1.2.4/xxhash_unsafe.go000066400000000000000000000121571342732511100162210ustar00rootroot00000000000000// +build !safe // +build !appengine // +build !ppc64le // +build !mipsle // +build !ppc64be // +build !mips // +build !s390x package xxhash import ( "reflect" "unsafe" ) // Backend returns the current version of xxhash being used. const Backend = "GoUnsafe" // ChecksumString32S returns the checksum of the input data, without creating a copy, with the specific seed. func ChecksumString32S(s string, seed uint32) uint32 { if len(s) == 0 { return Checksum32S(nil, seed) } ss := (*reflect.StringHeader)(unsafe.Pointer(&s)) return Checksum32S((*[maxInt32]byte)(unsafe.Pointer(ss.Data))[:len(s):len(s)], seed) } func (xx *XXHash32) WriteString(s string) (int, error) { if len(s) == 0 { return 0, nil } ss := (*reflect.StringHeader)(unsafe.Pointer(&s)) return xx.Write((*[maxInt32]byte)(unsafe.Pointer(ss.Data))[:len(s):len(s)]) } // ChecksumString64S returns the checksum of the input data, without creating a copy, with the specific seed. func ChecksumString64S(s string, seed uint64) uint64 { if len(s) == 0 { return Checksum64S(nil, seed) } ss := (*reflect.StringHeader)(unsafe.Pointer(&s)) return Checksum64S((*[maxInt32]byte)(unsafe.Pointer(ss.Data))[:len(s):len(s)], seed) } func (xx *XXHash64) WriteString(s string) (int, error) { if len(s) == 0 { return 0, nil } ss := (*reflect.StringHeader)(unsafe.Pointer(&s)) return xx.Write((*[maxInt32]byte)(unsafe.Pointer(ss.Data))[:len(s)]) } func checksum64(in []byte, seed uint64) uint64 { var ( wordsLen = len(in) >> 3 words = ((*[maxInt32 / 8]uint64)(unsafe.Pointer(&in[0])))[:wordsLen:wordsLen] h uint64 = prime64x5 v1, v2, v3, v4 = resetVs64(seed) i int ) for ; i < len(words)-3; i += 4 { words := (*[4]uint64)(unsafe.Pointer(&words[i])) v1 = round64(v1, words[0]) v2 = round64(v2, words[1]) v3 = round64(v3, words[2]) v4 = round64(v4, words[3]) } h = rotl64_1(v1) + rotl64_7(v2) + rotl64_12(v3) + rotl64_18(v4) h = mergeRound64(h, v1) h = mergeRound64(h, v2) h = mergeRound64(h, v3) h = mergeRound64(h, v4) h += uint64(len(in)) for _, k := range words[i:] { h ^= round64(0, k) h = rotl64_27(h)*prime64x1 + prime64x4 } if in = in[wordsLen<<3 : len(in) : len(in)]; len(in) > 3 { words := (*[1]uint32)(unsafe.Pointer(&in[0])) h ^= uint64(words[0]) * prime64x1 h = rotl64_23(h)*prime64x2 + prime64x3 in = in[4:len(in):len(in)] } for _, b := range in { h ^= uint64(b) * prime64x5 h = rotl64_11(h) * prime64x1 } return mix64(h) } func checksum64Short(in []byte, seed uint64) uint64 { var ( h = seed + prime64x5 + uint64(len(in)) i int ) if len(in) > 7 { var ( wordsLen = len(in) >> 3 words = ((*[maxInt32 / 8]uint64)(unsafe.Pointer(&in[0])))[:wordsLen:wordsLen] ) for i := range words { h ^= round64(0, words[i]) h = rotl64_27(h)*prime64x1 + prime64x4 } i = wordsLen << 3 } if in = in[i:len(in):len(in)]; len(in) > 3 { words := (*[1]uint32)(unsafe.Pointer(&in[0])) h ^= uint64(words[0]) * prime64x1 h = rotl64_23(h)*prime64x2 + prime64x3 in = in[4:len(in):len(in)] } for _, b := range in { h ^= uint64(b) * prime64x5 h = rotl64_11(h) * prime64x1 } return mix64(h) } func (xx *XXHash64) Write(in []byte) (n int, err error) { mem, idx := xx.mem[:], int(xx.memIdx) xx.ln, n = xx.ln+uint64(len(in)), len(in) if idx+len(in) < 32 { xx.memIdx += int8(copy(mem[idx:len(mem):len(mem)], in)) return } var ( v1, v2, v3, v4 = xx.v1, xx.v2, xx.v3, xx.v4 i int ) if d := 32 - int(idx); d > 0 && int(idx)+len(in) > 31 { copy(mem[idx:len(mem):len(mem)], in[:len(in):len(in)]) words := (*[4]uint64)(unsafe.Pointer(&mem[0])) v1 = round64(v1, words[0]) v2 = round64(v2, words[1]) v3 = round64(v3, words[2]) v4 = round64(v4, words[3]) if in, xx.memIdx = in[d:len(in):len(in)], 0; len(in) == 0 { goto RET } } for ; i < len(in)-31; i += 32 { words := (*[4]uint64)(unsafe.Pointer(&in[i])) v1 = round64(v1, words[0]) v2 = round64(v2, words[1]) v3 = round64(v3, words[2]) v4 = round64(v4, words[3]) } if len(in)-i != 0 { xx.memIdx += int8(copy(mem[xx.memIdx:len(mem):len(mem)], in[i:len(in):len(in)])) } RET: xx.v1, xx.v2, xx.v3, xx.v4 = v1, v2, v3, v4 return } func (xx *XXHash64) Sum64() (h uint64) { if seed := xx.seed; xx.ln > 31 { v1, v2, v3, v4 := xx.v1, xx.v2, xx.v3, xx.v4 h = rotl64_1(v1) + rotl64_7(v2) + rotl64_12(v3) + rotl64_18(v4) h = mergeRound64(h, v1) h = mergeRound64(h, v2) h = mergeRound64(h, v3) h = mergeRound64(h, v4) } else if seed == 0 { h = prime64x5 } else { h = seed + prime64x5 } h += uint64(xx.ln) if xx.memIdx == 0 { return mix64(h) } var ( in = xx.mem[:xx.memIdx:xx.memIdx] wordsLen = len(in) >> 3 words = ((*[maxInt32 / 8]uint64)(unsafe.Pointer(&in[0])))[:wordsLen:wordsLen] ) for _, k := range words { h ^= round64(0, k) h = rotl64_27(h)*prime64x1 + prime64x4 } if in = in[wordsLen<<3 : len(in) : len(in)]; len(in) > 3 { words := (*[1]uint32)(unsafe.Pointer(&in[0])) h ^= uint64(words[0]) * prime64x1 h = rotl64_23(h)*prime64x2 + prime64x3 in = in[4:len(in):len(in)] } for _, b := range in { h ^= uint64(b) * prime64x5 h = rotl64_11(h) * prime64x1 } return mix64(h) }