2018-10-04 05:14:51 +08:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2018 Yuxuan Shui <yshuiv7@gmail.com>
|
2018-09-30 06:37:15 +08:00
|
|
|
#pragma once
|
2019-02-21 22:15:25 +08:00
|
|
|
#include <assert.h>
|
2018-09-30 06:37:15 +08:00
|
|
|
#include <ctype.h>
|
2019-03-30 17:07:21 +08:00
|
|
|
#include <limits.h>
|
2018-12-25 12:11:29 +08:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdbool.h>
|
2018-09-30 06:37:15 +08:00
|
|
|
#include <stddef.h>
|
2018-12-25 12:11:29 +08:00
|
|
|
#include <stdio.h>
|
2019-02-21 22:15:25 +08:00
|
|
|
#include <stdlib.h>
|
2018-12-16 01:42:37 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-04-19 07:11:54 +08:00
|
|
|
#include <test.h>
|
|
|
|
|
2018-12-16 03:11:46 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2018-12-25 12:11:29 +08:00
|
|
|
#define ARR_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
|
2018-11-02 21:57:19 +08:00
|
|
|
|
|
|
|
#ifdef __FAST_MATH__
|
|
|
|
#warning Use of -ffast-math can cause rendering error or artifacts, \
|
|
|
|
therefore it is not recommended.
|
|
|
|
#endif
|
|
|
|
|
2018-11-03 22:02:58 +08:00
|
|
|
#ifdef __clang__
|
|
|
|
__attribute__((optnone))
|
|
|
|
#else
|
2018-11-02 21:57:19 +08:00
|
|
|
__attribute__((optimize("-fno-fast-math")))
|
2018-11-03 22:02:58 +08:00
|
|
|
#endif
|
2018-12-25 12:11:29 +08:00
|
|
|
static inline bool
|
|
|
|
safe_isnan(double a) {
|
2019-03-30 17:07:12 +08:00
|
|
|
return __builtin_isnan(a);
|
2018-11-02 21:57:19 +08:00
|
|
|
}
|
2018-10-21 09:58:37 +08:00
|
|
|
|
2019-03-17 21:44:26 +08:00
|
|
|
#define CASESTRRET(s) \
|
|
|
|
case s: return #s
|
|
|
|
|
2019-07-25 09:27:02 +08:00
|
|
|
/// Same as assert(false), but make sure we abort _even in release builds_.
|
2019-02-19 05:08:21 +08:00
|
|
|
/// Silence compiler warning caused by release builds making some code paths reachable.
|
2019-02-21 22:15:25 +08:00
|
|
|
#define BUG() \
|
|
|
|
do { \
|
|
|
|
assert(false); \
|
|
|
|
abort(); \
|
|
|
|
} while (0)
|
2019-03-31 06:58:08 +08:00
|
|
|
#define CHECK_EXPR(...) ((void)0)
|
2019-02-21 22:15:25 +08:00
|
|
|
/// Same as assert, but evaluates the expression even in release builds
|
2019-03-30 17:07:21 +08:00
|
|
|
#define CHECK(expr) \
|
2019-02-21 22:15:25 +08:00
|
|
|
do { \
|
2019-07-25 09:27:02 +08:00
|
|
|
auto _ = (expr); \
|
|
|
|
/* make sure the original expression appears in the assertion message */ \
|
2019-03-31 06:58:08 +08:00
|
|
|
assert((CHECK_EXPR(expr), _)); \
|
|
|
|
(void)_; \
|
2019-02-21 22:15:25 +08:00
|
|
|
} while (0)
|
2019-02-19 05:08:21 +08:00
|
|
|
|
2019-07-25 09:27:02 +08:00
|
|
|
/// Asserts that var is within [lower, upper]. Silence compiler warning about expressions
|
|
|
|
/// being always true or false.
|
|
|
|
#define ASSERT_IN_RANGE(var, lower, upper) \
|
|
|
|
do { \
|
2019-10-12 22:12:17 +08:00
|
|
|
auto __tmp attr_unused = (var); \
|
2019-07-25 09:27:02 +08:00
|
|
|
_Pragma("GCC diagnostic push"); \
|
|
|
|
_Pragma("GCC diagnostic ignored \"-Wtype-limits\""); \
|
|
|
|
assert(__tmp >= lower); \
|
|
|
|
assert(__tmp <= upper); \
|
|
|
|
_Pragma("GCC diagnostic pop"); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/// Asserts that var >= lower. Silence compiler warning about expressions
|
|
|
|
/// being always true or false.
|
|
|
|
#define ASSERT_GEQ(var, lower) \
|
|
|
|
do { \
|
2019-10-12 22:12:17 +08:00
|
|
|
auto __tmp attr_unused = (var); \
|
2019-07-25 09:27:02 +08:00
|
|
|
_Pragma("GCC diagnostic push"); \
|
|
|
|
_Pragma("GCC diagnostic ignored \"-Wtype-limits\""); \
|
|
|
|
assert(__tmp >= lower); \
|
|
|
|
_Pragma("GCC diagnostic pop"); \
|
|
|
|
} while (0)
|
|
|
|
|
2019-03-30 17:07:21 +08:00
|
|
|
// Some macros for checked cast
|
2019-03-31 06:57:29 +08:00
|
|
|
// Note these macros are not complete, as in, they won't work for every integer types. But
|
2019-10-24 02:27:30 +08:00
|
|
|
// they are good enough for our use cases.
|
2019-03-30 17:07:21 +08:00
|
|
|
|
|
|
|
#define to_int_checked(val) \
|
|
|
|
({ \
|
2019-03-31 06:57:29 +08:00
|
|
|
int64_t tmp = (val); \
|
2019-07-25 09:27:02 +08:00
|
|
|
ASSERT_IN_RANGE(tmp, INT_MIN, INT_MAX); \
|
2019-03-30 17:07:21 +08:00
|
|
|
(int)tmp; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define to_char_checked(val) \
|
|
|
|
({ \
|
2019-03-31 06:57:29 +08:00
|
|
|
int64_t tmp = (val); \
|
2019-07-25 09:27:02 +08:00
|
|
|
ASSERT_IN_RANGE(tmp, CHAR_MIN, CHAR_MAX); \
|
2019-03-30 17:07:21 +08:00
|
|
|
(char)tmp; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define to_u16_checked(val) \
|
|
|
|
({ \
|
|
|
|
auto tmp = (val); \
|
2019-07-25 09:27:02 +08:00
|
|
|
ASSERT_IN_RANGE(tmp, 0, UINT16_MAX); \
|
2019-03-30 17:07:21 +08:00
|
|
|
(uint16_t) tmp; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define to_i16_checked(val) \
|
|
|
|
({ \
|
2019-03-31 06:57:29 +08:00
|
|
|
int64_t tmp = (val); \
|
2019-07-25 09:27:02 +08:00
|
|
|
ASSERT_IN_RANGE(tmp, INT16_MIN, INT16_MAX); \
|
2019-03-30 17:07:21 +08:00
|
|
|
(int16_t) tmp; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define to_u32_checked(val) \
|
|
|
|
({ \
|
|
|
|
auto tmp = (val); \
|
2019-10-12 22:12:17 +08:00
|
|
|
int64_t max attr_unused = UINT32_MAX; /* silence clang tautological \
|
2019-03-31 06:57:29 +08:00
|
|
|
comparison warning*/ \
|
2019-07-25 09:27:02 +08:00
|
|
|
ASSERT_IN_RANGE(tmp, 0, max); \
|
2019-03-30 17:07:21 +08:00
|
|
|
(uint32_t) tmp; \
|
|
|
|
})
|
2018-10-21 09:58:37 +08:00
|
|
|
/**
|
|
|
|
* Normalize an int value to a specific range.
|
|
|
|
*
|
|
|
|
* @param i int value to normalize
|
|
|
|
* @param min minimal value
|
|
|
|
* @param max maximum value
|
|
|
|
* @return normalized value
|
|
|
|
*/
|
2018-12-25 12:11:29 +08:00
|
|
|
static inline int attr_const normalize_i_range(int i, int min, int max) {
|
|
|
|
if (i > max)
|
|
|
|
return max;
|
|
|
|
if (i < min)
|
|
|
|
return min;
|
|
|
|
return i;
|
2018-10-21 09:58:37 +08:00
|
|
|
}
|
|
|
|
|
2019-03-30 17:07:21 +08:00
|
|
|
#define min2(a, b) ((a) > (b) ? (b) : (a))
|
|
|
|
#define max2(a, b) ((a) > (b) ? (a) : (b))
|
2018-10-21 09:58:37 +08:00
|
|
|
|
2019-03-30 17:07:21 +08:00
|
|
|
/// clamp `val` into interval [min, max]
|
|
|
|
#define clamp(val, min, max) max2(min2(val, max), min)
|
2018-10-21 09:58:37 +08:00
|
|
|
|
2019-02-04 00:41:04 +08:00
|
|
|
static inline int attr_const popcountl(unsigned long a) {
|
|
|
|
return __builtin_popcountl(a);
|
|
|
|
}
|
|
|
|
|
2018-10-21 09:58:37 +08:00
|
|
|
/**
|
|
|
|
* Normalize a double value to a specific range.
|
|
|
|
*
|
|
|
|
* @param d double value to normalize
|
|
|
|
* @param min minimal value
|
|
|
|
* @param max maximum value
|
|
|
|
* @return normalized value
|
|
|
|
*/
|
2018-12-25 12:11:29 +08:00
|
|
|
static inline double attr_const normalize_d_range(double d, double min, double max) {
|
|
|
|
if (d > max)
|
|
|
|
return max;
|
|
|
|
if (d < min)
|
|
|
|
return min;
|
|
|
|
return d;
|
2018-10-21 09:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize a double value to 0.\ 0 - 1.\ 0.
|
|
|
|
*
|
|
|
|
* @param d double value to normalize
|
|
|
|
* @return normalized value
|
|
|
|
*/
|
2018-12-25 12:11:29 +08:00
|
|
|
static inline double attr_const normalize_d(double d) {
|
|
|
|
return normalize_d_range(d, 0.0, 1.0);
|
2018-10-21 09:58:37 +08:00
|
|
|
}
|
|
|
|
|
2019-02-21 22:15:25 +08:00
|
|
|
attr_noret void
|
|
|
|
report_allocation_failure(const char *func, const char *file, unsigned int line);
|
2018-12-20 22:13:14 +08:00
|
|
|
|
2018-12-16 01:42:37 +08:00
|
|
|
/**
|
|
|
|
* @brief Quit if the passed-in pointer is empty.
|
|
|
|
*/
|
|
|
|
static inline void *
|
2018-12-20 22:13:14 +08:00
|
|
|
allocchk_(const char *func_name, const char *file, unsigned int line, void *ptr) {
|
2018-12-25 12:11:29 +08:00
|
|
|
if (unlikely(!ptr)) {
|
|
|
|
report_allocation_failure(func_name, file, line);
|
|
|
|
}
|
|
|
|
return ptr;
|
2018-09-30 06:37:15 +08:00
|
|
|
}
|
|
|
|
|
2018-12-16 01:42:37 +08:00
|
|
|
/// @brief Wrapper of allocchk_().
|
2018-12-20 22:13:14 +08:00
|
|
|
#define allocchk(ptr) allocchk_(__func__, __FILE__, __LINE__, ptr)
|
2018-12-16 01:42:37 +08:00
|
|
|
|
|
|
|
/// @brief Wrapper of malloc().
|
2018-12-25 12:11:29 +08:00
|
|
|
#define cmalloc(type) ((type *)allocchk(malloc(sizeof(type))))
|
2018-12-16 02:47:21 +08:00
|
|
|
|
|
|
|
/// @brief Wrapper of malloc() that takes a size
|
|
|
|
#define cvalloc(size) allocchk(malloc(size))
|
2018-12-16 01:42:37 +08:00
|
|
|
|
|
|
|
/// @brief Wrapper of calloc().
|
2019-03-30 17:07:21 +08:00
|
|
|
#define ccalloc(nmemb, type) \
|
|
|
|
({ \
|
|
|
|
auto tmp = (nmemb); \
|
2019-07-25 09:27:02 +08:00
|
|
|
ASSERT_GEQ(tmp, 0); \
|
2019-03-30 17:07:21 +08:00
|
|
|
((type *)allocchk(calloc((size_t)tmp, sizeof(type)))); \
|
|
|
|
})
|
2018-09-30 06:37:15 +08:00
|
|
|
|
2018-12-16 01:42:37 +08:00
|
|
|
/// @brief Wrapper of ealloc().
|
2019-03-30 17:07:21 +08:00
|
|
|
#define crealloc(ptr, nmemb) \
|
|
|
|
({ \
|
|
|
|
auto tmp = (nmemb); \
|
2019-07-25 09:27:02 +08:00
|
|
|
ASSERT_GEQ(tmp, 0); \
|
2019-03-30 17:07:21 +08:00
|
|
|
((__typeof__(ptr))allocchk(realloc((ptr), (size_t)tmp * sizeof(*(ptr))))); \
|
|
|
|
})
|
2018-09-30 06:37:15 +08:00
|
|
|
|
|
|
|
/// RC_TYPE generates a reference counted type from `type`
|
|
|
|
///
|
|
|
|
/// parameters:
|
|
|
|
/// name = the generated type will be called `name`_t.
|
|
|
|
/// ctor = the constructor of `type`, will be called when
|
|
|
|
/// a value of `type` is created. should take one
|
|
|
|
/// argument of `type *`.
|
|
|
|
/// dtor = the destructor. will be called when all reference
|
|
|
|
/// is gone. has same signature as ctor
|
|
|
|
/// Q = function qualifier. this is the qualifier that
|
|
|
|
/// will be put before generated functions
|
|
|
|
//
|
|
|
|
/// functions generated:
|
|
|
|
/// `name`_new: create a new reference counted object of `type`
|
|
|
|
/// `name`_ref: increment the reference counter, return a
|
|
|
|
/// reference to the object
|
|
|
|
/// `name`_unref: decrement the reference counter. take a `type **`
|
|
|
|
/// because it needs to nullify the reference.
|
2018-12-25 12:11:29 +08:00
|
|
|
#define RC_TYPE(type, name, ctor, dtor, Q) \
|
|
|
|
typedef struct { \
|
|
|
|
type inner; \
|
|
|
|
int ref_count; \
|
|
|
|
} name##_internal_t; \
|
|
|
|
typedef type name##_t; \
|
|
|
|
Q type *name##_new(void) { \
|
|
|
|
name##_internal_t *ret = cmalloc(name##_internal_t); \
|
|
|
|
ctor((type *)ret); \
|
|
|
|
ret->ref_count = 1; \
|
|
|
|
return (type *)ret; \
|
|
|
|
} \
|
|
|
|
Q type *name##_ref(type *a) { \
|
|
|
|
__auto_type b = (name##_internal_t *)a; \
|
|
|
|
b->ref_count++; \
|
|
|
|
return a; \
|
|
|
|
} \
|
|
|
|
Q void name##_unref(type **a) { \
|
|
|
|
__auto_type b = (name##_internal_t *)*a; \
|
|
|
|
if (!b) \
|
|
|
|
return; \
|
|
|
|
b->ref_count--; \
|
|
|
|
if (!b->ref_count) { \
|
|
|
|
dtor((type *)b); \
|
|
|
|
free(b); \
|
|
|
|
} \
|
|
|
|
*a = NULL; \
|
|
|
|
}
|
2018-09-30 06:37:15 +08:00
|
|
|
|
|
|
|
/// Generate prototypes for functions generated by RC_TYPE
|
2018-12-25 12:11:29 +08:00
|
|
|
#define RC_TYPE_PROTO(type, name) \
|
|
|
|
typedef type name##_t; \
|
|
|
|
type *name##_new(void); \
|
|
|
|
void name##_ref(type *a); \
|
|
|
|
void name##_unref(type **a);
|
|
|
|
|
|
|
|
// vim: set noet sw=8 ts=8 :
|