rabbitizer/include/common/Utils.h

63 lines
1.6 KiB
C
Raw Normal View History

2022-06-04 00:19:58 +00:00
/* SPDX-FileCopyrightText: © 2022 Decompollaborate */
/* SPDX-License-Identifier: MIT */
2022-06-04 02:39:10 +00:00
#ifndef RABBITIZER_UTILS_H
#define RABBITIZER_UTILS_H
2022-06-04 05:24:59 +00:00
#include <stddef.h>
2022-06-04 02:39:10 +00:00
#include <stdint.h>
2022-06-04 00:19:58 +00:00
2022-06-06 05:59:38 +00:00
#if !defined(__GNUC__) && !defined(__clang__)
#define __attribute__(x)
#endif
#if __STDC_VERSION__ >= 202300L
#define DEPRECATED(reason) [[deprecated (reason)]]
#define FALLTHROUGH [[fallthrough]]
#define NODISCARD(reason) [[nodiscard (reason)]]
#define NORETURN [[noreturn]]
#define UNUSED [[maybe_unused]]
#else
#define DEPRECATED(reason) __attribute__((deprecated (reason)))
#define FALLTHROUGH __attribute__((fallthrough))
#define NODISCARD(reason) __attribute__((warn_unused_result))
#define NORETURN _Noreturn
#define UNUSED __attribute__((unused))
#endif
2022-06-10 18:01:00 +00:00
#if defined(_MSC_VER)
# define UNREACHABLE __assume(0)
#else
# define UNREACHABLE __builtin_unreachable()
#endif
2022-06-06 05:59:38 +00:00
#define PURE __attribute__((pure))
2022-06-04 00:19:58 +00:00
#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof(arr[0]))
2022-06-04 02:39:10 +00:00
/*
* the SHIFT macros take a value, a shift amount, and a width.
*
* For the left shift, the lower bits of the value are masked,
* then shifted left.
*
* For the right shift, the value is shifted right, then the lower bits
* are masked.
*
* (NOTE: SHIFTL(v, 0, 32) won't work, just use an assignment)
*
*/
#define SHIFTL(v, s, w) (((v) & ((1 << (w)) - 1)) << (s))
#define SHIFTR(v, s, w) (((v) >> (s)) & ((1 << (w)) - 1))
#define BITREPACK(fullword, v, s, w) (SHIFTL((self)->word, (s)+(w), 32-((s)+(w))) | SHIFTL((v), (s), (w)) | SHIFTL((self)->word, 0, (s)))
2022-06-04 02:39:10 +00:00
int32_t RabbitizerUtils_From2Complement(uint32_t number, int bits);
2022-06-04 05:24:59 +00:00
size_t RabbitizerUtils_CharFill(char *dst, int count, char fillchar);
2022-06-04 02:39:10 +00:00
2022-06-04 00:19:58 +00:00
#endif