From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.236.62.231 with SMTP id y67mr221883yhc.3.1383175899257; Wed, 30 Oct 2013 16:31:39 -0700 (PDT) X-Received: by 10.49.71.230 with SMTP id y6mr2972qeu.17.1383175899239; Wed, 30 Oct 2013 16:31:39 -0700 (PDT) Path: border1.nntp.dca3.giganews.com!backlog3.nntp.dca3.giganews.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!o2no10968935qas.0!news-out.google.com!9ni2008qaf.0!nntp.google.com!i2no1980626qav.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 30 Oct 2013 16:31:39 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=206.53.78.59; posting-account=ShYTIAoAAABytvcS76ZrG9GdaV-nXYKy NNTP-Posting-Host: 206.53.78.59 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: FNV-1 From: sbelmont700@gmail.com Injection-Date: Wed, 30 Oct 2013 23:31:39 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Original-Bytes: 5819 Xref: number.nntp.dca.giganews.com comp.lang.ada:183759 Date: 2013-10-30T16:31:39-07:00 List-Id: Hi, I did up the following implementation of the FNV1 and FNV1a hashes after ne= eding a decent cross-platform, compiler-independent way to hash things (64-= bit pointers, specifically), but it ought to be good for other things as we= ll. I haven't done any extensive testing, but it seems to suit my needs; p= erhaps it will suit yours as well. Any suggestions are welcome, especially= tips on better ways to use static expressions, or any cross-platform 'gotc= has'. The code is public domain. -sb -- Fowler/Noll/Vo hash functions with Ada.Containers; package FNV is -- FNV-1 Hash generic type T is private; function FNV1 (Item : T) return Ada.Containers.Hash_Type; -- FNV-1a Alternative Hash generic type T is private; function FNV1a (Item : T) return Ada.Containers.Hash_Type; end FNV; pragma Assertion_Policy (Check); with Ada.Storage_IO; package body FNV is subtype FNV_Hash_Type is Ada.Containers.Hash_Type; use type FNV_Hash_Type; -- Prime Values Prime_32 : constant :=3D 2**24 + 2**8 + 16#93#; Prime_64 : constant :=3D 2**40 + 2**8 + 16#b3#; Prime_128 : constant :=3D 2**88 + 2**8 + 16#3b#; Prime_256 : constant :=3D 2**168 + 2**8 + 16#63#; Prime_512 : constant :=3D 2**344 + 2**8 + 16#57#; Prime_1024 : constant :=3D 2**680 + 2**8 + 16#8d#; subtype FNV_Prime_Type is FNV_Hash_Type range 1 .. FNV_Hash_Type'Last; K_Prime : constant :=3D (case FNV_Prime_Type'Size is when 32 =3D> Prime_32, when 64 =3D> Prime_64, when 128 =3D> Prime_128, when 256 =3D> Prime_256, when 512 =3D> Prime_512, when 1024 =3D> Prime_1024, when others =3D> 0); Prime : constant FNV_Prime_Type :=3D K_Prime; -- Start Offset Values Offset_32 : constant :=3D 2166136261; Offset_64 : constant :=3D 14695981039346656037; Offset_128 : constant :=3D 144066263297769815596495629667062367629; Offset_256 : constant :=3D 10002925795805258090707096862062570483709279= 6014241193945225284501741471925557; Offset_512 : constant :=3D 96593031294966694980094354007163104660904187= 456726378961083743294344626579945829321977164384498130518922065398057844953= 28239340083876191928701583869517785; Offset_1024 : constant :=3D 14197795064947621068722070641403218320880622= 795441933960878474914617582723252296732303717722150864096521202355549365628= 174669108571814760471015076148029755969804077320157692458563003215304957150= 157403644460363550505412711285966361610267868082893823963790439336411086884= 584107735010676915; subtype FNV_Offset_Type is FNV_Hash_Type range 1 .. FNV_Hash_Type'Last; K_Offset : constant :=3D (case FNV_Offset_Type'Size is when 32 =3D> Offset_32, when 64 =3D> Offset_64, when 128 =3D> Offset_128, when 256 =3D> Offset_256, when 512 =3D> Offset_512, when 1024 =3D> Offset_1024, when others =3D> 0); Offset : constant FNV_Offset_Type :=3D K_Offset; ------------------------------------------------------------------------= ----- -- FNV1 Hash Function ------------------------------------------------------------------------= ----- function FNV1 (Item : T) return Ada.Containers.Hash_Type is package Data_Buffers is new Ada.Storage_IO (Element_Type =3D> T); Buffer : Data_Buffers.Buffer_Type; begin Data_Buffers.Write (Buffer =3D> Buffer, Item =3D> Item); return Hash : FNV_Hash_Type :=3D Offset do for Byte of Buffer loop Hash :=3D Hash * Prime; Hash :=3D Hash xor FNV_Hash_Type(Byte); end loop; end return; end FNV1; ------------------------------------------------------------------------= ----- -- FNV1a Hash Function ------------------------------------------------------------------------= ----- function FNV1a (Item : T) return Ada.Containers.Hash_Type is package Data_Buffers is new Ada.Storage_IO (Element_Type =3D> T); Buffer : Data_Buffers.Buffer_Type; begin Data_Buffers.Write (Buffer =3D> Buffer, Item =3D> Item); return Hash : FNV_Hash_Type :=3D Offset do for Byte of Buffer loop Hash :=3D Hash xor FNV_Hash_Type(Byte); Hash :=3D Hash * Prime; end loop; end return; end FNV1a; end FNV;