{"id":417,"date":"2021-05-13T09:12:31","date_gmt":"2021-05-13T09:12:31","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=417"},"modified":"2021-05-13T09:12:31","modified_gmt":"2021-05-13T09:12:31","slug":"fnv1a32fast-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2021\/05\/13\/fnv1a32fast-cs\/","title":{"rendered":"FNV1a32Fast.cs"},"content":{"rendered":"\n<p>FNV1a 32 Bit Fast Hashing<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System.Security.Cryptography;\n\/\/\/ &lt;summary>\n\/\/\/     https:\/\/en.wikipedia.org\/wiki\/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function\n\/\/\/     Very fast 2GBpS.\n\/\/\/ &lt;\/summary>\npublic class FNV1a32Fast : HashAlgorithm\n{\n    private const    ulong _prime = 0x100000001B3;\n    private          ulong _hash;\n    private readonly ulong _offset = 0xCBF29CE484222325;\n    public           uint  Hashf;\n    public FNV1a32Fast()\n    {\n        _hash = _offset;\n    }\n    public FNV1a32Fast(ulong seed)\n    {\n        _offset = seed;\n        _hash   = _offset;\n    }\n    public override int HashSize => 32;\n    public override void Initialize()\n    {\n        _hash = _offset;\n    }\n    protected override void HashCore(byte[] bytes, int ibStart, int cbSize)\n    {\n        Hash64(bytes, ibStart, cbSize);\n    }\n    private unsafe void Hash64(byte[] bytes, int ibStart, int cbSize)\n    {\n        if (bytes == null)\n            return;\n        if (ibStart >= bytes.Length || cbSize > bytes.Length)\n            return;\n        unchecked\n        {\n            fixed (byte* pb = bytes)\n            {\n                var nb = pb + ibStart;\n                while (cbSize >= 8)\n                {\n                    _hash  ^= *(ulong*) nb;\n                    _hash  *= _prime;\n                    nb     += 8;\n                    cbSize -= 8;\n                }\n                switch (cbSize &amp; 7)\n                {\n                    case 7:\n                        _hash ^= *(ulong*) (nb + 6);\n                        _hash *= _prime;\n                        goto case 6;\n                    case 6:\n                        _hash ^= *(ulong*) (nb + 5);\n                        _hash *= _prime;\n                        goto case 5;\n                    case 5:\n                        _hash ^= *(ulong*) (nb + 4);\n                        _hash *= _prime;\n                        goto case 4;\n                    case 4:\n                        _hash ^= *(ulong*) (nb + 3);\n                        _hash *= _prime;\n                        goto case 3;\n                    case 3:\n                        _hash ^= *(ulong*) (nb + 2);\n                        _hash *= _prime;\n                        goto case 2;\n                    case 2:\n                        _hash ^= *(ulong*) (nb + 1);\n                        _hash *= _prime;\n                        goto case 1;\n                    case 1:\n                        _hash ^= *nb;\n                        _hash *= _prime;\n                        break;\n                }\n            }\n        }\n        var usb = (uint) (_hash >> 32);\n        var lsb = (uint) _hash;\n        Hashf = usb ^ lsb;\n    }\n    protected override byte[] HashFinal()\n    {\n        var ba = new byte[4];\n        unsafe\n        {\n            fixed (byte* ptr = ba)\n            {\n                *(uint*) ptr = Hashf;\n            }\n        }\n        return ba;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>FNV1a 32 Bit Fast Hashing<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[180,100,166,76],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/417"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=417"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":418,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/417\/revisions\/418"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}