Last active
March 26, 2024 18:16
-
-
Save g-berthiaume/84160d113e5639e480eae9a0ef3af1ba to your computer and use it in GitHub Desktop.
godbolt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Did you know that [Compiler Explorer](godbolt.org) allows you to include any file | |
// from the internet ? | |
// | |
// Just add the following line to your C source: | |
// | |
// #include <https://gist.githubusercontent.com/g-berthiaume/84160d113e5639e480eae9a0ef3af1ba/raw/1fde54c83ec3ff74dad536e0bb36d14d4db4e36b/godbolt_utils.h> | |
// | |
// This file's purpose is to create a collection of utilities for increasing my productivity on godbolt. | |
// This file is unlicensed. Do what you want with it. :^) | |
// g-berthiaume - 2024 | |
#include <stddef.h> //< offsetof, ptrdiff_t and size_t | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <string.h> | |
#include <assert.h> | |
// | |
// types | |
// | |
typedef uint8_t u8; | |
typedef uint16_t u16; | |
typedef uint32_t u32; | |
typedef uint64_t u64; | |
typedef int8_t i8; | |
typedef int16_t i16; | |
typedef int32_t i32; | |
typedef int64_t i64; | |
#define U8_MAX UINT8_MAX | |
#define U16_MAX UINT16_MAX | |
#define U32_MAX UINT32_MAX | |
#define U64_MAX UINT64_MAX | |
#define U8_MIN (0) | |
#define U16_MIN (0) | |
#define U32_MIN (0) | |
#define U64_MIN (0) | |
#define I8_MAX INT8_MAX | |
#define I16_MAX INT16_MAX | |
#define I32_MAX INT32_MAX | |
#define I64_MAX INT64_MAX | |
#define I8_MIN INT8_MIN | |
#define I16_MIN INT16_MIN | |
#define I32_MIN INT32_MIN | |
#define I64_MIN INT64_MIN | |
// | |
// utils | |
// | |
#define ABS(a) (((a) < 0) ? -(a) : (a)) | |
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
#define MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
#define MIN3(a, b, c) (MIN(MIN(a, b), c)) | |
#define MAX3(a, b, c) (MAX(MAX(a, b), c)) | |
#define MIN4(a, b, c, d) (MIN(MIN(MIN(a, b), c), d)) | |
#define MAX4(a, b, c, d) (MAX(MAX(MAX(a, b), c), d)) | |
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) | |
#define CLAMP_TOP(x, top) MIN((x), (top)) | |
#define CLAMP_BOTTOM(x, bottom) MAX((x), (bottom)) | |
#define IS_IN_RANGE(_x_, _min_, _max_) ((((_x_) < (_min_)) || ((_x_) > (_max_))) ? 0 : 1) | |
#define IS_BETWEEN(_number_, _min_, _max_) (((_number_) > (_min_)) && ((_number_) < (_max_))) | |
#define IS_WITHIN(_number_, _min_, _max_) (((_number_) >= (_min_)) && ((_number_) <= (_max_))) | |
#define COUNT_OF(...) (ptrdiff_t)(sizeof(__VA_ARGS__) / sizeof(*__VA_ARGS__)) | |
#define SIZE_OF(type) ((ptrdiff_t)sizeof(type)) | |
#define printfln(fmt, ...) printf(fmt "\r\n", __VA_ARGS__) | |
// | |
// slices | |
// | |
#define SLICE_TYPEDEF(_data_type_, _new_slice_type_) \ | |
typedef struct \ | |
{ \ | |
_data_type_ *data; \ | |
ptrdiff_t len; \ | |
} _new_slice_type_; | |
#define SLICE(...) \ | |
{ /* v like COUNT_OF(), but that can accept compound literals */ \ | |
.data = (__VA_ARGS__), .len = ((ptrdiff_t)(sizeof(__VA_ARGS__) / sizeof(*__VA_ARGS__))) \ | |
} | |
SLICE_TYPEDEF(char, SliceChar_t) | |
SLICE_TYPEDEF(unsigned char, SliceUChar_t) | |
SLICE_TYPEDEF(signed char, SliceSChar_t) | |
SLICE_TYPEDEF(short, SliceShort_t) | |
SLICE_TYPEDEF(unsigned short, SliceUShort_t) | |
SLICE_TYPEDEF(int, SliceInt_t) | |
SLICE_TYPEDEF(unsigned int, SliceUInt_t) | |
SLICE_TYPEDEF(long, SliceLong_t) | |
SLICE_TYPEDEF(unsigned long, SliceULong_t) | |
SLICE_TYPEDEF(long long, SliceLongLong_t) | |
SLICE_TYPEDEF(unsigned long long, SliceULongLong_t) | |
SLICE_TYPEDEF(float, SliceFloat_t) | |
SLICE_TYPEDEF(double, SliceDouble_t) | |
SLICE_TYPEDEF(int8_t, SliceI8_t) | |
SLICE_TYPEDEF(uint8_t, SliceU8_t) | |
SLICE_TYPEDEF(int16_t, SliceI16_t) | |
SLICE_TYPEDEF(uint16_t, SliceU16_t) | |
SLICE_TYPEDEF(int32_t, SliceI32_t) | |
SLICE_TYPEDEF(uint32_t, SliceU32_t) | |
SLICE_TYPEDEF(int64_t, SliceI64_t) | |
SLICE_TYPEDEF(uint64_t, SliceU64_t) | |
SLICE_TYPEDEF(size_t, SliceSize_t) | |
SLICE_TYPEDEF(ptrdiff_t, SlicePtrdiff_t) | |
SLICE_TYPEDEF(intptr_t, SliceIntptr_t) | |
SLICE_TYPEDEF(uintptr_t, SliceUIntptr_t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment