btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / kernel / util / Random.h
blobd30976d1987aeb8f1ea5b2f18635d67096b4ef9e
1 /*
2 * Copyright 2013 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Paweł Dziepak, pdziepak@quarnos.org
7 */
8 #ifndef KERNEL_UTIL_RANDOM_H
9 #define KERNEL_UTIL_RANDOM_H
12 #include <smp.h>
13 #include <SupportDefs.h>
16 #define MAX_FAST_RANDOM_VALUE 0x7fff
17 #define MAX_RANDOM_VALUE 0x7fffffffu
18 #define MAX_SECURE_RANDOM_VALUE 0xffffffffu
20 static const int kFastRandomShift = 15;
21 static const int kRandomShift = 31;
22 static const int kSecureRandomShift = 32;
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
28 unsigned int fast_random_value(void);
29 unsigned int random_value(void);
30 unsigned int secure_random_value(void);
32 #ifdef __cplusplus
34 #endif
37 #ifdef __cplusplus
39 template<typename T>
41 fast_get_random()
43 size_t shift = 0;
44 T random = 0;
45 while (shift < sizeof(T) * 8) {
46 random |= (T)fast_random_value() << shift;
47 shift += kFastRandomShift;
50 return random;
54 template<typename T>
56 get_random()
58 size_t shift = 0;
59 T random = 0;
60 while (shift < sizeof(T) * 8) {
61 random |= (T)random_value() << shift;
62 shift += kRandomShift;
65 return random;
69 template<typename T>
71 secure_get_random()
73 size_t shift = 0;
74 T random = 0;
75 while (shift < sizeof(T) * 8) {
76 random |= (T)secure_random_value() << shift;
77 shift += kSecureRandomShift;
80 return random;
84 #endif // __cplusplus
86 #endif // KERNEL_UTIL_RANDOM_H