xref: /freebsd/sys/contrib/openzfs/module/lua/lcompat.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: MIT
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy  * Copyright (c) 2016 by Delphix. All rights reserved.
4eda14cbcSMatt Macy  */
5eda14cbcSMatt Macy 
6eda14cbcSMatt Macy #include <sys/lua/lua.h>
7eda14cbcSMatt Macy 
8eda14cbcSMatt Macy 
9eda14cbcSMatt Macy ssize_t
lcompat_sprintf(char * buf,size_t size,const char * fmt,...)10eda14cbcSMatt Macy lcompat_sprintf(char *buf, size_t size, const char *fmt, ...)
11eda14cbcSMatt Macy {
12eda14cbcSMatt Macy 	ssize_t res;
13eda14cbcSMatt Macy 	va_list args;
14eda14cbcSMatt Macy 
15eda14cbcSMatt Macy 	va_start(args, fmt);
16eda14cbcSMatt Macy 	res = vsnprintf(buf, size, fmt, args);
17eda14cbcSMatt Macy 	va_end(args);
18eda14cbcSMatt Macy 
19eda14cbcSMatt Macy 	return (res);
20eda14cbcSMatt Macy }
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy int64_t
lcompat_strtoll(const char * str,char ** ptr)23eda14cbcSMatt Macy lcompat_strtoll(const char *str, char **ptr)
24eda14cbcSMatt Macy {
25eda14cbcSMatt Macy 	int base;
26eda14cbcSMatt Macy 	const char *cp;
27eda14cbcSMatt Macy 	int digits;
28eda14cbcSMatt Macy 	int64_t value;
29eda14cbcSMatt Macy 	boolean_t is_negative;
30eda14cbcSMatt Macy 
31eda14cbcSMatt Macy 	cp = str;
32eda14cbcSMatt Macy 	while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
33eda14cbcSMatt Macy 		cp++;
34eda14cbcSMatt Macy 	}
35eda14cbcSMatt Macy 	is_negative = (*cp == '-');
36eda14cbcSMatt Macy 	if (is_negative) {
37eda14cbcSMatt Macy 		cp++;
38eda14cbcSMatt Macy 	}
39eda14cbcSMatt Macy 	base = 10;
40eda14cbcSMatt Macy 
41eda14cbcSMatt Macy 	if (*cp == '0') {
42eda14cbcSMatt Macy 		base = 8;
43eda14cbcSMatt Macy 		cp++;
44eda14cbcSMatt Macy 		if (*cp == 'x' || *cp == 'X') {
45eda14cbcSMatt Macy 			base = 16;
46eda14cbcSMatt Macy 			cp++;
47eda14cbcSMatt Macy 		}
48eda14cbcSMatt Macy 	}
49eda14cbcSMatt Macy 
50eda14cbcSMatt Macy 	value = 0;
51eda14cbcSMatt Macy 	for (; *cp != '\0'; cp++) {
52eda14cbcSMatt Macy 		if (*cp >= '0' && *cp <= '9') {
53eda14cbcSMatt Macy 			digits = *cp - '0';
54eda14cbcSMatt Macy 		} else if (*cp >= 'a' && *cp <= 'f') {
55eda14cbcSMatt Macy 			digits = *cp - 'a' + 10;
56eda14cbcSMatt Macy 		} else if (*cp >= 'A' && *cp <= 'F') {
57eda14cbcSMatt Macy 			digits = *cp - 'A' + 10;
58eda14cbcSMatt Macy 		} else {
59eda14cbcSMatt Macy 			break;
60eda14cbcSMatt Macy 		}
61eda14cbcSMatt Macy 		if (digits >= base) {
62eda14cbcSMatt Macy 			break;
63eda14cbcSMatt Macy 		}
64eda14cbcSMatt Macy 		value = (value * base) + digits;
65eda14cbcSMatt Macy 	}
66eda14cbcSMatt Macy 
67eda14cbcSMatt Macy 	if (ptr != NULL) {
68eda14cbcSMatt Macy 		*ptr = (char *)cp;
69eda14cbcSMatt Macy 	}
70eda14cbcSMatt Macy 	if (is_negative) {
71eda14cbcSMatt Macy 		value = -value;
72eda14cbcSMatt Macy 	}
73eda14cbcSMatt Macy 	return (value);
74eda14cbcSMatt Macy }
75eda14cbcSMatt Macy 
76eda14cbcSMatt Macy int64_t
lcompat_pow(int64_t x,int64_t y)77eda14cbcSMatt Macy lcompat_pow(int64_t x, int64_t y)
78eda14cbcSMatt Macy {
79eda14cbcSMatt Macy 	int64_t result = 1;
80eda14cbcSMatt Macy 	if (y < 0)
81eda14cbcSMatt Macy 		return (0);
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy 	while (y) {
84eda14cbcSMatt Macy 		if (y & 1)
85eda14cbcSMatt Macy 			result *= x;
86eda14cbcSMatt Macy 		y >>= 1;
87eda14cbcSMatt Macy 		x *= x;
88eda14cbcSMatt Macy 	}
89eda14cbcSMatt Macy 	return (result);
90eda14cbcSMatt Macy }
91eda14cbcSMatt Macy 
92eda14cbcSMatt Macy int
lcompat_hashnum(int64_t x)93eda14cbcSMatt Macy lcompat_hashnum(int64_t x)
94eda14cbcSMatt Macy {
95eda14cbcSMatt Macy 	x = (~x) + (x << 18);
96eda14cbcSMatt Macy 	x = x ^ (x >> 31);
97eda14cbcSMatt Macy 	x = x * 21;
98eda14cbcSMatt Macy 	x = x ^ (x >> 11);
99eda14cbcSMatt Macy 	x = x + (x << 6);
100eda14cbcSMatt Macy 	x = x ^ (x >> 22);
101eda14cbcSMatt Macy 	return ((int)x);
102eda14cbcSMatt Macy }
103