Skip to content

Instantly share code, notes, and snippets.

@ericbmerritt
Created March 15, 2012 17:07
Show Gist options
  • Save ericbmerritt/2045326 to your computer and use it in GitHub Desktop.
Save ericbmerritt/2045326 to your computer and use it in GitHub Desktop.
very simple fnv hash test
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers;
with Interfaces.C;
with Ada.Unchecked_Conversion;
procedure Hello is
function Amem_Hash(K1 : Interfaces.C.Unsigned_Long;
K2 : Interfaces.C.Unsigned_Long;
K3 : Interfaces.C.Unsigned_Long)
return Ada.Containers.Hash_Type is
type Byte is mod 2**8;
type Hash_Array is array(1..24) of byte;
type Unsigned_Long_Array is array(1..3) of Interfaces.C.Unsigned_Long;
type Local_Hash is mod 2**32;
function Convert is
new Ada.Unchecked_Conversion (Source => Unsigned_Long_Array,
Target => Hash_Array);
function Convert_Byte is
new Ada.Unchecked_Conversion (Source => Byte,
Target => Local_Hash);
Int_Cast : Unsigned_Long_Array;
Byte_Cast : Hash_Array;
-- FNV_Prime Value for 32 Bit Hashes
FNV_Prime : constant Local_Hash := 2166136261;
--- Offset Basis for 32 Bit Values
Offset_Basis : constant Local_Hash := 16777619;
Hash : Local_Hash := Offset_Basis;
begin
Int_Cast(1) := K1;
Int_Cast(2) := K2;
Int_Cast(3) := K3;
Byte_Cast := Convert(Int_Cast);
FNV_Loop :
for I in Byte_Cast'Range loop
Hash := (Hash * FNV_Prime) xor Convert_Byte(Byte_Cast(I));
end loop FNV_Loop;
return Ada.Containers.Hash_Type'Mod(Hash);
end Amem_Hash;
X : Ada.Containers.Hash_Type;
begin
X := Amem_Hash(1,2,3);
Ada.Text_IO.Put_Line (
Item => "The image of X is: " & Ada.Containers.Hash_Type'Image (X)) ;
end Hello;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment