tommytypes.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2// Copyright (C) 2010 Andrea Mazzoleni
3
8#ifndef TOMMY_TYPES_H
9#define TOMMY_TYPES_H
10
11/******************************************************************************/
12/* types */
13
14#include <stddef.h>
15
16#ifdef _MSC_VER
17#include <limits.h>
18typedef unsigned tommy_uint32_t;
19typedef unsigned _int64 tommy_uint64_t;
20typedef size_t tommy_uintptr_t;
21#ifdef _WIN64
22#define TOMMY_SIZE_BIT 64
23#define TOMMY_SIZE_MAX _UI64_MAX
24typedef unsigned _int64 tommy_size_t;
25typedef _int64 tommy_ssize_t;
26#else
27#define TOMMY_SIZE_BIT 32
28#define TOMMY_SIZE_MAX UINT_MAX
29typedef unsigned tommy_size_t;
30typedef int tommy_ssize_t;
31#endif
32#else
33#include <stdint.h>
34typedef uint32_t tommy_uint32_t;
35typedef uint64_t tommy_uint64_t;
36typedef uintptr_t tommy_uintptr_t;
37#if SIZE_MAX == UINT64_MAX
38#define TOMMY_SIZE_BIT 64
39#define TOMMY_SIZE_MAX SIZE_MAX
40typedef uint64_t tommy_size_t;
41typedef int64_t tommy_ssize_t;
42#elif SIZE_MAX == UINT32_MAX
43#define TOMMY_SIZE_BIT 32
44#define TOMMY_SIZE_MAX SIZE_MAX
45typedef uint32_t tommy_size_t;
46typedef int32_t tommy_ssize_t;
47#else
48#error Unsupported SIZE_MAX
49#endif
50#endif
51
52typedef ptrdiff_t tommy_ptrdiff_t;
53typedef int tommy_bool_t;
62
75#ifdef __cplusplus
76#define tommy_cast(type, value) static_cast<type>(value)
77#else
78#define tommy_cast(type, value) (value)
79#endif
80
81/******************************************************************************/
82/* heap */
83
84/* by default uses malloc/calloc/realloc/free */
85
140#if !defined(tommy_malloc) || !defined(tommy_calloc) || !defined(tommy_realloc) || !defined(tommy_free)
141#include <stdlib.h>
142#endif
143#if !defined(tommy_malloc)
144#define tommy_malloc malloc
145#endif
146#if !defined(tommy_calloc)
147#define tommy_calloc calloc
148#endif
149#if !defined(tommy_realloc)
150#define tommy_realloc realloc
151#endif
152#if !defined(tommy_free)
153#define tommy_free free
154#endif
155
156/******************************************************************************/
157/* modifiers */
158
163#if !defined(TOMMY_API)
164#define TOMMY_API
165#endif
166
170#if !defined(tommy_inline)
171#if defined(_MSC_VER) || defined(__GNUC__)
172#define tommy_inline static __inline
173#else
174#define tommy_inline static
175#endif
176#endif
177
181#if !defined(tommy_restrict)
182#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
183#define tommy_restrict restrict
184#elif defined(_MSC_VER) || defined(__GNUC__)
185#define tommy_restrict __restrict
186#else
187#define tommy_restrict
188#endif
189#endif
190
194#if !defined(tommy_likely)
195#if defined(__GNUC__)
196#define tommy_likely(x) __builtin_expect(!!(x), 1)
197#else
198#define tommy_likely(x) (x)
199#endif
200#endif
201
205#if !defined(tommy_unlikely)
206#if defined(__GNUC__)
207#define tommy_unlikely(x) __builtin_expect(!!(x), 0)
208#else
209#define tommy_unlikely(x) (x)
210#endif
211#endif
212
213/******************************************************************************/
214/* key/hash */
215
220
225
226/******************************************************************************/
227/* node */
228
242typedef struct tommy_node_struct {
248
254
260 void* data;
261
271
272/******************************************************************************/
273/* compare */
274
302typedef int tommy_compare_func(const void* elem_a, const void* elem_b);
303
334typedef int tommy_search_func(const void* arg, const void* elem);
335
346typedef void tommy_foreach_func(void* elem);
347
353typedef void tommy_foreach_arg_func(void* arg, void* elem);
354
355/******************************************************************************/
356/* bit hacks */
357
358#if defined(_MSC_VER) && !defined(__cplusplus)
359#include <intrin.h>
360#pragma intrinsic(_BitScanReverse)
361#pragma intrinsic(_BitScanForward)
362#if TOMMY_SIZE_BIT == 64
363#pragma intrinsic(_BitScanReverse64)
364#pragma intrinsic(_BitScanForward64)
365#endif
366#endif
367
372#define TOMMY_ILOG2(value) ((value) == 256 ? 8 : (value) == 128 ? 7 : (value) == 64 ? 6 : (value) == 32 ? 5 : (value) == 16 ? 4 : (value) == 8 ? 3 : (value) == 4 ? 2 : (value) == 2 ? 1 : 0)
373
393{
394#if defined(_MSC_VER)
395 unsigned long count;
396 _BitScanReverse(&count, value);
397 return count;
398#elif defined(__GNUC__)
399 /*
400 * GCC implements __builtin_clz(x) as "__builtin_clz(x) = bsr(x) ^ 31"
401 *
402 * Where "x ^ 31 = 31 - x", but gcc does not optimize "31 - __builtin_clz(x)" to bsr(x),
403 * but generates 31 - (bsr(x) xor 31).
404 *
405 * So we write "__builtin_clz(x) ^ 31" instead of "31 - __builtin_clz(x)",
406 * to allow the double xor to be optimized out.
407 */
408 return __builtin_clz(value) ^ 31;
409#else
410 /* find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup */
411 /* from http://graphics.stanford.edu/~seander/bithacks.html */
412 static const unsigned char TOMMY_DE_BRUIJN_INDEX_ILOG2[32] = {
413 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
414 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
415 };
416
417 value |= value >> 1;
418 value |= value >> 2;
419 value |= value >> 4;
420 value |= value >> 8;
421 value |= value >> 16;
422
423 return TOMMY_DE_BRUIJN_INDEX_ILOG2[(tommy_uint32_t)(value * 0x07C4ACDDU) >> 27];
424#endif
425}
426
436{
437#if defined(_MSC_VER) && defined(_WIN64)
438 unsigned long count;
439 _BitScanReverse64(&count, value);
440 return count;
441#elif defined(__GNUC__)
442 return __builtin_clzll(value) ^ 63;
443#else
444 tommy_uint32_t l = value & 0xFFFFFFFFU;
445 tommy_uint32_t h = value >> 32;
446 if (h)
447 return tommy_ilog2_u32(h) + 32;
448 else
449 return tommy_ilog2_u32(l);
450#endif
451}
452
462{
463#if defined(_MSC_VER)
464 unsigned long count;
465 _BitScanForward(&count, value);
466 return count;
467#elif defined(__GNUC__)
468 return __builtin_ctz(value);
469#else
470 /* count the consecutive zero bits (trailing) on the right with multiply and lookup */
471 /* from http://graphics.stanford.edu/~seander/bithacks.html */
472 static const unsigned char TOMMY_DE_BRUIJN_INDEX_CTZ[32] = {
473 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
474 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
475 };
476
477 return TOMMY_DE_BRUIJN_INDEX_CTZ[(tommy_uint32_t)(((value & -value) * 0x077CB531U)) >> 27];
478#endif
479}
480
490{
491#if defined(_MSC_VER) && defined(_WIN64)
492 unsigned long count;
493 _BitScanForward64(&count, value);
494 return count;
495#elif defined(__GNUC__)
496 return __builtin_ctzll(value);
497#else
498 tommy_uint32_t l = value & 0xFFFFFFFFU;
499 tommy_uint32_t h = value >> 32;
500 if (l)
501 return tommy_ctz_u32(l);
502 else
503 return tommy_ctz_u32(h) + 32;
504#endif
505}
506
514{
515 /* round up to the next highest power of 2 */
516 /* from http://graphics.stanford.edu/~seander/bithacks.html */
517
518 --value;
519 value |= value >> 1;
520 value |= value >> 2;
521 value |= value >> 4;
522 value |= value >> 8;
523 value |= value >> 16;
524 ++value;
525
526 return value;
527}
528
536{
537 --value;
538 value |= value >> 1;
539 value |= value >> 2;
540 value |= value >> 4;
541 value |= value >> 8;
542 value |= value >> 16;
543 value |= value >> 32;
544 ++value;
545
546 return value;
547}
548
554tommy_inline int tommy_haszero_u32(tommy_uint32_t value)
555{
556 return ((value - 0x01010101) & ~value & 0x80808080) != 0;
557}
558
564tommy_inline int tommy_haszero_u64(tommy_uint64_t value)
565{
566 return ((value - 0x0101010101010101ULL) & ~value & 0x8080808080808080ULL) != 0;
567}
568
572#if TOMMY_SIZE_BIT == 64
573#define tommy_ilog2 tommy_ilog2_u64
574#define tommy_ctz tommy_ctz_u64
575#define tommy_roundup_pow2 tommy_roundup_pow2_u64
576#else
577#define tommy_ilog2 tommy_ilog2_u32
578#define tommy_ctz tommy_ctz_u32
579#define tommy_roundup_pow2 tommy_roundup_pow2_u32
580#endif
581
582#endif
583
Data structure node.
Definition: tommytypes.h:242
tommy_size_t index
Index of the node.
Definition: tommytypes.h:269
struct tommy_node_struct * next
Next node.
Definition: tommytypes.h:247
struct tommy_node_struct * prev
Previous node.
Definition: tommytypes.h:253
void * data
Pointer to the element containing the node.
Definition: tommytypes.h:260
uint64_t tommy_uint64_t
Generic uint64_t type.
Definition: tommytypes.h:35
int tommy_haszero_u64(tommy_uint64_t value)
Checks if the specified 64-bit word has a byte at 0.
Definition: tommytypes.h:564
struct tommy_node_struct tommy_node
Data structure node.
uint32_t tommy_uint32_t
Generic uint32_t type.
Definition: tommytypes.h:34
void tommy_foreach_func(void *elem)
Foreach function.
Definition: tommytypes.h:346
int tommy_haszero_u32(tommy_uint32_t value)
Checks if the specified word has a byte at 0.
Definition: tommytypes.h:554
tommy_uint_t tommy_ctz_u32(tommy_uint32_t value)
Bit scan forward or trailing zero count.
Definition: tommytypes.h:461
int tommy_search_func(const void *arg, const void *elem)
Search function for elements.
Definition: tommytypes.h:334
tommy_uint64_t tommy_roundup_pow2_u64(tommy_uint64_t value)
Rounds up to the next power of 2 for 64 bits.
Definition: tommytypes.h:535
int tommy_compare_func(const void *elem_a, const void *elem_b)
Compare function for elements.
Definition: tommytypes.h:302
tommy_uint_t tommy_ctz_u64(tommy_uint64_t value)
Bit scan forward or trailing zero count for 64 bits.
Definition: tommytypes.h:489
ptrdiff_t tommy_ptrdiff_t
Generic ptrdiff_t type.
Definition: tommytypes.h:52
uint64_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:40
void tommy_foreach_arg_func(void *arg, void *elem)
Foreach function with an argument.
Definition: tommytypes.h:353
tommy_uint32_t tommy_roundup_pow2_u32(tommy_uint32_t value)
Rounds up to the next power of 2.
Definition: tommytypes.h:513
tommy_uint_t tommy_ilog2_u32(tommy_uint32_t value)
Bit scan reverse or integer log2.
Definition: tommytypes.h:392
int64_t tommy_ssize_t
Generic ssize_t type.
Definition: tommytypes.h:41
tommy_size_t tommy_hash_t
Type used in hashtables to store the hash of an element.
Definition: tommytypes.h:224
tommy_size_t tommy_key_t
Type used in indexed data structures to store the key of an element.
Definition: tommytypes.h:219
tommy_uint32_t tommy_uint_t
Generic unsigned integer type.
Definition: tommytypes.h:61
uintptr_t tommy_uintptr_t
Generic uintptr_t type.
Definition: tommytypes.h:36
tommy_uint_t tommy_ilog2_u64(tommy_uint64_t value)
Bit scan reverse or integer log2 for 64 bits.
Definition: tommytypes.h:435
int tommy_bool_t
Generic boolean type.
Definition: tommytypes.h:53