blob: c0a27182c7d874c4d4f5765dcb064bf9f1000be0 (
plain)
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
|
/*
* Copyright (c) 2016 by Delphix. All rights reserved.
*/
#include <sys/lua/lua.h>
ssize_t
lcompat_sprintf(char *buf, size_t size, const char *fmt, ...)
{
ssize_t res;
va_list args;
va_start(args, fmt);
res = vsnprintf(buf, size, fmt, args);
va_end(args);
return (res);
}
int64_t
lcompat_strtoll(const char *str, char **ptr)
{
int base;
const char *cp;
int digits;
int64_t value;
boolean_t is_negative;
cp = str;
while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
cp++;
}
is_negative = (*cp == '-');
if (is_negative) {
cp++;
}
base = 10;
if (*cp == '0') {
base = 8;
cp++;
if (*cp == 'x' || *cp == 'X') {
base = 16;
cp++;
}
}
value = 0;
for (; *cp != '\0'; cp++) {
if (*cp >= '0' && *cp <= '9') {
digits = *cp - '0';
} else if (*cp >= 'a' && *cp <= 'f') {
digits = *cp - 'a' + 10;
} else if (*cp >= 'A' && *cp <= 'F') {
digits = *cp - 'A' + 10;
} else {
break;
}
if (digits >= base) {
break;
}
value = (value * base) + digits;
}
if (ptr != NULL) {
*ptr = (char *)cp;
}
if (is_negative) {
value = -value;
}
return (value);
}
int64_t
lcompat_pow(int64_t x, int64_t y)
{
int64_t result = 1;
if (y < 0)
return (0);
while (y) {
if (y & 1)
result *= x;
y >>= 1;
x *= x;
}
return (result);
}
int
lcompat_hashnum(int64_t x)
{
x = (~x) + (x << 18);
x = x ^ (x >> 31);
x = x * 21;
x = x ^ (x >> 11);
x = x + (x << 6);
x = x ^ (x >> 22);
return ((int)x);
}
|