-
Notifications
You must be signed in to change notification settings - Fork 59
/
util.c
146 lines (130 loc) · 3.23 KB
/
util.c
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* CPUID
*
* A simple and small tool to dump/decode CPUID information.
*
* Copyright (c) 2010-2021, Steven Noonan <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "prefix.h"
#include "util.h"
#include <ctype.h>
#include <limits.h>
#include <string.h>
#ifdef TARGET_OS_WINDOWS
#include <windows.h>
#else
#include <sys/time.h>
#endif
size_t safe_strcat(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}
uint32_t popcnt(uint32_t v)
{
uint32_t n = 0;
while (v) {
n += (v & 1);
v >>= 1;
}
return n;
}
uint32_t count_trailing_zero_bits(uint32_t v)
{
uint32_t c;
if (v) {
v = (v ^ (v - 1)) >> 1; /* Set v's trailing 0s to 1s and zero rest */
for (c = 0; v; c++)
{
v >>= 1;
}
} else {
c = CHAR_BIT * sizeof(v);
}
return c;
}
void squeeze(char *str)
{
int r; /* next character to be read */
int w; /* next character to be written */
r=w=0;
while (str[r])
{
if (isspace((int)(str[r])) || iscntrl((int)(str[r])))
{
if (w > 0 && !isspace((int)(str[w-1])))
str[w++] = ' ';
}
else
str[w++] = str[r];
r++;
}
str[w] = 0;
}
double time_sec(void)
{
#ifdef TARGET_OS_WINDOWS
LARGE_INTEGER freq, ctr;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&ctr);
return (double)ctr.QuadPart / (double)freq.QuadPart;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0);
#endif
}
#ifdef TARGET_OS_WINDOWS
int is_windows7_or_greater(void)
{
static int cached = -1;
if (cached < 0) {
OSVERSIONINFOEXW osvi;
DWORDLONG const dwlConditionMask = VerSetConditionMask(
VerSetConditionMask(
VerSetConditionMask(
0, VER_MAJORVERSION, VER_GREATER_EQUAL),
VER_MINORVERSION, VER_GREATER_EQUAL),
VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
memset(&osvi, 0, sizeof(osvi));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
osvi.dwMajorVersion = HIBYTE(_WIN32_WINNT_WIN7);
osvi.dwMinorVersion = LOBYTE(_WIN32_WINNT_WIN7);
osvi.wServicePackMajor = 0;
cached = (VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE) ? 1 : 0;
}
return cached;
}
#endif
/* vim: set ts=4 sts=4 sw=4 noet: */