2015-12-04 14:47:52 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2015-12-04 14:47:52 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
2016-09-05 19:47:26 +00:00
|
|
|
#include "common.h"
|
2016-09-16 15:01:18 +00:00
|
|
|
#include "types.h"
|
2016-09-05 19:47:26 +00:00
|
|
|
#include "shared.h"
|
2015-12-04 14:47:52 +00:00
|
|
|
|
2018-01-27 12:26:36 +00:00
|
|
|
#if defined (__CYGWIN__)
|
|
|
|
#include <sys/cygwin.h>
|
|
|
|
#endif
|
|
|
|
|
2017-02-23 09:39:17 +00:00
|
|
|
static inline int get_msb32 (const u32 v)
|
2017-02-22 15:33:23 +00:00
|
|
|
{
|
2017-03-02 22:12:31 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 32; i > 0; i--) if ((v >> (i - 1)) & 1) break;
|
|
|
|
|
|
|
|
return i;
|
2017-02-23 09:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int get_msb64 (const u64 v)
|
|
|
|
{
|
2017-03-02 22:12:31 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 64; i > 0; i--) if ((v >> (i - 1)) & 1) break;
|
|
|
|
|
|
|
|
return i;
|
2017-02-23 09:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool overflow_check_u32_add (const u32 a, const u32 b)
|
|
|
|
{
|
|
|
|
const int a_msb = get_msb32 (a);
|
|
|
|
const int b_msb = get_msb32 (b);
|
|
|
|
|
|
|
|
return ((a_msb < 32) && (b_msb < 32));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool overflow_check_u32_mul (const u32 a, const u32 b)
|
|
|
|
{
|
|
|
|
const int a_msb = get_msb32 (a);
|
|
|
|
const int b_msb = get_msb32 (b);
|
|
|
|
|
|
|
|
return ((a_msb + b_msb) < 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool overflow_check_u64_add (const u64 a, const u64 b)
|
|
|
|
{
|
|
|
|
const int a_msb = get_msb64 (a);
|
|
|
|
const int b_msb = get_msb64 (b);
|
2017-02-22 15:33:23 +00:00
|
|
|
|
2017-02-23 09:39:17 +00:00
|
|
|
return ((a_msb < 64) && (b_msb < 64));
|
2017-02-22 15:33:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 09:39:17 +00:00
|
|
|
bool overflow_check_u64_mul (const u64 a, const u64 b)
|
2017-02-22 15:33:23 +00:00
|
|
|
{
|
2017-02-23 09:39:17 +00:00
|
|
|
const int a_msb = get_msb64 (a);
|
|
|
|
const int b_msb = get_msb64 (b);
|
2017-02-22 15:33:23 +00:00
|
|
|
|
2017-02-23 09:39:17 +00:00
|
|
|
return ((a_msb + b_msb) < 64);
|
2017-02-22 15:33:23 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 15:04:31 +00:00
|
|
|
bool is_power_of_2 (const u32 v)
|
|
|
|
{
|
|
|
|
return (v && !(v & (v - 1)));
|
|
|
|
}
|
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
u32 mydivc32 (const u32 dividend, const u32 divisor)
|
2016-09-08 07:21:25 +00:00
|
|
|
{
|
2016-09-08 08:01:49 +00:00
|
|
|
u32 quotient = dividend / divisor;
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
if (dividend % divisor) quotient++;
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
return quotient;
|
|
|
|
}
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
u64 mydivc64 (const u64 dividend, const u64 divisor)
|
|
|
|
{
|
|
|
|
u64 quotient = dividend / divisor;
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
if (dividend % divisor) quotient++;
|
|
|
|
|
|
|
|
return quotient;
|
|
|
|
}
|
|
|
|
|
2016-09-24 19:44:43 +00:00
|
|
|
char *filename_from_filepath (char *filepath)
|
|
|
|
{
|
|
|
|
char *ptr = NULL;
|
|
|
|
|
|
|
|
if ((ptr = strrchr (filepath, '/')) != NULL)
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
else if ((ptr = strrchr (filepath, '\\')) != NULL)
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = filepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2016-09-30 15:41:40 +00:00
|
|
|
void naive_replace (char *s, const char key_char, const char replace_char)
|
2016-09-08 08:01:49 +00:00
|
|
|
{
|
|
|
|
const size_t len = strlen (s);
|
|
|
|
|
|
|
|
for (size_t in = 0; in < len; in++)
|
2016-09-08 07:21:25 +00:00
|
|
|
{
|
2016-09-30 15:41:40 +00:00
|
|
|
const char c = s[in];
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
if (c == key_char)
|
2016-09-08 07:21:25 +00:00
|
|
|
{
|
2016-09-08 08:01:49 +00:00
|
|
|
s[in] = replace_char;
|
2016-09-08 07:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-08 08:01:49 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 15:41:40 +00:00
|
|
|
void naive_escape (char *s, size_t s_max, const char key_char, const char escape_char)
|
2016-09-08 08:01:49 +00:00
|
|
|
{
|
|
|
|
char s_escaped[1024] = { 0 };
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
size_t s_escaped_max = sizeof (s_escaped);
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
const size_t len = strlen (s);
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
for (size_t in = 0, out = 0; in < len; in++, out++)
|
|
|
|
{
|
2016-09-30 15:41:40 +00:00
|
|
|
const char c = s[in];
|
2016-09-06 09:49:26 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
if (c == key_char)
|
|
|
|
{
|
|
|
|
s_escaped[out] = escape_char;
|
2015-12-04 14:47:52 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
out++;
|
|
|
|
}
|
2015-12-04 14:47:52 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
if (out == s_escaped_max - 2) break;
|
2015-12-04 14:47:52 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
s_escaped[out] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy (s, s_escaped, s_max - 1);
|
2016-09-06 09:49:26 +00:00
|
|
|
}
|
2015-12-04 14:47:52 +00:00
|
|
|
|
2016-12-23 23:40:40 +00:00
|
|
|
void hc_asprintf (char **strp, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start (args, fmt);
|
|
|
|
int rc __attribute__((unused));
|
2016-12-23 23:57:38 +00:00
|
|
|
rc = vasprintf (strp, fmt, args);
|
2016-12-23 23:40:40 +00:00
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:22:57 +00:00
|
|
|
#if defined (_WIN)
|
|
|
|
#define __WINDOWS__
|
|
|
|
#endif
|
|
|
|
#include "sort_r.h"
|
|
|
|
#if defined (_WIN)
|
|
|
|
#undef __WINDOWS__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void hc_qsort_r (void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg)
|
|
|
|
{
|
|
|
|
sort_r (base, nmemb, size, compar, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *hc_bsearch_r (const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg)
|
|
|
|
{
|
|
|
|
for (size_t l = 0, r = nmemb; r; r >>= 1)
|
|
|
|
{
|
|
|
|
const size_t m = r >> 1;
|
|
|
|
|
|
|
|
const size_t c = l + m;
|
|
|
|
|
2017-11-14 04:46:26 +00:00
|
|
|
const char *next = (const char *) base + (c * size);
|
2016-11-16 09:22:57 +00:00
|
|
|
|
|
|
|
const int cmp = (*compar) (key, next, arg);
|
|
|
|
|
|
|
|
if (cmp > 0)
|
|
|
|
{
|
|
|
|
l += m + 1;
|
|
|
|
|
|
|
|
r--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp == 0) return ((void *) next);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2017-01-27 10:46:45 +00:00
|
|
|
bool hc_path_is_file (const char *path)
|
|
|
|
{
|
2017-12-10 00:40:45 +00:00
|
|
|
struct stat s;
|
2017-01-27 10:46:45 +00:00
|
|
|
|
2017-12-10 00:40:45 +00:00
|
|
|
if (stat (path, &s) == -1) return false;
|
2017-01-27 10:46:45 +00:00
|
|
|
|
|
|
|
if (S_ISREG (s.st_mode)) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hc_path_is_directory (const char *path)
|
|
|
|
{
|
2017-12-10 00:40:45 +00:00
|
|
|
struct stat s;
|
2017-01-27 10:46:45 +00:00
|
|
|
|
2017-12-10 00:40:45 +00:00
|
|
|
if (stat (path, &s) == -1) return false;
|
2017-01-27 10:46:45 +00:00
|
|
|
|
|
|
|
if (S_ISDIR (s.st_mode)) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hc_path_is_empty (const char *path)
|
|
|
|
{
|
2017-12-10 00:40:45 +00:00
|
|
|
struct stat s;
|
2017-01-27 10:46:45 +00:00
|
|
|
|
2017-12-10 00:40:45 +00:00
|
|
|
if (stat (path, &s) == -1) return false;
|
2017-01-27 10:46:45 +00:00
|
|
|
|
|
|
|
if (s.st_size == 0) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hc_path_exist (const char *path)
|
|
|
|
{
|
|
|
|
if (access (path, F_OK) == -1) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hc_path_read (const char *path)
|
|
|
|
{
|
|
|
|
if (access (path, R_OK) == -1) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hc_path_write (const char *path)
|
|
|
|
{
|
|
|
|
if (access (path, W_OK) == -1) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:08:12 +00:00
|
|
|
bool hc_path_create (const char *path)
|
|
|
|
{
|
|
|
|
if (hc_path_exist (path) == true) return false;
|
|
|
|
|
2017-02-14 15:12:18 +00:00
|
|
|
const int fd = creat (path, S_IRUSR | S_IWUSR);
|
|
|
|
|
|
|
|
if (fd == -1) return false;
|
|
|
|
|
|
|
|
close (fd);
|
2017-01-27 23:08:12 +00:00
|
|
|
|
|
|
|
unlink (path);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-19 14:11:41 +00:00
|
|
|
bool hc_string_is_digit (const char *s)
|
|
|
|
{
|
|
|
|
if (s == NULL) return false;
|
|
|
|
|
|
|
|
const size_t len = strlen (s);
|
|
|
|
|
|
|
|
if (len == 0) return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
const int c = (const int) s[i];
|
|
|
|
|
|
|
|
if (isdigit (c) == 0) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-28 21:53:46 +00:00
|
|
|
void setup_environment_variables ()
|
|
|
|
{
|
|
|
|
char *compute = getenv ("COMPUTE");
|
|
|
|
|
|
|
|
if (compute)
|
|
|
|
{
|
2017-10-04 11:04:58 +00:00
|
|
|
char *display;
|
2016-09-28 21:53:46 +00:00
|
|
|
|
2017-10-03 05:10:10 +00:00
|
|
|
hc_asprintf (&display, "DISPLAY=%s", compute);
|
2016-09-28 21:53:46 +00:00
|
|
|
|
2017-02-15 13:32:24 +00:00
|
|
|
putenv (display);
|
2017-10-04 11:04:58 +00:00
|
|
|
|
|
|
|
free (display);
|
2016-09-28 21:53:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (getenv ("DISPLAY") == NULL)
|
|
|
|
putenv ((char *) "DISPLAY=:0");
|
|
|
|
}
|
|
|
|
|
2017-07-27 22:38:17 +00:00
|
|
|
if (getenv ("OCL_CODE_CACHE_ENABLE") == NULL)
|
|
|
|
putenv ((char *) "OCL_CODE_CACHE_ENABLE=0");
|
|
|
|
|
2016-09-28 21:53:46 +00:00
|
|
|
if (getenv ("CUDA_CACHE_DISABLE") == NULL)
|
|
|
|
putenv ((char *) "CUDA_CACHE_DISABLE=1");
|
|
|
|
|
|
|
|
if (getenv ("POCL_KERNEL_CACHE") == NULL)
|
|
|
|
putenv ((char *) "POCL_KERNEL_CACHE=0");
|
2018-01-27 12:26:36 +00:00
|
|
|
|
|
|
|
#if defined (__CYGWIN__)
|
|
|
|
cygwin_internal (CW_SYNC_WINENV);
|
|
|
|
#endif
|
2016-09-28 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup_umask ()
|
|
|
|
{
|
|
|
|
umask (077);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup_seeding (const bool rp_gen_seed_chgd, const u32 rp_gen_seed)
|
|
|
|
{
|
|
|
|
if (rp_gen_seed_chgd == true)
|
|
|
|
{
|
|
|
|
srand (rp_gen_seed);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-08 18:13:29 +00:00
|
|
|
const time_t ts = time (NULL); // don't tell me that this is an insecure seed
|
2016-09-28 21:53:46 +00:00
|
|
|
|
2018-02-08 18:13:29 +00:00
|
|
|
srand ((unsigned int) ts);
|
2016-09-28 21:53:46 +00:00
|
|
|
}
|
2017-02-15 11:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 get_random_num (const u32 min, const u32 max)
|
|
|
|
{
|
|
|
|
if (min == max) return (min);
|
|
|
|
|
2017-02-15 12:26:02 +00:00
|
|
|
const u32 low = max - min;
|
2017-02-15 11:36:03 +00:00
|
|
|
|
|
|
|
if (low == 0) return (0);
|
|
|
|
|
2017-07-28 00:14:55 +00:00
|
|
|
#if defined (_WIN)
|
2017-02-15 11:36:03 +00:00
|
|
|
|
2017-07-29 22:54:20 +00:00
|
|
|
return (((u32) rand () % (max - min)) + min);
|
2017-02-15 11:36:03 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-07-28 00:14:55 +00:00
|
|
|
return (((u32) random () % (max - min)) + min);
|
2017-02-15 11:36:03 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
2017-06-02 08:08:19 +00:00
|
|
|
|
|
|
|
void hc_string_trim_leading (char *s)
|
|
|
|
{
|
|
|
|
int skip = 0;
|
|
|
|
|
|
|
|
const int len = (int) strlen (s);
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
const int c = (const int) s[i];
|
|
|
|
|
|
|
|
if (isspace (c) == 0) break;
|
|
|
|
|
|
|
|
skip++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip == 0) return;
|
|
|
|
|
|
|
|
const int new_len = len - skip;
|
|
|
|
|
|
|
|
memmove (s, s + skip, new_len);
|
|
|
|
|
|
|
|
s[new_len] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hc_string_trim_trailing (char *s)
|
|
|
|
{
|
|
|
|
int skip = 0;
|
|
|
|
|
|
|
|
const int len = (int) strlen (s);
|
|
|
|
|
|
|
|
for (int i = len - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
const int c = (const int) s[i];
|
|
|
|
|
|
|
|
if (isspace (c) == 0) break;
|
|
|
|
|
|
|
|
skip++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip == 0) return;
|
|
|
|
|
|
|
|
const size_t new_len = len - skip;
|
|
|
|
|
|
|
|
s[new_len] = 0;
|
|
|
|
}
|
2017-07-06 08:35:25 +00:00
|
|
|
|
|
|
|
size_t hc_fread (void *ptr, size_t size, size_t nmemb, FILE *stream)
|
|
|
|
{
|
|
|
|
return fread (ptr, size, nmemb, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void hc_fwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)
|
|
|
|
{
|
|
|
|
size_t rc = fwrite (ptr, size, nmemb, stream);
|
|
|
|
|
|
|
|
if (rc == 0) rc = 0;
|
|
|
|
}
|
2017-08-16 15:51:35 +00:00
|
|
|
|
2017-10-10 09:30:20 +00:00
|
|
|
bool hc_same_files (char *file1, char *file2)
|
|
|
|
{
|
|
|
|
if ((file1 != NULL) && (file2 != NULL))
|
|
|
|
{
|
2017-12-10 00:40:45 +00:00
|
|
|
struct stat tmpstat_file1;
|
|
|
|
struct stat tmpstat_file2;
|
2017-10-10 09:30:20 +00:00
|
|
|
|
|
|
|
int do_check = 0;
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen (file1, "r");
|
|
|
|
|
|
|
|
if (fp)
|
|
|
|
{
|
2017-12-10 00:40:45 +00:00
|
|
|
if (fstat (fileno (fp), &tmpstat_file1))
|
2017-10-10 09:30:20 +00:00
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
do_check++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen (file2, "r");
|
|
|
|
|
|
|
|
if (fp)
|
|
|
|
{
|
2017-12-10 00:40:45 +00:00
|
|
|
if (fstat (fileno (fp), &tmpstat_file2))
|
2017-10-10 09:30:20 +00:00
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
do_check++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_check == 2)
|
|
|
|
{
|
|
|
|
tmpstat_file1.st_mode = 0;
|
|
|
|
tmpstat_file1.st_nlink = 0;
|
|
|
|
tmpstat_file1.st_uid = 0;
|
|
|
|
tmpstat_file1.st_gid = 0;
|
|
|
|
tmpstat_file1.st_rdev = 0;
|
|
|
|
tmpstat_file1.st_atime = 0;
|
|
|
|
|
|
|
|
#if defined (STAT_NANOSECONDS_ACCESS_TIME)
|
|
|
|
tmpstat_file1.STAT_NANOSECONDS_ACCESS_TIME = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined (_POSIX)
|
|
|
|
tmpstat_file1.st_blksize = 0;
|
|
|
|
tmpstat_file1.st_blocks = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tmpstat_file2.st_mode = 0;
|
|
|
|
tmpstat_file2.st_nlink = 0;
|
|
|
|
tmpstat_file2.st_uid = 0;
|
|
|
|
tmpstat_file2.st_gid = 0;
|
|
|
|
tmpstat_file2.st_rdev = 0;
|
|
|
|
tmpstat_file2.st_atime = 0;
|
|
|
|
|
|
|
|
#if defined (STAT_NANOSECONDS_ACCESS_TIME)
|
|
|
|
tmpstat_file2.STAT_NANOSECONDS_ACCESS_TIME = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined (_POSIX)
|
|
|
|
tmpstat_file2.st_blksize = 0;
|
|
|
|
tmpstat_file2.st_blocks = 0;
|
|
|
|
#endif
|
|
|
|
|
2017-12-10 00:40:45 +00:00
|
|
|
if (memcmp (&tmpstat_file1, &tmpstat_file2, sizeof (struct stat)) == 0)
|
2017-10-10 09:30:20 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-08 18:13:29 +00:00
|
|
|
|
|
|
|
u32 hc_strtoul (const char *nptr, char **endptr, int base)
|
|
|
|
{
|
|
|
|
return (u32) strtoul (nptr, endptr, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 hc_strtoull (const char *nptr, char **endptr, int base)
|
|
|
|
{
|
|
|
|
return (u64) strtoull (nptr, endptr, base);
|
|
|
|
}
|
2018-02-11 09:56:08 +00:00
|
|
|
|
|
|
|
u32 power_of_two_ceil_32 (const u32 v)
|
|
|
|
{
|
|
|
|
u32 r = v;
|
|
|
|
|
|
|
|
r--;
|
|
|
|
|
|
|
|
r |= r >> 1;
|
|
|
|
r |= r >> 2;
|
|
|
|
r |= r >> 4;
|
|
|
|
r |= r >> 8;
|
|
|
|
r |= r >> 16;
|
|
|
|
|
|
|
|
r++;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 power_of_two_floor_32 (const u32 v)
|
|
|
|
{
|
|
|
|
u32 r = power_of_two_ceil_32 (v);
|
|
|
|
|
|
|
|
if (r > v)
|
|
|
|
{
|
|
|
|
r >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2018-02-13 12:20:29 +00:00
|
|
|
|
|
|
|
u32 round_up_multiple_32 (const u32 v, const u32 m)
|
|
|
|
{
|
|
|
|
if (m == 0) return v;
|
|
|
|
|
|
|
|
const u32 r = v % m;
|
|
|
|
|
|
|
|
if (r == 0) return v;
|
|
|
|
|
|
|
|
return v + m - r;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 round_up_multiple_64 (const u64 v, const u64 m)
|
|
|
|
{
|
|
|
|
if (m == 0) return v;
|
|
|
|
|
|
|
|
const u64 r = v % m;
|
|
|
|
|
|
|
|
if (r == 0) return v;
|
|
|
|
|
|
|
|
return v + m - r;
|
|
|
|
}
|