libgpr2_24.0.0_eda3c693/langkit/generated/src/gpr_parser_support-hashes.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
--
--  Copyright (C) 2014-2022, AdaCore
--  SPDX-License-Identifier: Apache-2.0
--

with Ada.Unchecked_Conversion;

with System.Storage_Elements;

with Interfaces;

package body Gpr_Parser_Support.Hashes is

   -------------
   -- Combine --
   -------------

   function Combine (L, R : Hash_Type) return Hash_Type is
      use Interfaces;

      --  The following is a translation from Boost's uint32_t hash function

      C1 : constant Unsigned_32 := 16#cc9e2d51#;
      C2 : constant Unsigned_32 := 16#1b873593#;
      H1 : Unsigned_32 := Unsigned_32 (L);
      K1 : Unsigned_32 := Unsigned_32 (R);
   begin
      K1 := K1 * C1;
      K1 := Rotate_Left (K1, 15);
      K1 := K1 * C2;

      H1 := H1 xor K1;
      H1 := Rotate_Left (H1, 13);
      H1 := H1 * 5 + 16#e6546b64#;

      return Hash_Type (H1);
   end Combine;

   -------------
   -- Combine --
   -------------

   function Combine (Hashes : Hash_Array) return Hash_Type is
      Result : Hash_Type := Initial_Hash;
   begin
      for H of Hashes loop
         Result := Combine (Result, H);
      end loop;
      return Result;
   end Combine;

   ------------------
   -- Hash_Address --
   ------------------

   function Hash_Address (Addr : System.Address) return Hash_Type is
      use System, System.Storage_Elements;

      Result : constant Integer_Address :=
         To_Integer (Addr) / (2 ** Ignored_LSB);
   begin
      return Hash_Type'Mod (Result);
   end Hash_Address;

   -----------------
   -- Hash_Access --
   -----------------

   function Hash_Access (Acc : Object_Access) return Hash_Type is
      use System;

      function Convert is new Ada.Unchecked_Conversion
        (Object_Access, System.Address);
      function Hash is new Hash_Address (Word_Size / Storage_Unit);

   begin
      return Hash (Convert (Acc));
   end Hash_Access;

end Gpr_Parser_Support.Hashes;