2016-09-21 03:57:24 -07:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Józef Kucia for CodeWeavers
|
|
|
|
*
|
2017-06-16 12:05:54 -07: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 03:57:24 -07:00
|
|
|
*
|
2017-06-16 12:05:54 -07: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 03:57:24 -07:00
|
|
|
*
|
2017-06-16 12:05:54 -07: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 03:57:24 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __VKD3D_COMMON_H
|
|
|
|
#define __VKD3D_COMMON_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "vkd3d_windows.h"
|
|
|
|
|
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
|
|
|
|
|
2017-09-14 05:57:09 -07:00
|
|
|
#define DIV_ROUND_UP(a, b) ((a) % (b) == 0 ? (a) / (b) : (a) / (b) + 1)
|
|
|
|
|
2018-11-08 08:19:31 -08:00
|
|
|
#define STATIC_ASSERT(e) extern void __VKD3D_STATIC_ASSERT__(int [(e) ? 1 : -1])
|
|
|
|
|
2018-11-08 08:19:32 -08:00
|
|
|
#define MEMBER_SIZE(t, m) sizeof(((t *)0)->m)
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline size_t align(size_t addr, size_t alignment)
|
|
|
|
{
|
|
|
|
return (addr + (alignment - 1)) & ~(alignment - 1);
|
|
|
|
}
|
|
|
|
|
2016-10-10 02:22:50 -07: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 06:04:30 -07:00
|
|
|
static inline unsigned int vkd3d_popcount(unsigned int v)
|
|
|
|
{
|
2017-09-08 06:04:30 -07:00
|
|
|
#ifdef HAVE_BUILTIN_POPCOUNT
|
|
|
|
return __builtin_popcount(v);
|
|
|
|
#else
|
2017-09-08 06:04:30 -07:00
|
|
|
v -= (v >> 1) & 0x55555555;
|
|
|
|
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
|
|
|
|
return (((v + (v >> 4)) & 0x0f0f0f0f) * 0x01010101) >> 24;
|
2017-09-08 06:04:30 -07:00
|
|
|
#endif
|
2017-09-08 06:04:30 -07:00
|
|
|
}
|
|
|
|
|
2018-01-11 08:03:44 -08:00
|
|
|
/* Undefined for x == 0. */
|
|
|
|
static inline unsigned int vkd3d_log2i(unsigned int x)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_BUILTIN_CLZ
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-28 00:42:49 -07:00
|
|
|
#ifndef _WIN32
|
|
|
|
# if HAVE_SYNC_ADD_AND_FETCH
|
2016-10-10 02:22:50 -07:00
|
|
|
static inline LONG InterlockedIncrement(LONG volatile *x)
|
2016-09-21 03:57:24 -07:00
|
|
|
{
|
|
|
|
return __sync_add_and_fetch(x, 1);
|
|
|
|
}
|
2016-09-28 00:42:49 -07:00
|
|
|
# else
|
|
|
|
# error "InterlockedIncrement not implemented for this platform"
|
|
|
|
# endif /* HAVE_SYNC_ADD_AND_FETCH */
|
2016-09-21 03:57:24 -07:00
|
|
|
|
2016-09-28 00:42:49 -07:00
|
|
|
# if HAVE_SYNC_SUB_AND_FETCH
|
2016-10-10 02:22:50 -07:00
|
|
|
static inline LONG InterlockedDecrement(LONG volatile *x)
|
2016-09-21 03:57:24 -07:00
|
|
|
{
|
|
|
|
return __sync_sub_and_fetch(x, 1);
|
|
|
|
}
|
2016-09-28 00:42:49 -07:00
|
|
|
# else
|
|
|
|
# error "InterlockedDecrement not implemented for this platform"
|
|
|
|
# endif
|
|
|
|
#endif /* _WIN32 */
|
2016-09-21 03:57:24 -07:00
|
|
|
|
|
|
|
#endif /* __VKD3D_COMMON_H */
|