ZXC 0.15.0
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
zxc_deps.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause */
2/*
3 * ZXC - High-performance lossless compression
4 *
5 * Copyright (c) Bertrand Lebonnois and contributors.
6 */
7
27#ifndef ZXC_DEPS_H
28#define ZXC_DEPS_H
29
47#include <assert.h>
48#include <limits.h>
49#include <stdint.h>
50#include <stdlib.h>
51#include <string.h>
/* end of Standard Headers */
53
67#ifndef ZXC_MALLOC
68#define ZXC_MALLOC(size) malloc(size)
69#endif
70
73#ifndef ZXC_CALLOC
74#define ZXC_CALLOC(nmemb, size) calloc(nmemb, size)
75#endif
76
79#ifndef ZXC_REALLOC
80#define ZXC_REALLOC(ptr, size) realloc(ptr, size)
81#endif
82
85#ifndef ZXC_FREE
86#define ZXC_FREE(ptr) free(ptr)
87#endif
88
/* end of Heap Allocator Abstraction */
90
106#if defined(ZXC_ALIGNED_MALLOC) != defined(ZXC_ALIGNED_FREE)
107#error "ZXC_ALIGNED_MALLOC and ZXC_ALIGNED_FREE are overridden together"
108#endif
109
110#ifndef ZXC_ALIGNED_MALLOC
111
113static inline void* zxc_aligned_malloc(const size_t size, const size_t alignment) {
114#if defined(_WIN32)
115 return _aligned_malloc(size, alignment);
116#else
117 void* ptr = NULL;
118 if (posix_memalign(&ptr, alignment, size) != 0) return NULL;
119 return ptr;
120#endif
121}
122
124static inline void zxc_aligned_free(void* ptr) {
125#if defined(_WIN32)
126 _aligned_free(ptr);
127#else
128 free(ptr);
129#endif
130}
131
134#define ZXC_ALIGNED_MALLOC(size, alignment) zxc_aligned_malloc(size, alignment)
137#define ZXC_ALIGNED_FREE(ptr) zxc_aligned_free(ptr)
138
139#endif // ZXC_ALIGNED_MALLOC
140
/* end of Aligned Allocator Abstraction */
142
150#if defined(ZXC_POPCOUNT32) != defined(ZXC_POPCOUNT64)
151#error "ZXC_POPCOUNT32 and ZXC_POPCOUNT64 are overridden together"
152#endif
153
154#ifndef ZXC_POPCOUNT32
155#if defined(__GNUC__) || defined(__clang__)
156#define ZXC_POPCOUNT32(v) __builtin_popcount(v)
157#define ZXC_POPCOUNT64(v) __builtin_popcountll(v)
158#else
160static inline int zxc_popcount32(const uint32_t v) {
161 uint32_t x = v - ((v >> 1) & 0x55555555U);
162 x = (x & 0x33333333U) + ((x >> 2) & 0x33333333U);
163 x = (x + (x >> 4)) & 0x0F0F0F0FU;
164 return (int)((x * 0x01010101U) >> 24);
165}
166static inline int zxc_popcount64(const uint64_t v) {
167 return zxc_popcount32((uint32_t)v) + zxc_popcount32((uint32_t)(v >> 32));
168}
169#define ZXC_POPCOUNT32(v) zxc_popcount32(v)
170#define ZXC_POPCOUNT64(v) zxc_popcount64(v)
171#endif
172#endif // ZXC_POPCOUNT32
173
/* end of Population Count */
175
/* end of addtogroup internal */
177
178#endif /* ZXC_DEPS_H */
static void * zxc_aligned_malloc(const size_t size, const size_t alignment)
Default cache-line-aligned allocator; release with zxc_aligned_free.
Definition zxc_deps.h:113
static int zxc_popcount32(const uint32_t v)
Portable SWAR popcount (MSVC).
Definition zxc_deps.h:160
static void zxc_aligned_free(void *ptr)
Counterpart of zxc_aligned_malloc; NULL is a no-op.
Definition zxc_deps.h:124