2016-09-21 11:57:24 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Józef Kucia for CodeWeavers
|
|
|
|
*
|
2017-06-16 20:05:54 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2016-09-21 11:57:24 +01:00
|
|
|
*
|
2017-06-16 20:05:54 +01:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2016-09-21 11:57:24 +01:00
|
|
|
*
|
2017-06-16 20:05:54 +01:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2016-09-21 11:57:24 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __VKD3D_COMMON_H
|
|
|
|
#define __VKD3D_COMMON_H
|
|
|
|
|
|
|
|
#include "vkd3d_windows.h"
|
2020-03-26 23:13:35 +00:00
|
|
|
#include "vkd3d_spinlock.h"
|
2020-07-17 12:00:43 +01:00
|
|
|
#include "vkd3d_profiling.h"
|
2016-09-21 11:57:24 +01:00
|
|
|
|
2019-06-17 14:43:31 +01:00
|
|
|
#include <ctype.h>
|
2020-03-03 14:43:28 +00:00
|
|
|
#include <stdint.h>
|
2019-07-04 15:41:09 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
2022-06-08 11:32:46 +01:00
|
|
|
#include <assert.h>
|
2019-06-17 14:43:31 +01:00
|
|
|
|
2019-10-01 11:33:38 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <intrin.h>
|
2022-02-02 12:37:01 +00:00
|
|
|
#else
|
|
|
|
#include <time.h>
|
2019-10-01 11:33:38 +01:00
|
|
|
#endif
|
|
|
|
|
2019-01-31 10:29:34 +00:00
|
|
|
#ifndef ARRAY_SIZE
|
|
|
|
# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
|
|
|
|
#endif
|
2016-09-21 11:57:24 +01:00
|
|
|
|
2017-09-14 13:57:09 +01:00
|
|
|
#define DIV_ROUND_UP(a, b) ((a) % (b) == 0 ? (a) / (b) : (a) / (b) + 1)
|
|
|
|
|
2018-11-08 16:19:31 +00:00
|
|
|
#define STATIC_ASSERT(e) extern void __VKD3D_STATIC_ASSERT__(int [(e) ? 1 : -1])
|
|
|
|
|
2018-11-08 16:19:32 +00:00
|
|
|
#define MEMBER_SIZE(t, m) sizeof(((t *)0)->m)
|
|
|
|
|
2021-10-15 15:51:14 +01:00
|
|
|
static inline uint64_t align64(uint64_t addr, uint64_t alignment)
|
|
|
|
{
|
2022-06-08 11:32:46 +01:00
|
|
|
assert(alignment > 0 && (alignment & (alignment - 1)) == 0);
|
2021-10-15 15:51:14 +01:00
|
|
|
return (addr + (alignment - 1)) & ~(alignment - 1);
|
|
|
|
}
|
|
|
|
|
2017-06-16 21:38:21 +01:00
|
|
|
static inline size_t align(size_t addr, size_t alignment)
|
|
|
|
{
|
2022-06-08 11:32:46 +01:00
|
|
|
assert(alignment > 0 && (alignment & (alignment - 1)) == 0);
|
2017-06-16 21:38:21 +01:00
|
|
|
return (addr + (alignment - 1)) & ~(alignment - 1);
|
|
|
|
}
|
|
|
|
|
2016-10-10 10:22:50 +01:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# define VKD3D_PRINTF_FUNC(fmt, args) __attribute__((format(printf, fmt, args)))
|
|
|
|
# define VKD3D_UNUSED __attribute__((unused))
|
|
|
|
#else
|
|
|
|
# define VKD3D_PRINTF_FUNC(fmt, args)
|
|
|
|
# define VKD3D_UNUSED
|
|
|
|
#endif /* __GNUC__ */
|
|
|
|
|
2017-09-08 14:04:30 +01:00
|
|
|
static inline unsigned int vkd3d_popcount(unsigned int v)
|
|
|
|
{
|
2019-10-01 11:33:38 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
return __popcnt(v);
|
2020-10-30 09:09:58 +00:00
|
|
|
#elif defined(__GNUC__) || defined(__clang__)
|
2017-09-08 14:04:30 +01:00
|
|
|
return __builtin_popcount(v);
|
|
|
|
#else
|
2017-09-08 14:04:30 +01:00
|
|
|
v -= (v >> 1) & 0x55555555;
|
|
|
|
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
|
|
|
|
return (((v + (v >> 4)) & 0x0f0f0f0f) * 0x01010101) >> 24;
|
2017-09-08 14:04:30 +01:00
|
|
|
#endif
|
2017-09-08 14:04:30 +01:00
|
|
|
}
|
|
|
|
|
2019-07-04 15:41:09 +01:00
|
|
|
static inline bool vkd3d_bitmask_is_contiguous(unsigned int mask)
|
|
|
|
{
|
|
|
|
unsigned int i, j;
|
|
|
|
|
|
|
|
for (i = 0, j = 0; i < sizeof(mask) * CHAR_BIT; ++i)
|
|
|
|
{
|
|
|
|
if (mask & (1u << i))
|
|
|
|
++j;
|
|
|
|
else if (j)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vkd3d_popcount(mask) == j;
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:43:28 +00:00
|
|
|
/* Returns 64 for mask == 0 */
|
|
|
|
static inline unsigned int vkd3d_bitmask_tzcnt64(uint64_t mask)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
unsigned long result;
|
2020-07-07 15:28:21 +01:00
|
|
|
#ifdef _WIN64
|
|
|
|
return _BitScanForward64(&result, mask) ? result : 64;
|
|
|
|
#else
|
|
|
|
uint32_t lower, upper;
|
|
|
|
lower = (uint32_t)mask;
|
|
|
|
upper = (uint32_t)(mask >> 32);
|
|
|
|
if (_BitScanForward(&result, lower))
|
|
|
|
return result;
|
|
|
|
else if (_BitScanForward(&result, upper))
|
|
|
|
return result + 32;
|
|
|
|
else
|
|
|
|
return 64;
|
|
|
|
#endif
|
2020-06-24 14:44:00 +01:00
|
|
|
#elif defined(__GNUC__) || defined(__clang__)
|
2020-03-03 14:43:28 +00:00
|
|
|
return mask ? __builtin_ctzll(mask) : 64;
|
|
|
|
#else
|
2020-06-24 14:44:00 +01:00
|
|
|
#error "No implementation for ctzll."
|
2020-03-03 14:43:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-06-24 14:42:55 +01:00
|
|
|
/* Returns 32 for mask == 0 */
|
|
|
|
static inline unsigned int vkd3d_bitmask_tzcnt32(uint32_t mask)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
unsigned long result;
|
2022-04-22 10:38:06 +01:00
|
|
|
return _BitScanForward(&result, mask) ? result : 32;
|
2020-06-24 14:42:55 +01:00
|
|
|
#elif defined(__GNUC__) || defined(__clang__)
|
|
|
|
return mask ? __builtin_ctz(mask) : 32;
|
|
|
|
#else
|
|
|
|
#error "No implementation for ctz."
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:43:28 +00:00
|
|
|
/* find least significant bit, then remove that bit from mask */
|
|
|
|
static inline unsigned int vkd3d_bitmask_iter64(uint64_t* mask)
|
|
|
|
{
|
|
|
|
uint64_t cur_mask = *mask;
|
|
|
|
*mask = cur_mask & (cur_mask - 1);
|
|
|
|
return vkd3d_bitmask_tzcnt64(cur_mask);
|
|
|
|
}
|
|
|
|
|
2020-06-25 12:40:15 +01:00
|
|
|
static inline unsigned int vkd3d_bitmask_iter32(uint32_t *mask)
|
|
|
|
{
|
|
|
|
uint32_t cur_mask = *mask;
|
|
|
|
*mask = cur_mask & (cur_mask - 1);
|
|
|
|
return vkd3d_bitmask_tzcnt32(cur_mask);
|
|
|
|
}
|
|
|
|
|
2020-06-24 14:42:55 +01:00
|
|
|
struct vkd3d_bitmask_range
|
|
|
|
{
|
|
|
|
unsigned int offset;
|
|
|
|
unsigned int count;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct vkd3d_bitmask_range vkd3d_bitmask_iter32_range(uint32_t *mask)
|
|
|
|
{
|
|
|
|
struct vkd3d_bitmask_range range;
|
|
|
|
uint32_t tmp;
|
|
|
|
|
|
|
|
if (*mask == ~0u)
|
|
|
|
{
|
|
|
|
range.offset = 0;
|
|
|
|
range.count = 32;
|
|
|
|
*mask = 0u;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
range.offset = vkd3d_bitmask_tzcnt32(*mask);
|
|
|
|
tmp = *mask >> range.offset;
|
|
|
|
range.count = vkd3d_bitmask_tzcnt32(~tmp);
|
|
|
|
*mask &= ~(((1u << range.count) - 1u) << range.offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
2018-01-11 16:03:44 +00:00
|
|
|
/* Undefined for x == 0. */
|
|
|
|
static inline unsigned int vkd3d_log2i(unsigned int x)
|
|
|
|
{
|
2019-10-01 11:33:39 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
/* _BitScanReverse returns the index of the highest set bit,
|
|
|
|
* unlike clz which is 31 - index. */
|
|
|
|
unsigned long result;
|
|
|
|
_BitScanReverse(&result, x);
|
|
|
|
return (unsigned int)result;
|
2020-10-30 09:09:58 +00:00
|
|
|
#elif defined(__GNUC__) || defined(__clang__)
|
2018-01-11 16:03:44 +00:00
|
|
|
return __builtin_clz(x) ^ 0x1f;
|
|
|
|
#else
|
|
|
|
static const unsigned int l[] =
|
|
|
|
{
|
|
|
|
~0u, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
|
|
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
|
|
|
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
|
|
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
|
|
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
|
|
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
|
|
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
|
|
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
};
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
return (i = x >> 16) ? (x = i >> 8) ? l[x] + 24
|
|
|
|
: l[i] + 16 : (i = x >> 8) ? l[i] + 8 : l[x];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-02-23 15:22:26 +00:00
|
|
|
static inline unsigned int vkd3d_log2i_ceil(unsigned int x)
|
|
|
|
{
|
|
|
|
if (x == 1)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return vkd3d_log2i(x - 1) + 1;
|
|
|
|
}
|
|
|
|
|
2019-04-30 13:33:48 +01:00
|
|
|
static inline int ascii_isupper(int c)
|
|
|
|
{
|
|
|
|
return 'A' <= c && c <= 'Z';
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ascii_tolower(int c)
|
|
|
|
{
|
|
|
|
return ascii_isupper(c) ? c - 'A' + 'a' : c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ascii_strcasecmp(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
int c_a, c_b;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
c_a = ascii_tolower(*a++);
|
|
|
|
c_b = ascii_tolower(*b++);
|
|
|
|
} while (c_a == c_b && c_a != '\0');
|
|
|
|
|
|
|
|
return c_a - c_b;
|
|
|
|
}
|
|
|
|
|
2020-05-05 12:13:06 +01:00
|
|
|
static inline bool is_power_of_two(unsigned int x)
|
|
|
|
{
|
|
|
|
return x && !(x & (x -1));
|
|
|
|
}
|
|
|
|
|
2020-12-02 18:19:28 +00:00
|
|
|
static inline void vkd3d_parse_version(const char *version, int *major, int *minor, int *patch)
|
2019-06-17 14:43:31 +01:00
|
|
|
{
|
2020-12-02 18:19:28 +00:00
|
|
|
char *end;
|
2019-06-17 14:43:31 +01:00
|
|
|
|
2020-12-02 18:19:28 +00:00
|
|
|
*major = strtol(version, &end, 10);
|
|
|
|
version = end;
|
|
|
|
if (*version == '.')
|
2019-06-17 14:43:31 +01:00
|
|
|
++version;
|
2020-12-02 18:19:28 +00:00
|
|
|
*minor = strtol(version, &end, 10);
|
|
|
|
version = end;
|
2019-06-17 14:43:31 +01:00
|
|
|
if (*version == '.')
|
|
|
|
++version;
|
2020-12-02 18:19:28 +00:00
|
|
|
*patch = strtol(version, NULL, 10);
|
2019-06-17 14:43:31 +01:00
|
|
|
}
|
|
|
|
|
2020-08-06 16:20:01 +01:00
|
|
|
static inline uint32_t float_bits_to_uint32(float f)
|
|
|
|
{
|
|
|
|
uint32_t u;
|
|
|
|
memcpy(&u, &f, sizeof(u));
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
2021-02-06 09:51:58 +00:00
|
|
|
static inline size_t vkd3d_wcslen(const WCHAR *wstr)
|
2020-09-02 08:55:49 +01:00
|
|
|
{
|
|
|
|
size_t length = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2021-02-06 09:51:58 +00:00
|
|
|
if (!wstr[length])
|
2020-09-02 08:55:49 +01:00
|
|
|
return length;
|
|
|
|
|
|
|
|
length += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 17:40:09 +01:00
|
|
|
static inline void *void_ptr_offset(void *ptr, size_t offset)
|
|
|
|
{
|
|
|
|
return ((char*)ptr) + offset;
|
|
|
|
}
|
|
|
|
|
2021-08-31 10:26:01 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define VKD3D_THREAD_LOCAL __declspec(thread)
|
|
|
|
#else
|
|
|
|
#define VKD3D_THREAD_LOCAL __thread
|
|
|
|
#endif
|
|
|
|
|
2022-02-02 12:37:01 +00:00
|
|
|
static inline uint64_t vkd3d_get_current_time_ns(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
LARGE_INTEGER li, lf;
|
|
|
|
uint64_t whole, part;
|
|
|
|
QueryPerformanceCounter(&li);
|
|
|
|
QueryPerformanceFrequency(&lf);
|
|
|
|
whole = (li.QuadPart / lf.QuadPart) * 1000000000;
|
|
|
|
part = ((li.QuadPart % lf.QuadPart) * 1000000000) / lf.QuadPart;
|
|
|
|
return whole + part;
|
|
|
|
#else
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
|
|
|
|
return ts.tv_sec * 1000000000ll + ts.tv_nsec;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-21 11:57:24 +01:00
|
|
|
#endif /* __VKD3D_COMMON_H */
|