aboutsummaryrefslogtreecommitdiff
path: root/src/utils.h
blob: 4adf4829abe4804c2a3e9699b2246ee48bcee297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef INCLUDED_UTILS_H
#define INCLUDED_UTILS_H

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#define CTZ __builtin_ctz
#define CLZ __builtin_clz
#define FIRST_LEADING_ZERO __builtin_stdc_first_leading_zero
#define FIRST_LEADING_ONE  __builtin_stdc_first_leading_one

#define STRINGIFY(ARG) #ARG
#define TO_STRING(ARG) STRINGIFY (ARG)
#define LOG(MSG, ...)                                                          \
  fprintf (stderr, __FILE__ ":" TO_STRING (__LINE__) ": " MSG "\n", __VA_ARGS__)
#define  INFO(MSG, ...) LOG("info: "  MSG __VA_OPT__(,) __VA_ARGS__)
#define ERROR(MSG, ...) LOG("error: " MSG __VA_OPT__(,) __VA_ARGS__)
#define DIE(CODE, MSG, ...) do {                \
    ERROR (MSG __VA_OPT__(,) __VA_ARGS__);      \
    exit (CODE);                                \
  } while (0)

#define UNUSED(X) ((void) (X))
#define UNREACHABLE() ASSERT (!"reached the unreachable...")
#define TODO(STR) fprintf (stderr, "%s:%d: warning: TODO "STR"\n", __FILE__, __LINE__)
// #define TODO(...)
#define REACHED(FMT, ...) fprintf (stderr, "%s:%d: info: %s: reached: "FMT"\n", __FILE__, __LINE__, __func__ __VA_OPT__(,) __VA_ARGS__)

#define MALLOC malloc
#define REALLOC realloc
#define FREE free
#define ASSERT assert

#define PRINT_EXP(FMT, EXP) printf (#EXP" = "FMT"\n", EXP)

#define MAX(A, B) ((A) >= (B) ? (A) : (B))
#define MIN(A, B) ((A) <= (B) ? (A) : (B))

inline float
interpolate (float min, float max, float alpha)
{
  return min + (max - min)*alpha;
}

#endif