hashlittle 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/.rubocop.yml +33 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +22 -0
- data/ext/hashlittle/extconf.rb +10 -0
- data/ext/hashlittle/hashlittle.c +66 -0
- data/ext/hashlittle/hashlittle.h +6 -0
- data/ext/hashlittle/lookup.h +24 -0
- data/ext/hashlittle/lookup3.c +747 -0
- data/lib/hashlittle/version.rb +5 -0
- data/lib/hashlittle.rb +9 -0
- data/sig/hashlittle.rbs +4 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b88cfbdb270614ebe40be9d44cbde601fa0492d1dd120237451e96eb16830291
|
4
|
+
data.tar.gz: 49fe46fa8cf98685205c16934070c4dae4b8253cdfabdb30bde31cb3918349e3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bd7e1962db677db183fbb6e0e3e49ba1161303966978e3bc8fcffb16e7e8ce863816f197c9cd967e0a9ffef211b0ecb98b4d17afc255827b3cd142e9a084d324
|
7
|
+
data.tar.gz: 0ce4419afddf9d05be265282da6ca6a30ae08b9bb92cd40d75fca0a578d291c651507f3603753bbefd9d31b8b252baab103d4c830e4ad0caf87f0e47d1f8be0f
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rake
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 3.0
|
7
|
+
NewCops: enable
|
8
|
+
|
9
|
+
Style/StringLiterals:
|
10
|
+
Enabled: true
|
11
|
+
EnforcedStyle: double_quotes
|
12
|
+
|
13
|
+
Style/StringLiteralsInInterpolation:
|
14
|
+
Enabled: true
|
15
|
+
EnforcedStyle: double_quotes
|
16
|
+
|
17
|
+
Layout/LineLength:
|
18
|
+
Max: 120
|
19
|
+
|
20
|
+
Style/TrailingCommaInArrayLiteral:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Metrics/CollectionLiteralLength:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Rake/Desc:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
RSpec/MultipleExpectations:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
RSpec/ExampleLength:
|
33
|
+
Enabled: false
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at zed.0xff@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Andrey "Zed" Zaikin
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Hashlittle
|
2
|
+
|
3
|
+
Bob Jenkins' hashlittle and hashlittle2 non-cryptographic hash functions.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add hashlittle
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install hashlittle
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
require 'hashlittle'
|
18
|
+
|
19
|
+
Hashlittle.hashlittle("test") # 3188463954
|
20
|
+
Hashlittle.hashlittle("test", 123) # 1744958730 optional seed argument
|
21
|
+
|
22
|
+
Hashlittle.hashlittle2("test") # [3188463954, 1839385006]
|
23
|
+
Hashlittle.hashlittle2("test", a) # [3188463954, 1839385006] store result in a pre-existing array
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zed-0xff/hashlittle. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/zed-0xff/hashlittle/blob/master/CODE_OF_CONDUCT.md).
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
38
|
+
|
39
|
+
## Code of Conduct
|
40
|
+
|
41
|
+
Everyone interacting in the Hashlittle project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zed-0xff/hashlittle/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
require "rubocop/rake_task"
|
9
|
+
|
10
|
+
RuboCop::RakeTask.new
|
11
|
+
|
12
|
+
require "rake/extensiontask"
|
13
|
+
|
14
|
+
task build: :compile
|
15
|
+
|
16
|
+
GEMSPEC = Gem::Specification.load("hashlittle.gemspec")
|
17
|
+
|
18
|
+
Rake::ExtensionTask.new("hashlittle", GEMSPEC) do |ext|
|
19
|
+
ext.lib_dir = "lib/hashlittle"
|
20
|
+
end
|
21
|
+
|
22
|
+
task default: %i[clobber compile spec rubocop]
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "mkmf"
|
4
|
+
|
5
|
+
# Makes all symbols private by default to avoid unintended conflict
|
6
|
+
# with other gems. To explicitly export symbols you can use RUBY_FUNC_EXPORTED
|
7
|
+
# selectively, or entirely remove this flag.
|
8
|
+
append_cflags("-fvisibility=hidden")
|
9
|
+
|
10
|
+
create_makefile("hashlittle/hashlittle")
|
@@ -0,0 +1,66 @@
|
|
1
|
+
#include "hashlittle.h"
|
2
|
+
#include "lookup.h"
|
3
|
+
|
4
|
+
VALUE rb_mHashlittle;
|
5
|
+
|
6
|
+
static VALUE rb_hashlittle(int argc, VALUE *argv, VALUE self) {
|
7
|
+
VALUE rb_key, rb_initval;
|
8
|
+
uint32_t initval = 0;
|
9
|
+
|
10
|
+
rb_scan_args(argc, argv, "11", &rb_key, &rb_initval);
|
11
|
+
|
12
|
+
Check_Type(rb_key, T_STRING);
|
13
|
+
|
14
|
+
// Get the byte array from the Ruby string
|
15
|
+
uint8_t *key = (uint8_t *)RSTRING_PTR(rb_key);
|
16
|
+
long key_len = RSTRING_LEN(rb_key);
|
17
|
+
|
18
|
+
// If initval is provided, use it
|
19
|
+
if (!NIL_P(rb_initval)) {
|
20
|
+
initval = NUM2UINT(rb_initval);
|
21
|
+
}
|
22
|
+
|
23
|
+
// Compute the hash using hashlittle function
|
24
|
+
uint32_t result = hashlittle(key, key_len, initval);
|
25
|
+
|
26
|
+
// Return the hash as a Ruby integer
|
27
|
+
return UINT2NUM(result);
|
28
|
+
}
|
29
|
+
|
30
|
+
static VALUE rb_hashlittle2(int argc, VALUE *argv, VALUE self) {
|
31
|
+
VALUE rb_key, rb_result;
|
32
|
+
uint32_t pb = 0;
|
33
|
+
uint32_t pc = 0;
|
34
|
+
|
35
|
+
rb_scan_args(argc, argv, "11", &rb_key, &rb_result);
|
36
|
+
|
37
|
+
if (NIL_P(rb_result)) {
|
38
|
+
rb_result = rb_ary_new2(2);
|
39
|
+
} else {
|
40
|
+
Check_Type(rb_result, T_ARRAY);
|
41
|
+
rb_ary_modify(rb_result);
|
42
|
+
}
|
43
|
+
|
44
|
+
Check_Type(rb_key, T_STRING);
|
45
|
+
uint8_t *key = (uint8_t *)RSTRING_PTR(rb_key);
|
46
|
+
long key_len = RSTRING_LEN(rb_key);
|
47
|
+
|
48
|
+
hashlittle2(key, key_len, &pc, &pb);
|
49
|
+
|
50
|
+
if( RARRAY_LEN(rb_result) < 2 ){
|
51
|
+
rb_ary_resize(rb_result, 2);
|
52
|
+
}
|
53
|
+
|
54
|
+
rb_ary_store(rb_result, 0, UINT2NUM(pc));
|
55
|
+
rb_ary_store(rb_result, 1, UINT2NUM(pb));
|
56
|
+
|
57
|
+
return rb_result;
|
58
|
+
}
|
59
|
+
|
60
|
+
RUBY_FUNC_EXPORTED void
|
61
|
+
Init_hashlittle(void)
|
62
|
+
{
|
63
|
+
rb_mHashlittle = rb_define_module("Hashlittle");
|
64
|
+
rb_define_singleton_method(rb_mHashlittle, "hashlittle", rb_hashlittle, -1);
|
65
|
+
rb_define_singleton_method(rb_mHashlittle, "hashlittle2", rb_hashlittle2, -1);
|
66
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#ifndef __LOOKUP3_H__
|
2
|
+
#define __LOOKUP3_H__
|
3
|
+
|
4
|
+
#ifdef WIN32
|
5
|
+
typedef unsigned char uint8_t;
|
6
|
+
typedef unsigned short uint16_t;
|
7
|
+
typedef unsigned int uint32_t;
|
8
|
+
#else
|
9
|
+
#include <stdint.h> /* defines uint32_t etc */
|
10
|
+
#endif
|
11
|
+
|
12
|
+
#ifdef __cplusplus
|
13
|
+
extern "C"
|
14
|
+
{
|
15
|
+
#endif
|
16
|
+
|
17
|
+
uint32_t hashlittle(const void *key, size_t length, uint32_t initval);
|
18
|
+
void hashlittle2(const void *key, size_t length, uint32_t *pc, uint32_t *pb);
|
19
|
+
|
20
|
+
#ifdef __cplusplus
|
21
|
+
}
|
22
|
+
#endif
|
23
|
+
|
24
|
+
#endif // __LOOKUP3_H__
|
@@ -0,0 +1,747 @@
|
|
1
|
+
/*
|
2
|
+
-------------------------------------------------------------------------------
|
3
|
+
lookup3.c, by Bob Jenkins, May 2006, Public Domain.
|
4
|
+
|
5
|
+
These are functions for producing 32-bit hashes for hash table lookup.
|
6
|
+
hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
|
7
|
+
are externally useful functions. Routines to test the hash are included
|
8
|
+
if SELF_TEST is defined. You can use this free for any purpose. It's in
|
9
|
+
the public domain. It has no warranty.
|
10
|
+
|
11
|
+
You probably want to use hashlittle(). hashlittle() and hashbig()
|
12
|
+
hash byte arrays. hashlittle() is is faster than hashbig() on
|
13
|
+
little-endian machines. Intel and AMD are little-endian machines.
|
14
|
+
On second thought, you probably want hashlittle2(), which is identical to
|
15
|
+
hashlittle() except it returns two 32-bit hashes for the price of one.
|
16
|
+
You could implement hashbig2() if you wanted but I haven't bothered here.
|
17
|
+
|
18
|
+
If you want to find a hash of, say, exactly 7 integers, do
|
19
|
+
a = i1; b = i2; c = i3;
|
20
|
+
mix(a,b,c);
|
21
|
+
a += i4; b += i5; c += i6;
|
22
|
+
mix(a,b,c);
|
23
|
+
a += i7;
|
24
|
+
final(a,b,c);
|
25
|
+
then use c as the hash value. If you have a variable length array of
|
26
|
+
4-byte integers to hash, use hashword(). If you have a byte array (like
|
27
|
+
a character string), use hashlittle(). If you have several byte arrays, or
|
28
|
+
a mix of things, see the comments above hashlittle().
|
29
|
+
|
30
|
+
Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
|
31
|
+
then mix those integers. This is fast (you can do a lot more thorough
|
32
|
+
mixing with 12*3 instructions on 3 integers than you can with 3 instructions
|
33
|
+
on 1 byte), but shoehorning those bytes into integers efficiently is messy.
|
34
|
+
-------------------------------------------------------------------------------
|
35
|
+
*/
|
36
|
+
//#define SELF_TEST 1
|
37
|
+
|
38
|
+
#include <stdio.h> /* defines printf for tests */
|
39
|
+
#include <time.h> /* defines time_t for timings in the test */
|
40
|
+
|
41
|
+
#ifdef linux
|
42
|
+
#include <sys/param.h> /* attempt to define endianness */
|
43
|
+
#include <endian.h> /* attempt to define endianness */
|
44
|
+
#endif
|
45
|
+
|
46
|
+
#include "lookup.h"
|
47
|
+
|
48
|
+
/*
|
49
|
+
* My best guess at if you are big-endian or little-endian. This may
|
50
|
+
* need adjustment.
|
51
|
+
*/
|
52
|
+
#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
|
53
|
+
__BYTE_ORDER == __LITTLE_ENDIAN) || \
|
54
|
+
(defined(i386) || defined(__i386__) || defined(__i486__) || \
|
55
|
+
defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
|
56
|
+
# define HASH_LITTLE_ENDIAN 1
|
57
|
+
# define HASH_BIG_ENDIAN 0
|
58
|
+
#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
|
59
|
+
__BYTE_ORDER == __BIG_ENDIAN) || \
|
60
|
+
(defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
|
61
|
+
# define HASH_LITTLE_ENDIAN 0
|
62
|
+
# define HASH_BIG_ENDIAN 1
|
63
|
+
#else
|
64
|
+
# define HASH_LITTLE_ENDIAN 0
|
65
|
+
# define HASH_BIG_ENDIAN 0
|
66
|
+
#endif
|
67
|
+
|
68
|
+
#define hashsize(n) ((uint32_t)1<<(n))
|
69
|
+
#define hashmask(n) (hashsize(n)-1)
|
70
|
+
#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
|
71
|
+
|
72
|
+
/* Fixup some warnings in MS Visual C++ */
|
73
|
+
#ifdef _MSC_VER
|
74
|
+
#pragma warning(disable: 4127) // warning C4127: conditional expression is constant
|
75
|
+
#pragma warning(disable: 4101) // warning C4101: 'k8': unreferenced local variable
|
76
|
+
#endif
|
77
|
+
|
78
|
+
/*
|
79
|
+
-------------------------------------------------------------------------------
|
80
|
+
mix -- mix 3 32-bit values reversibly.
|
81
|
+
|
82
|
+
This is reversible, so any information in (a,b,c) before mix() is
|
83
|
+
still in (a,b,c) after mix().
|
84
|
+
|
85
|
+
If four pairs of (a,b,c) inputs are run through mix(), or through
|
86
|
+
mix() in reverse, there are at least 32 bits of the output that
|
87
|
+
are sometimes the same for one pair and different for another pair.
|
88
|
+
This was tested for:
|
89
|
+
* pairs that differed by one bit, by two bits, in any combination
|
90
|
+
of top bits of (a,b,c), or in any combination of bottom bits of
|
91
|
+
(a,b,c).
|
92
|
+
* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
|
93
|
+
the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
|
94
|
+
is commonly produced by subtraction) look like a single 1-bit
|
95
|
+
difference.
|
96
|
+
* the base values were pseudorandom, all zero but one bit set, or
|
97
|
+
all zero plus a counter that starts at zero.
|
98
|
+
|
99
|
+
Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
|
100
|
+
satisfy this are
|
101
|
+
4 6 8 16 19 4
|
102
|
+
9 15 3 18 27 15
|
103
|
+
14 9 3 7 17 3
|
104
|
+
Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
|
105
|
+
for "differ" defined as + with a one-bit base and a two-bit delta. I
|
106
|
+
used http://burtleburtle.net/bob/hash/avalanche.html to choose
|
107
|
+
the operations, constants, and arrangements of the variables.
|
108
|
+
|
109
|
+
This does not achieve avalanche. There are input bits of (a,b,c)
|
110
|
+
that fail to affect some output bits of (a,b,c), especially of a. The
|
111
|
+
most thoroughly mixed value is c, but it doesn't really even achieve
|
112
|
+
avalanche in c.
|
113
|
+
|
114
|
+
This allows some parallelism. Read-after-writes are good at doubling
|
115
|
+
the number of bits affected, so the goal of mixing pulls in the opposite
|
116
|
+
direction as the goal of parallelism. I did what I could. Rotates
|
117
|
+
seem to cost as much as shifts on every machine I could lay my hands
|
118
|
+
on, and rotates are much kinder to the top and bottom bits, so I used
|
119
|
+
rotates.
|
120
|
+
-------------------------------------------------------------------------------
|
121
|
+
*/
|
122
|
+
#define mix(a,b,c) \
|
123
|
+
{ \
|
124
|
+
a -= c; a ^= rot(c, 4); c += b; \
|
125
|
+
b -= a; b ^= rot(a, 6); a += c; \
|
126
|
+
c -= b; c ^= rot(b, 8); b += a; \
|
127
|
+
a -= c; a ^= rot(c,16); c += b; \
|
128
|
+
b -= a; b ^= rot(a,19); a += c; \
|
129
|
+
c -= b; c ^= rot(b, 4); b += a; \
|
130
|
+
}
|
131
|
+
|
132
|
+
/*
|
133
|
+
-------------------------------------------------------------------------------
|
134
|
+
final -- final mixing of 3 32-bit values (a,b,c) into c
|
135
|
+
|
136
|
+
Pairs of (a,b,c) values differing in only a few bits will usually
|
137
|
+
produce values of c that look totally different. This was tested for
|
138
|
+
* pairs that differed by one bit, by two bits, in any combination
|
139
|
+
of top bits of (a,b,c), or in any combination of bottom bits of
|
140
|
+
(a,b,c).
|
141
|
+
* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
|
142
|
+
the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
|
143
|
+
is commonly produced by subtraction) look like a single 1-bit
|
144
|
+
difference.
|
145
|
+
* the base values were pseudorandom, all zero but one bit set, or
|
146
|
+
all zero plus a counter that starts at zero.
|
147
|
+
|
148
|
+
These constants passed:
|
149
|
+
14 11 25 16 4 14 24
|
150
|
+
12 14 25 16 4 14 24
|
151
|
+
and these came close:
|
152
|
+
4 8 15 26 3 22 24
|
153
|
+
10 8 15 26 3 22 24
|
154
|
+
11 8 15 26 3 22 24
|
155
|
+
-------------------------------------------------------------------------------
|
156
|
+
*/
|
157
|
+
#define final(a,b,c) \
|
158
|
+
{ \
|
159
|
+
c ^= b; c -= rot(b,14); \
|
160
|
+
a ^= c; a -= rot(c,11); \
|
161
|
+
b ^= a; b -= rot(a,25); \
|
162
|
+
c ^= b; c -= rot(b,16); \
|
163
|
+
a ^= c; a -= rot(c,4); \
|
164
|
+
b ^= a; b -= rot(a,14); \
|
165
|
+
c ^= b; c -= rot(b,24); \
|
166
|
+
}
|
167
|
+
|
168
|
+
/*
|
169
|
+
--------------------------------------------------------------------
|
170
|
+
This works on all machines. To be useful, it requires
|
171
|
+
-- that the key be an array of uint32_t's, and
|
172
|
+
-- that the length be the number of uint32_t's in the key
|
173
|
+
|
174
|
+
The function hashword() is identical to hashlittle() on little-endian
|
175
|
+
machines, and identical to hashbig() on big-endian machines,
|
176
|
+
except that the length has to be measured in uint32_ts rather than in
|
177
|
+
bytes. hashlittle() is more complicated than hashword() only because
|
178
|
+
hashlittle() has to dance around fitting the key bytes into registers.
|
179
|
+
--------------------------------------------------------------------
|
180
|
+
*/
|
181
|
+
uint32_t hashword(
|
182
|
+
const uint32_t *k, /* the key, an array of uint32_t values */
|
183
|
+
size_t length, /* the length of the key, in uint32_ts */
|
184
|
+
uint32_t initval) /* the previous hash, or an arbitrary value */
|
185
|
+
{
|
186
|
+
uint32_t a,b,c;
|
187
|
+
|
188
|
+
/* Set up the internal state */
|
189
|
+
a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
|
190
|
+
|
191
|
+
/*------------------------------------------------- handle most of the key */
|
192
|
+
while (length > 3)
|
193
|
+
{
|
194
|
+
a += k[0];
|
195
|
+
b += k[1];
|
196
|
+
c += k[2];
|
197
|
+
mix(a,b,c);
|
198
|
+
length -= 3;
|
199
|
+
k += 3;
|
200
|
+
}
|
201
|
+
|
202
|
+
/*------------------------------------------- handle the last 3 uint32_t's */
|
203
|
+
switch(length) /* all the case statements fall through */
|
204
|
+
{
|
205
|
+
case 3 : c+=k[2];
|
206
|
+
case 2 : b+=k[1];
|
207
|
+
case 1 : a+=k[0];
|
208
|
+
final(a,b,c);
|
209
|
+
case 0: /* case 0: nothing left to add */
|
210
|
+
break;
|
211
|
+
}
|
212
|
+
/*------------------------------------------------------ report the result */
|
213
|
+
return c;
|
214
|
+
}
|
215
|
+
|
216
|
+
|
217
|
+
/*
|
218
|
+
--------------------------------------------------------------------
|
219
|
+
hashword2() -- same as hashword(), but take two seeds and return two
|
220
|
+
32-bit values. pc and pb must both be nonnull, and *pc and *pb must
|
221
|
+
both be initialized with seeds. If you pass in (*pb)==0, the output
|
222
|
+
(*pc) will be the same as the return value from hashword().
|
223
|
+
--------------------------------------------------------------------
|
224
|
+
*/
|
225
|
+
void hashword2 (
|
226
|
+
const uint32_t *k, /* the key, an array of uint32_t values */
|
227
|
+
size_t length, /* the length of the key, in uint32_ts */
|
228
|
+
uint32_t *pc, /* IN: seed OUT: primary hash value */
|
229
|
+
uint32_t *pb) /* IN: more seed OUT: secondary hash value */
|
230
|
+
{
|
231
|
+
uint32_t a,b,c;
|
232
|
+
|
233
|
+
/* Set up the internal state */
|
234
|
+
a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
|
235
|
+
c += *pb;
|
236
|
+
|
237
|
+
/*------------------------------------------------- handle most of the key */
|
238
|
+
while (length > 3)
|
239
|
+
{
|
240
|
+
a += k[0];
|
241
|
+
b += k[1];
|
242
|
+
c += k[2];
|
243
|
+
mix(a,b,c);
|
244
|
+
length -= 3;
|
245
|
+
k += 3;
|
246
|
+
}
|
247
|
+
|
248
|
+
/*------------------------------------------- handle the last 3 uint32_t's */
|
249
|
+
switch(length) /* all the case statements fall through */
|
250
|
+
{
|
251
|
+
case 3 : c+=k[2];
|
252
|
+
case 2 : b+=k[1];
|
253
|
+
case 1 : a+=k[0];
|
254
|
+
final(a,b,c);
|
255
|
+
case 0: /* case 0: nothing left to add */
|
256
|
+
break;
|
257
|
+
}
|
258
|
+
/*------------------------------------------------------ report the result */
|
259
|
+
*pc=c; *pb=b;
|
260
|
+
}
|
261
|
+
|
262
|
+
|
263
|
+
/*
|
264
|
+
-------------------------------------------------------------------------------
|
265
|
+
hashlittle() -- hash a variable-length key into a 32-bit value
|
266
|
+
k : the key (the unaligned variable-length array of bytes)
|
267
|
+
length : the length of the key, counting by bytes
|
268
|
+
initval : can be any 4-byte value
|
269
|
+
Returns a 32-bit value. Every bit of the key affects every bit of
|
270
|
+
the return value. Two keys differing by one or two bits will have
|
271
|
+
totally different hash values.
|
272
|
+
|
273
|
+
The best hash table sizes are powers of 2. There is no need to do
|
274
|
+
mod a prime (mod is sooo slow!). If you need less than 32 bits,
|
275
|
+
use a bitmask. For example, if you need only 10 bits, do
|
276
|
+
h = (h & hashmask(10));
|
277
|
+
In which case, the hash table should have hashsize(10) elements.
|
278
|
+
|
279
|
+
If you are hashing n strings (uint8_t **)k, do it like this:
|
280
|
+
for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
|
281
|
+
|
282
|
+
By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
|
283
|
+
code any way you wish, private, educational, or commercial. It's free.
|
284
|
+
|
285
|
+
Use for hash table lookup, or anything where one collision in 2^^32 is
|
286
|
+
acceptable. Do NOT use for cryptographic purposes.
|
287
|
+
-------------------------------------------------------------------------------
|
288
|
+
*/
|
289
|
+
|
290
|
+
uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
|
291
|
+
{
|
292
|
+
uint32_t a,b,c; /* internal state */
|
293
|
+
union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
|
294
|
+
|
295
|
+
/* Set up the internal state */
|
296
|
+
a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
|
297
|
+
|
298
|
+
u.ptr = key;
|
299
|
+
if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
|
300
|
+
const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
|
301
|
+
|
302
|
+
/*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
|
303
|
+
while (length > 12)
|
304
|
+
{
|
305
|
+
a += k[0];
|
306
|
+
b += k[1];
|
307
|
+
c += k[2];
|
308
|
+
mix(a,b,c);
|
309
|
+
length -= 12;
|
310
|
+
k += 3;
|
311
|
+
}
|
312
|
+
|
313
|
+
/*----------------------------- handle the last (probably partial) block */
|
314
|
+
/*
|
315
|
+
* "k[2]&0xffffff" actually reads beyond the end of the string, but
|
316
|
+
* then masks off the part it's not allowed to read. Because the
|
317
|
+
* string is aligned, the masked-off tail is in the same word as the
|
318
|
+
* rest of the string. Every machine with memory protection I've seen
|
319
|
+
* does it on word boundaries, so is OK with this. But VALGRIND will
|
320
|
+
* still catch it and complain. The masking trick does make the hash
|
321
|
+
* noticably faster for short strings (like English words).
|
322
|
+
*/
|
323
|
+
#ifndef VALGRIND
|
324
|
+
|
325
|
+
switch(length)
|
326
|
+
{
|
327
|
+
case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
|
328
|
+
case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
|
329
|
+
case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
|
330
|
+
case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
|
331
|
+
case 8 : b+=k[1]; a+=k[0]; break;
|
332
|
+
case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
|
333
|
+
case 6 : b+=k[1]&0xffff; a+=k[0]; break;
|
334
|
+
case 5 : b+=k[1]&0xff; a+=k[0]; break;
|
335
|
+
case 4 : a+=k[0]; break;
|
336
|
+
case 3 : a+=k[0]&0xffffff; break;
|
337
|
+
case 2 : a+=k[0]&0xffff; break;
|
338
|
+
case 1 : a+=k[0]&0xff; break;
|
339
|
+
case 0 : return c; /* zero length strings require no mixing */
|
340
|
+
}
|
341
|
+
|
342
|
+
#else /* make valgrind happy */
|
343
|
+
|
344
|
+
k8 = (const uint8_t *)k;
|
345
|
+
switch(length)
|
346
|
+
{
|
347
|
+
case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
|
348
|
+
case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
|
349
|
+
case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
|
350
|
+
case 9 : c+=k8[8]; /* fall through */
|
351
|
+
case 8 : b+=k[1]; a+=k[0]; break;
|
352
|
+
case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
|
353
|
+
case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
|
354
|
+
case 5 : b+=k8[4]; /* fall through */
|
355
|
+
case 4 : a+=k[0]; break;
|
356
|
+
case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
|
357
|
+
case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
|
358
|
+
case 1 : a+=k8[0]; break;
|
359
|
+
case 0 : return c;
|
360
|
+
}
|
361
|
+
|
362
|
+
#endif /* !valgrind */
|
363
|
+
|
364
|
+
} else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
|
365
|
+
const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
|
366
|
+
const uint8_t *k8;
|
367
|
+
|
368
|
+
/*--------------- all but last block: aligned reads and different mixing */
|
369
|
+
while (length > 12)
|
370
|
+
{
|
371
|
+
a += k[0] + (((uint32_t)k[1])<<16);
|
372
|
+
b += k[2] + (((uint32_t)k[3])<<16);
|
373
|
+
c += k[4] + (((uint32_t)k[5])<<16);
|
374
|
+
mix(a,b,c);
|
375
|
+
length -= 12;
|
376
|
+
k += 6;
|
377
|
+
}
|
378
|
+
|
379
|
+
/*----------------------------- handle the last (probably partial) block */
|
380
|
+
k8 = (const uint8_t *)k;
|
381
|
+
switch(length)
|
382
|
+
{
|
383
|
+
case 12: c+=k[4]+(((uint32_t)k[5])<<16);
|
384
|
+
b+=k[2]+(((uint32_t)k[3])<<16);
|
385
|
+
a+=k[0]+(((uint32_t)k[1])<<16);
|
386
|
+
break;
|
387
|
+
case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
|
388
|
+
case 10: c+=k[4];
|
389
|
+
b+=k[2]+(((uint32_t)k[3])<<16);
|
390
|
+
a+=k[0]+(((uint32_t)k[1])<<16);
|
391
|
+
break;
|
392
|
+
case 9 : c+=k8[8]; /* fall through */
|
393
|
+
case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
|
394
|
+
a+=k[0]+(((uint32_t)k[1])<<16);
|
395
|
+
break;
|
396
|
+
case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
|
397
|
+
case 6 : b+=k[2];
|
398
|
+
a+=k[0]+(((uint32_t)k[1])<<16);
|
399
|
+
break;
|
400
|
+
case 5 : b+=k8[4]; /* fall through */
|
401
|
+
case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
|
402
|
+
break;
|
403
|
+
case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
|
404
|
+
case 2 : a+=k[0];
|
405
|
+
break;
|
406
|
+
case 1 : a+=k8[0];
|
407
|
+
break;
|
408
|
+
case 0 : return c; /* zero length requires no mixing */
|
409
|
+
}
|
410
|
+
|
411
|
+
} else { /* need to read the key one byte at a time */
|
412
|
+
const uint8_t *k = (const uint8_t *)key;
|
413
|
+
|
414
|
+
/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
|
415
|
+
while (length > 12)
|
416
|
+
{
|
417
|
+
a += k[0];
|
418
|
+
a += ((uint32_t)k[1])<<8;
|
419
|
+
a += ((uint32_t)k[2])<<16;
|
420
|
+
a += ((uint32_t)k[3])<<24;
|
421
|
+
b += k[4];
|
422
|
+
b += ((uint32_t)k[5])<<8;
|
423
|
+
b += ((uint32_t)k[6])<<16;
|
424
|
+
b += ((uint32_t)k[7])<<24;
|
425
|
+
c += k[8];
|
426
|
+
c += ((uint32_t)k[9])<<8;
|
427
|
+
c += ((uint32_t)k[10])<<16;
|
428
|
+
c += ((uint32_t)k[11])<<24;
|
429
|
+
mix(a,b,c);
|
430
|
+
length -= 12;
|
431
|
+
k += 12;
|
432
|
+
}
|
433
|
+
|
434
|
+
/*-------------------------------- last block: affect all 32 bits of (c) */
|
435
|
+
switch(length) /* all the case statements fall through */
|
436
|
+
{
|
437
|
+
case 12: c+=((uint32_t)k[11])<<24;
|
438
|
+
case 11: c+=((uint32_t)k[10])<<16;
|
439
|
+
case 10: c+=((uint32_t)k[9])<<8;
|
440
|
+
case 9 : c+=k[8];
|
441
|
+
case 8 : b+=((uint32_t)k[7])<<24;
|
442
|
+
case 7 : b+=((uint32_t)k[6])<<16;
|
443
|
+
case 6 : b+=((uint32_t)k[5])<<8;
|
444
|
+
case 5 : b+=k[4];
|
445
|
+
case 4 : a+=((uint32_t)k[3])<<24;
|
446
|
+
case 3 : a+=((uint32_t)k[2])<<16;
|
447
|
+
case 2 : a+=((uint32_t)k[1])<<8;
|
448
|
+
case 1 : a+=k[0];
|
449
|
+
break;
|
450
|
+
case 0 : return c;
|
451
|
+
}
|
452
|
+
}
|
453
|
+
|
454
|
+
final(a,b,c);
|
455
|
+
return c;
|
456
|
+
}
|
457
|
+
|
458
|
+
|
459
|
+
/*
|
460
|
+
* hashlittle2: return 2 32-bit hash values
|
461
|
+
*
|
462
|
+
* This is identical to hashlittle(), except it returns two 32-bit hash
|
463
|
+
* values instead of just one. This is good enough for hash table
|
464
|
+
* lookup with 2^^64 buckets, or if you want a second hash if you're not
|
465
|
+
* happy with the first, or if you want a probably-unique 64-bit ID for
|
466
|
+
* the key. *pc is better mixed than *pb, so use *pc first. If you want
|
467
|
+
* a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
|
468
|
+
*/
|
469
|
+
void hashlittle2(
|
470
|
+
const void *key, /* the key to hash */
|
471
|
+
size_t length, /* length of the key */
|
472
|
+
uint32_t *pc, /* IN: primary initval, OUT: primary hash */
|
473
|
+
uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */
|
474
|
+
{
|
475
|
+
uint32_t a,b,c; /* internal state */
|
476
|
+
union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
|
477
|
+
|
478
|
+
/* Set up the internal state */
|
479
|
+
a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
|
480
|
+
c += *pb;
|
481
|
+
|
482
|
+
u.ptr = key;
|
483
|
+
if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
|
484
|
+
const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
|
485
|
+
|
486
|
+
/*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
|
487
|
+
while (length > 12)
|
488
|
+
{
|
489
|
+
a += k[0];
|
490
|
+
b += k[1];
|
491
|
+
c += k[2];
|
492
|
+
mix(a,b,c);
|
493
|
+
length -= 12;
|
494
|
+
k += 3;
|
495
|
+
}
|
496
|
+
|
497
|
+
/*----------------------------- handle the last (probably partial) block */
|
498
|
+
/*
|
499
|
+
* "k[2]&0xffffff" actually reads beyond the end of the string, but
|
500
|
+
* then masks off the part it's not allowed to read. Because the
|
501
|
+
* string is aligned, the masked-off tail is in the same word as the
|
502
|
+
* rest of the string. Every machine with memory protection I've seen
|
503
|
+
* does it on word boundaries, so is OK with this. But VALGRIND will
|
504
|
+
* still catch it and complain. The masking trick does make the hash
|
505
|
+
* noticably faster for short strings (like English words).
|
506
|
+
*/
|
507
|
+
|
508
|
+
switch(length)
|
509
|
+
{
|
510
|
+
case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
|
511
|
+
case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
|
512
|
+
case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
|
513
|
+
case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
|
514
|
+
case 8 : b+=k[1]; a+=k[0]; break;
|
515
|
+
case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
|
516
|
+
case 6 : b+=k[1]&0xffff; a+=k[0]; break;
|
517
|
+
case 5 : b+=k[1]&0xff; a+=k[0]; break;
|
518
|
+
case 4 : a+=k[0]; break;
|
519
|
+
case 3 : a+=k[0]&0xffffff; break;
|
520
|
+
case 2 : a+=k[0]&0xffff; break;
|
521
|
+
case 1 : a+=k[0]&0xff; break;
|
522
|
+
case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
|
523
|
+
}
|
524
|
+
|
525
|
+
} else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
|
526
|
+
const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
|
527
|
+
const uint8_t *k8;
|
528
|
+
|
529
|
+
/*--------------- all but last block: aligned reads and different mixing */
|
530
|
+
while (length > 12)
|
531
|
+
{
|
532
|
+
a += k[0] + (((uint32_t)k[1])<<16);
|
533
|
+
b += k[2] + (((uint32_t)k[3])<<16);
|
534
|
+
c += k[4] + (((uint32_t)k[5])<<16);
|
535
|
+
mix(a,b,c);
|
536
|
+
length -= 12;
|
537
|
+
k += 6;
|
538
|
+
}
|
539
|
+
|
540
|
+
/*----------------------------- handle the last (probably partial) block */
|
541
|
+
k8 = (const uint8_t *)k;
|
542
|
+
switch(length)
|
543
|
+
{
|
544
|
+
case 12: c+=k[4]+(((uint32_t)k[5])<<16);
|
545
|
+
b+=k[2]+(((uint32_t)k[3])<<16);
|
546
|
+
a+=k[0]+(((uint32_t)k[1])<<16);
|
547
|
+
break;
|
548
|
+
case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
|
549
|
+
case 10: c+=k[4];
|
550
|
+
b+=k[2]+(((uint32_t)k[3])<<16);
|
551
|
+
a+=k[0]+(((uint32_t)k[1])<<16);
|
552
|
+
break;
|
553
|
+
case 9 : c+=k8[8]; /* fall through */
|
554
|
+
case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
|
555
|
+
a+=k[0]+(((uint32_t)k[1])<<16);
|
556
|
+
break;
|
557
|
+
case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
|
558
|
+
case 6 : b+=k[2];
|
559
|
+
a+=k[0]+(((uint32_t)k[1])<<16);
|
560
|
+
break;
|
561
|
+
case 5 : b+=k8[4]; /* fall through */
|
562
|
+
case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
|
563
|
+
break;
|
564
|
+
case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
|
565
|
+
case 2 : a+=k[0];
|
566
|
+
break;
|
567
|
+
case 1 : a+=k8[0];
|
568
|
+
break;
|
569
|
+
case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
|
570
|
+
}
|
571
|
+
|
572
|
+
} else { /* need to read the key one byte at a time */
|
573
|
+
const uint8_t *k = (const uint8_t *)key;
|
574
|
+
|
575
|
+
/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
|
576
|
+
while (length > 12)
|
577
|
+
{
|
578
|
+
a += k[0];
|
579
|
+
a += ((uint32_t)k[1])<<8;
|
580
|
+
a += ((uint32_t)k[2])<<16;
|
581
|
+
a += ((uint32_t)k[3])<<24;
|
582
|
+
b += k[4];
|
583
|
+
b += ((uint32_t)k[5])<<8;
|
584
|
+
b += ((uint32_t)k[6])<<16;
|
585
|
+
b += ((uint32_t)k[7])<<24;
|
586
|
+
c += k[8];
|
587
|
+
c += ((uint32_t)k[9])<<8;
|
588
|
+
c += ((uint32_t)k[10])<<16;
|
589
|
+
c += ((uint32_t)k[11])<<24;
|
590
|
+
mix(a,b,c);
|
591
|
+
length -= 12;
|
592
|
+
k += 12;
|
593
|
+
}
|
594
|
+
|
595
|
+
/*-------------------------------- last block: affect all 32 bits of (c) */
|
596
|
+
switch(length) /* all the case statements fall through */
|
597
|
+
{
|
598
|
+
case 12: c+=((uint32_t)k[11])<<24;
|
599
|
+
case 11: c+=((uint32_t)k[10])<<16;
|
600
|
+
case 10: c+=((uint32_t)k[9])<<8;
|
601
|
+
case 9 : c+=k[8];
|
602
|
+
case 8 : b+=((uint32_t)k[7])<<24;
|
603
|
+
case 7 : b+=((uint32_t)k[6])<<16;
|
604
|
+
case 6 : b+=((uint32_t)k[5])<<8;
|
605
|
+
case 5 : b+=k[4];
|
606
|
+
case 4 : a+=((uint32_t)k[3])<<24;
|
607
|
+
case 3 : a+=((uint32_t)k[2])<<16;
|
608
|
+
case 2 : a+=((uint32_t)k[1])<<8;
|
609
|
+
case 1 : a+=k[0];
|
610
|
+
break;
|
611
|
+
case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
|
612
|
+
}
|
613
|
+
}
|
614
|
+
|
615
|
+
final(a,b,c);
|
616
|
+
*pc=c; *pb=b;
|
617
|
+
}
|
618
|
+
|
619
|
+
|
620
|
+
|
621
|
+
/*
|
622
|
+
* hashbig():
|
623
|
+
* This is the same as hashword() on big-endian machines. It is different
|
624
|
+
* from hashlittle() on all machines. hashbig() takes advantage of
|
625
|
+
* big-endian byte ordering.
|
626
|
+
*/
|
627
|
+
uint32_t hashbig( const void *key, size_t length, uint32_t initval)
|
628
|
+
{
|
629
|
+
uint32_t a,b,c;
|
630
|
+
union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
|
631
|
+
|
632
|
+
/* Set up the internal state */
|
633
|
+
a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
|
634
|
+
|
635
|
+
u.ptr = key;
|
636
|
+
if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
|
637
|
+
const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
|
638
|
+
|
639
|
+
/*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
|
640
|
+
while (length > 12)
|
641
|
+
{
|
642
|
+
a += k[0];
|
643
|
+
b += k[1];
|
644
|
+
c += k[2];
|
645
|
+
mix(a,b,c);
|
646
|
+
length -= 12;
|
647
|
+
k += 3;
|
648
|
+
}
|
649
|
+
|
650
|
+
/*----------------------------- handle the last (probably partial) block */
|
651
|
+
/*
|
652
|
+
* "k[2]<<8" actually reads beyond the end of the string, but
|
653
|
+
* then shifts out the part it's not allowed to read. Because the
|
654
|
+
* string is aligned, the illegal read is in the same word as the
|
655
|
+
* rest of the string. Every machine with memory protection I've seen
|
656
|
+
* does it on word boundaries, so is OK with this. But VALGRIND will
|
657
|
+
* still catch it and complain. The masking trick does make the hash
|
658
|
+
* noticably faster for short strings (like English words).
|
659
|
+
*/
|
660
|
+
#ifndef VALGRIND
|
661
|
+
|
662
|
+
switch(length)
|
663
|
+
{
|
664
|
+
case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
|
665
|
+
case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
|
666
|
+
case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
|
667
|
+
case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
|
668
|
+
case 8 : b+=k[1]; a+=k[0]; break;
|
669
|
+
case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
|
670
|
+
case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
|
671
|
+
case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
|
672
|
+
case 4 : a+=k[0]; break;
|
673
|
+
case 3 : a+=k[0]&0xffffff00; break;
|
674
|
+
case 2 : a+=k[0]&0xffff0000; break;
|
675
|
+
case 1 : a+=k[0]&0xff000000; break;
|
676
|
+
case 0 : return c; /* zero length strings require no mixing */
|
677
|
+
}
|
678
|
+
|
679
|
+
#else /* make valgrind happy */
|
680
|
+
|
681
|
+
k8 = (const uint8_t *)k;
|
682
|
+
switch(length) /* all the case statements fall through */
|
683
|
+
{
|
684
|
+
case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
|
685
|
+
case 11: c+=((uint32_t)k8[10])<<8; /* fall through */
|
686
|
+
case 10: c+=((uint32_t)k8[9])<<16; /* fall through */
|
687
|
+
case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */
|
688
|
+
case 8 : b+=k[1]; a+=k[0]; break;
|
689
|
+
case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */
|
690
|
+
case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */
|
691
|
+
case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */
|
692
|
+
case 4 : a+=k[0]; break;
|
693
|
+
case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */
|
694
|
+
case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */
|
695
|
+
case 1 : a+=((uint32_t)k8[0])<<24; break;
|
696
|
+
case 0 : return c;
|
697
|
+
}
|
698
|
+
|
699
|
+
#endif /* !VALGRIND */
|
700
|
+
|
701
|
+
} else { /* need to read the key one byte at a time */
|
702
|
+
const uint8_t *k = (const uint8_t *)key;
|
703
|
+
|
704
|
+
/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
|
705
|
+
while (length > 12)
|
706
|
+
{
|
707
|
+
a += ((uint32_t)k[0])<<24;
|
708
|
+
a += ((uint32_t)k[1])<<16;
|
709
|
+
a += ((uint32_t)k[2])<<8;
|
710
|
+
a += ((uint32_t)k[3]);
|
711
|
+
b += ((uint32_t)k[4])<<24;
|
712
|
+
b += ((uint32_t)k[5])<<16;
|
713
|
+
b += ((uint32_t)k[6])<<8;
|
714
|
+
b += ((uint32_t)k[7]);
|
715
|
+
c += ((uint32_t)k[8])<<24;
|
716
|
+
c += ((uint32_t)k[9])<<16;
|
717
|
+
c += ((uint32_t)k[10])<<8;
|
718
|
+
c += ((uint32_t)k[11]);
|
719
|
+
mix(a,b,c);
|
720
|
+
length -= 12;
|
721
|
+
k += 12;
|
722
|
+
}
|
723
|
+
|
724
|
+
/*-------------------------------- last block: affect all 32 bits of (c) */
|
725
|
+
switch(length) /* all the case statements fall through */
|
726
|
+
{
|
727
|
+
case 12: c+=k[11];
|
728
|
+
case 11: c+=((uint32_t)k[10])<<8;
|
729
|
+
case 10: c+=((uint32_t)k[9])<<16;
|
730
|
+
case 9 : c+=((uint32_t)k[8])<<24;
|
731
|
+
case 8 : b+=k[7];
|
732
|
+
case 7 : b+=((uint32_t)k[6])<<8;
|
733
|
+
case 6 : b+=((uint32_t)k[5])<<16;
|
734
|
+
case 5 : b+=((uint32_t)k[4])<<24;
|
735
|
+
case 4 : a+=k[3];
|
736
|
+
case 3 : a+=((uint32_t)k[2])<<8;
|
737
|
+
case 2 : a+=((uint32_t)k[1])<<16;
|
738
|
+
case 1 : a+=((uint32_t)k[0])<<24;
|
739
|
+
break;
|
740
|
+
case 0 : return c;
|
741
|
+
}
|
742
|
+
}
|
743
|
+
|
744
|
+
final(a,b,c);
|
745
|
+
return c;
|
746
|
+
}
|
747
|
+
|
data/lib/hashlittle.rb
ADDED
data/sig/hashlittle.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashlittle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrey "Zed" Zaikin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-03-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- zed.0xff@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions:
|
18
|
+
- ext/hashlittle/extconf.rb
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- CODE_OF_CONDUCT.md
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- ext/hashlittle/extconf.rb
|
28
|
+
- ext/hashlittle/hashlittle.c
|
29
|
+
- ext/hashlittle/hashlittle.h
|
30
|
+
- ext/hashlittle/lookup.h
|
31
|
+
- ext/hashlittle/lookup3.c
|
32
|
+
- lib/hashlittle.rb
|
33
|
+
- lib/hashlittle/version.rb
|
34
|
+
- sig/hashlittle.rbs
|
35
|
+
homepage: https://github.com/zed-0xff/hashlittle
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata:
|
39
|
+
homepage_uri: https://github.com/zed-0xff/hashlittle
|
40
|
+
source_code_uri: https://github.com/zed-0xff/hashlittle
|
41
|
+
rubygems_mfa_required: 'true'
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 3.0.0
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubygems_version: 3.5.6
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Bob Jenkins' hashlittle and hashlittle2 non-cryptographic hash functions.
|
61
|
+
test_files: []
|