OSDN Git Service

Updated MSWS RNG to latest version + some more fixes.
[mhash384/mhash384.git] / tools / GenTables / include / common.h
1 /* ----------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Generate tables utility program                                                     */
3 /* Copyright(c) 2016-2017 LoRd_MuldeR <mulder2@gmx.de>                                             */
4 /*                                                                                                 */
5 /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software   */
6 /* and associated documentation files(the "Software"), to deal in the Software without             */
7 /* restriction, including without limitation the rights to use, copy, modify, merge, publish,      */
8 /* distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the */
9 /* Software is furnished to do so, subject to the following conditions:                            */
10 /*                                                                                                 */
11 /* The above copyright notice and this permission notice shall be included in all copies or        */
12 /* substantial portions of the Software.                                                           */
13 /*                                                                                                 */
14 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING   */
15 /* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND      */
16 /* NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,     */
17 /* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  */
18 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.         */
19 /* ----------------------------------------------------------------------------------------------- */
20
21 #ifndef INC_COMMON_H
22 #define INC_COMMON_H
23
24 #define _CRT_RAND_S  
25 #include <stdlib.h> 
26 #include <stdint.h>
27 #include <time.h>
28 #include <stdio.h>
29
30 #include "hd_table.h"
31
32 static inline void crit_exit(const char *const msg)
33 {
34         printf("\n\n%s\n\n", msg);
35         flushall();
36 #ifndef NDEBUG
37         abort();
38 #endif
39         for(;;) _exit(666);
40 }
41
42 static inline uint32_t make_seed(void)
43 {
44         uint32_t seed;
45         if (rand_s(&seed) != 0)
46         {
47                 crit_exit("FATAL: System PRNG initialization has failed!");
48         }
49         return seed;
50 }
51
52 static inline void invert_byte_buffer(uint8_t *const buffer, const size_t size)
53 {
54         const size_t words = size / sizeof(uint32_t);
55         const size_t bytes = size % sizeof(uint32_t);
56         uint8_t *pos = buffer;
57         for (size_t i = 0; i < words; ++i)
58         {
59                 *((uint32_t*)pos) = (~(*((uint32_t*)pos)));
60                 pos += sizeof(uint32_t);
61         }
62         for (size_t i = 0; i < bytes; ++i)
63         {
64                 *pos = (~(*pos));
65                 pos += sizeof(uint8_t);
66         }
67 }
68
69 static inline uint32_t hamming_distance(const uint8_t *const a, const uint8_t *const b, const size_t len)
70 {
71         uint32_t distance = 0U;
72         for (size_t i = 0; i < len; ++i)
73         {
74                 distance += HAMMING_DISTANCE_LUT[a[i]][b[i]];
75         }
76         return distance;
77 }
78
79 static inline void get_time_str(char *const time_string, const size_t buff_size)
80 {
81         time_t current_time;
82         struct tm time_info;
83         time(&current_time);
84         if (localtime_s(&time_info, &current_time))
85         {
86                 crit_exit("FATAL: localtime_s() has failed!");
87         }
88         const size_t len = strftime(time_string, buff_size, "%H:%M:%S", &time_info);
89         if ((len < 1) || (len >= buff_size))
90         {
91                 crit_exit("FATAL: strftime() has failed!");
92         }
93 }
94
95 static inline uint32_t max_ui32(const uint32_t a, const uint32_t b)
96 {
97         return (a > b) ? a : b;
98 }
99
100 static inline uint_fast16_t max_ui16(const uint_fast16_t a, const uint_fast16_t b)
101 {
102         return (a > b) ? a : b;
103 }
104
105 static inline uint_fast8_t max_ui8(const uint_fast8_t a, const uint_fast8_t b)
106 {
107         return (a > b) ? a : b;
108 }
109
110 static inline double clip_dbl(const double min, const double val, const double max)
111 {
112         return (val > max) ? max : ((val < min) ? min : val);
113 }
114
115 static inline uint32_t adler32(const uint8_t *const buffer, size_t buflength)
116 {
117         uint32_t s1 = 1, s2 = 0;
118         for (size_t n = 0; n < buflength; n++)
119         {
120                 s1 = (s1 + buffer[n]) % 65521;
121                 s2 = (s2 + s1) % 65521;
122         }
123         return (s2 << 16) | s1;
124 }
125
126 #endif //INC_COMMON_H