libosmocore  1.10.0.2-2e788.202408152026
Osmocom core library
jhash.h
Go to the documentation of this file.
1 #pragma once
3 
4 /* Below is a partial copy of
5  * https://raw.githubusercontent.com/torvalds/linux/3eb3c33c1d87029a3832e205eebd59cfb56ba3a4/tools/include/linux/bitops.h
6  * with an osmo_ prefix applied to avoid any collisions.
7  */
8 /* SPDX-License-Identifier: GPL-2.0 */
14 static inline uint32_t osmo_rol32(uint32_t word, unsigned int shift)
15 {
16  return (word << shift) | (word >> ((-shift) & 31));
17 }
18 
19 /* Below is a partial copy of
20  * https://raw.githubusercontent.com/torvalds/linux/22c033989c3eb9731ad0c497dfab4231b8e367d6/include/linux/unaligned/packed_struct.h
21  * with an osmo_ prefix applied to avoid any collisions.
22  */
24  uint32_t x;
25 } __attribute__((__packed__));
26 
27 static inline uint32_t osmo_get_unaligned_cpu32(const void *p)
28 {
29  const struct osmo_unaligned_cpu32 *ptr = (const struct osmo_unaligned_cpu32 *)p;
30  return ptr->x;
31 }
32 
33 /* Below is a partial copy of
34  * https://raw.githubusercontent.com/torvalds/linux/79e3ea5aab48c83de9410e43b52895406847eca7/tools/include/linux/jhash.h
35  * with an osmo_ prefix applied to avoid any collisions.
36  */
37 /* jhash.h: Jenkins hash support.
38  *
39  * Copyright (C) 2006. Bob Jenkins (bob_jenkins@burtleburtle.net)
40  *
41  * https://burtleburtle.net/bob/hash/
42  *
43  * These are the credits from Bob's sources:
44  *
45  * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
46  *
47  * These are functions for producing 32-bit hashes for hash table lookup.
48  * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
49  * are externally useful functions. Routines to test the hash are included
50  * if SELF_TEST is defined. You can use this free for any purpose. It's in
51  * the public domain. It has no warranty.
52  *
53  * Copyright (C) 2009-2010 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
54  *
55  * I've modified Bob's hash to be useful in the Linux kernel, and
56  * any bugs present are my fault.
57  * Jozsef
58  */
59 
60 /* OSMO_JHASH_MIX -- mix 3 32-bit values reversibly. */
61 #define OSMO_JHASH_MIX(a, b, c) \
62 { \
63  a -= c; a ^= osmo_rol32(c, 4); c += b; \
64  b -= a; b ^= osmo_rol32(a, 6); a += c; \
65  c -= b; c ^= osmo_rol32(b, 8); b += a; \
66  a -= c; a ^= osmo_rol32(c, 16); c += b; \
67  b -= a; b ^= osmo_rol32(a, 19); a += c; \
68  c -= b; c ^= osmo_rol32(b, 4); b += a; \
69 }
70 
71 /* OSMO_JHASH_FINAL - final mixing of 3 32-bit values (a,b,c) into c */
72 #define OSMO_JHASH_FINAL(a, b, c) \
73 { \
74  c ^= b; c -= osmo_rol32(b, 14); \
75  a ^= c; a -= osmo_rol32(c, 11); \
76  b ^= a; b -= osmo_rol32(a, 25); \
77  c ^= b; c -= osmo_rol32(b, 16); \
78  a ^= c; a -= osmo_rol32(c, 4); \
79  b ^= a; b -= osmo_rol32(a, 14); \
80  c ^= b; c -= osmo_rol32(b, 24); \
81 }
82 
83 /* An arbitrary initial parameter */
84 #define JHASH_INITVAL 0xdeadbeef
85 
86 /* osmo_jhash - hash an arbitrary key
87  * @k: sequence of bytes as key
88  * @length: the length of the key
89  * @initval: the previous hash, or an arbitray value
90  *
91  * The generic version, hashes an arbitrary sequence of bytes.
92  * No alignment or length assumptions are made about the input key.
93  *
94  * Returns the hash value of the key. The result depends on endianness.
95  */
96 static inline uint32_t osmo_jhash(const void *key, uint32_t length, uint32_t initval)
97 {
98  uint32_t a, b, c;
99  const uint8_t *k = key;
100 
101  /* Set up the internal state */
102  a = b = c = JHASH_INITVAL + length + initval;
103 
104  /* All but the last block: affect some 32 bits of (a,b,c) */
105  while (length > 12) {
107  b += osmo_get_unaligned_cpu32(k + 4);
108  c += osmo_get_unaligned_cpu32(k + 8);
109  OSMO_JHASH_MIX(a, b, c);
110  length -= 12;
111  k += 12;
112  }
113  /* Last block: affect all 32 bits of (c) */
114  /* All the case statements fall through */
115  switch (length) {
116  case 12: c += (uint32_t)k[11]<<24;
117  case 11: c += (uint32_t)k[10]<<16;
118  case 10: c += (uint32_t)k[9]<<8;
119  case 9: c += k[8];
120  case 8: b += (uint32_t)k[7]<<24;
121  case 7: b += (uint32_t)k[6]<<16;
122  case 6: b += (uint32_t)k[5]<<8;
123  case 5: b += k[4];
124  case 4: a += (uint32_t)k[3]<<24;
125  case 3: a += (uint32_t)k[2]<<16;
126  case 2: a += (uint32_t)k[1]<<8;
127  case 1: a += k[0];
128  OSMO_JHASH_FINAL(a, b, c);
129  case 0: /* Nothing left to add */
130  break;
131  }
132 
133  return c;
134 }
135 
136 /* osmo_jhash2 - hash an array of uint32_t's
137  * @k: the key which must be an array of uint32_t's
138  * @length: the number of uint32_t's in the key
139  * @initval: the previous hash, or an arbitray value
140  *
141  * Returns the hash value of the key.
142  */
143 static inline uint32_t osmo_jhash2(const uint32_t *k, uint32_t length, uint32_t initval)
144 {
145  uint32_t a, b, c;
146 
147  /* Set up the internal state */
148  a = b = c = JHASH_INITVAL + (length<<2) + initval;
149 
150  /* Handle most of the key */
151  while (length > 3) {
152  a += k[0];
153  b += k[1];
154  c += k[2];
155  OSMO_JHASH_MIX(a, b, c);
156  length -= 3;
157  k += 3;
158  }
159 
160  /* Handle the last 3 uint32_t's: all the case statements fall through */
161  switch (length) {
162  case 3: c += k[2];
163  case 2: b += k[1];
164  case 1: a += k[0];
165  OSMO_JHASH_FINAL(a, b, c);
166  case 0: /* Nothing left to add */
167  break;
168  }
169 
170  return c;
171 }
uint8_t k[32]
uint8_t length
struct abis_rsl_common_hdr c
#define OSMO_JHASH_MIX(a, b, c)
Definition: jhash.h:61
static uint32_t osmo_jhash(const void *key, uint32_t length, uint32_t initval)
Definition: jhash.h:96
#define JHASH_INITVAL
Definition: jhash.h:84
static uint32_t osmo_rol32(uint32_t word, unsigned int shift)
rol32 - rotate a 32-bit value left @word: value to rotate @shift: bits to roll
Definition: jhash.h:14
static uint32_t osmo_jhash2(const uint32_t *k, uint32_t length, uint32_t initval)
Definition: jhash.h:143
static uint32_t osmo_get_unaligned_cpu32(const void *p)
Definition: jhash.h:27
struct osmo_unaligned_cpu32 __attribute__((__packed__))
#define OSMO_JHASH_FINAL(a, b, c)
Definition: jhash.h:72
Definition: jhash.h:23
uint32_t x
Definition: jhash.h:24