ESOS32
ESOSOn32-bitProcessors
esos_utils.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2019 J. W. Bruce ("AUTHOR(S)")"
3  * All rights reserved.
4  * (J. W. Bruce, jwbruce_AT_tntech.edu, Tennessee Tech University)
5  *
6  * Permission to use, copy, modify, and distribute this software and its
7  * documentation for any purpose, without fee, and without written agreement is
8  * hereby granted, provided that the above copyright notice, the following
9  * two paragraphs and the authors appear in all copies of this software.
10  *
11  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
12  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
13  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
14  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15  *
16  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
19  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
20  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
21  *
22  * Please maintain this header in its entirety when copying/modifying
23  * these files.
24  *
25  *
26  */
27 
34 #include "esos.h"
35 #include "esos_utils.h"
36 
37 // ******** G L O B A L S ***************
38 uint32_t __u32_esos_PRNG_Seed;
39 uint32_t __esos_u32FNVHash = 2166136261uL;
40 
41 /****************************************************************
42 ** F U N C T I O N S
43 ****************************************************************/
44 
70 void __esos_set_PRNG_U32Seed(uint32_t u32_seed) {
71  __u32_esos_PRNG_Seed = u32_seed;
72 } // end __esos_set_PRNG_U32Seed()
73 
81  uint32_t hi, lo;
82 
83  lo = 16807 * ( __u32_esos_PRNG_Seed * 0xFFFF );
84  hi = 16807 * ( __u32_esos_PRNG_Seed >> 16 );
85  lo += (hi & 0x7FFF) << 16;
86  lo += (hi >> 15);
87  if (lo > 0x7FFFFFFF) lo -= 0x7FFFFFFF;
88  return (__u32_esos_PRNG_Seed = (uint32_t) lo );
89 } // end __esos_get_PRNG_RandomUint32()
90 
100 inline uint32_t esos_GetRandomUint32(void) {
101  return ( __esos_hw_PRNG_u32() );
102 } // end esos_getRandomUint32()
103 
104 
109 uint16_t esos_taskname_hash_u16( void* buf, uint16_t len ) {
110  unsigned char *bp = (unsigned char *)buf; /* start of buffer */
111  unsigned char *be = bp + len; /* beyond end of buffer */
112  uint32_t u32_temp = 2166136261uL;
113 
114  /*
115  * FNV-1 hash each octet in the buffer
116  */
117  while (bp < be) {
118  /* multiply by the 32 bit FNV magic prime mod 2^32 */
119  u32_temp += (u32_temp<<1) + (u32_temp<<4) + (u32_temp<<7) + (u32_temp<<8) + (u32_temp<<24);
120 
121  /* xor the bottom with the current octet */
122  u32_temp ^= (uint32_t)*bp++;
123  } // end while
124  return (uint16_t) ((u32_temp >> 16) ^ (u32_temp & 0xFFFF));
125 } // end esos_taskname_hash_u16
126 
127 
140 uint32_t esos_buffer_hash_u32(void *buf, uint16_t len) {
141  unsigned char *bp = (unsigned char *)buf; /* start of buffer */
142  unsigned char *be = bp + len; /* beyond end of buffer */
143 
144  /*
145  * FNV-1 hash each octet in the buffer
146  */
147  while (bp < be) {
148 
149  /* multiply by the 32 bit FNV magic prime mod 2^32 */
150 #if defined(NO_FNV_GCC_OPTIMIZATION)
151  __esos_u32FNVHash *= FNV_32_PRIME;
152 #else
153  __esos_u32FNVHash += (__esos_u32FNVHash<<1) + (__esos_u32FNVHash<<4) + (__esos_u32FNVHash<<7) + (__esos_u32FNVHash<<8) + (__esos_u32FNVHash<<24);
154 #endif
155 
156  /* xor the bottom with the current octet */
157  __esos_u32FNVHash ^= (uint32_t)*bp++;
158  }
159 
160  /* return our new hash value */
161  return __esos_u32FNVHash;
162 }
163 
164 
176 uint32_t esos_string_hash_u32(char *psz_str) {
177  unsigned char *ch_s = (unsigned char *)psz_str; /* unsigned string */
178 
179  /*
180  * FNV-1 hash each octet in the buffer
181  */
182  while (*ch_s) {
183 
184  /* multiply by the 32 bit FNV magic prime mod 2^32 */
185 #if defined(NO_FNV_GCC_OPTIMIZATION)
186  __esos_u32FNVHash *= FNV_32_PRIME;
187 #else
188  __esos_u32FNVHash += (__esos_u32FNVHash<<1) + (__esos_u32FNVHash<<4) + (__esos_u32FNVHash<<7) + (__esos_u32FNVHash<<8) + (__esos_u32FNVHash<<24);
189 #endif
190 
191  /* xor the bottom with the current octet */
192  __esos_u32FNVHash ^= (uint32_t)*ch_s++;
193  }
194 
195  /* return our new hash value */
196  return __esos_u32FNVHash;
197 }
198 
199 inline uint16_t esos_hash_u32_to_u16(uint32_t u32_hash) {
200  return (uint16_t) ((u32_hash>>16) ^ (u32_hash&0xFFFF));
201 }
202 
__esos_set_PRNG_U32Seed
void __esos_set_PRNG_U32Seed(uint32_t u32_seed)
Definition: esos_utils.c:70
esos_utils.h
esos_GetRandomUint32
uint32_t esos_GetRandomUint32(void)
Definition: esos_utils.c:100
__esos_get_PRNG_RandomUint32
uint32_t __esos_get_PRNG_RandomUint32(void)
Definition: esos_utils.c:80
esos.h
esos_buffer_hash_u32
uint32_t esos_buffer_hash_u32(void *buf, uint16_t len)
Definition: esos_utils.c:140
esos_taskname_hash_u16
uint16_t esos_taskname_hash_u16(void *buf, uint16_t len)
Definition: esos_utils.c:109
esos_string_hash_u32
uint32_t esos_string_hash_u32(char *psz_str)
Definition: esos_utils.c:176