{"id":419,"date":"2021-05-13T09:14:30","date_gmt":"2021-05-13T09:14:30","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=419"},"modified":"2021-05-13T09:14:30","modified_gmt":"2021-05-13T09:14:30","slug":"fnv1a64fast-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2021\/05\/13\/fnv1a64fast-cs\/","title":{"rendered":"Fnv1a64Fast.cs"},"content":{"rendered":"\n<p>FNV1a 64Bit Hashing Fast<\/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 up to 2GBpS.\n\/\/\/ &lt;\/summary>\npublic class Fnv1a64Fast : HashAlgorithm\n{\n    private const    ulong _prime  = 0x100000001B3;\n    private readonly ulong _offset = 0xCBF29CE484222325;\n    private          ulong _hash;\n    public           ulong Hashf;\n    public Fnv1a64Fast()\n    {\n        _hash = _offset;\n    }\n    public Fnv1a64Fast(ulong seed)\n    {\n        _offset = seed;\n        _hash   = _offset;\n    }\n    public override int HashSize => 64;\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        _hash = _hash ^ (_hash >> 32);\n        Hashf = _hash;\n    }\n    protected override byte[] HashFinal()\n    {\n        var ba = new byte[8];\n        unsafe\n        {\n            fixed (byte* ptr = ba)\n            {\n                *(ulong*) ptr = _hash;\n            }\n        }\n        return ba;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>FNV1a 64Bit Hashing Fast<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[181,100,166,76],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/419"}],"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=419"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/419\/revisions"}],"predecessor-version":[{"id":420,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/419\/revisions\/420"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}