summaryrefslogtreecommitdiffstats
path: root/src/common.h
blob: 0cac24dfbcc468dd8616b1d93ad299d4363522e7 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * This file is part of libplacebo.
 *
 * libplacebo is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libplacebo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with libplacebo. If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#define __STDC_FORMAT_MACROS

#ifdef __cplusplus
#include <version>
#endif

#if !defined(__cplusplus) || defined(__cpp_lib_stdatomic_h)
#define PL_HAVE_STDATOMIC
#endif

#ifdef PL_HAVE_STDATOMIC
#include <stdatomic.h>
#endif
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <inttypes.h>

#if defined(__MINGW32__) && !defined(__clang__)
#define PL_PRINTF(fmt, va) __attribute__ ((format(gnu_printf, fmt, va))) \
                           __attribute__ ((nonnull(fmt)))
#elif defined(__GNUC__)
#define PL_PRINTF(fmt, va) __attribute__ ((format(printf, fmt, va))) \
                           __attribute__ ((nonnull(fmt)))
#else
#define PL_PRINTF(fmt, va)
#endif

#define PL_NOINLINE __attribute__((noinline))

#include "os.h"

#include "config_internal.h"

#define PL_DEPRECATED

#include <libplacebo/config.h>

#include "pl_assert.h"
#include "pl_alloc.h"
#include "pl_clock.h"
#include "pl_string.h"

#if PL_API_VER != BUILD_API_VER
#error Header mismatch? <libplacebo/config.h> pulled from elsewhere!
#endif

// Divide a number while rounding up (careful: double-eval)
#define PL_DIV_UP(x, y) (((x) + (y) - 1) / (y))

// Align up to the nearest multiple of an arbitrary alignment, which may also
// be 0 to signal no alignment requirements.
#define PL_ALIGN(x, align) ((align) ? PL_DIV_UP(x, align) * (align) : (x))

// This is faster but must only be called on positive powers of two.
#define PL_ALIGN2(x, align) (((x) + (align) - 1) & ~((align) - 1))

// Returns the log base 2 of an unsigned long long
#define PL_LOG2(x) ((unsigned) (8*sizeof (unsigned long long) - __builtin_clzll((x)) - 1))

// Rounds a number up to the nearest power of two
#define PL_ALIGN_POT(x) (0x1LLU << (PL_LOG2((x) - 1) + 1))

// Right shift a number while rounding up
#define PL_RSHIFT_UP(x, s) -((-(x)) >> (s))

// Returns whether or not a number is a power of two (or zero)
#define PL_ISPOT(x) (((x) & ((x) - 1)) == 0)

// Returns the size of a static array with known size.
#define PL_ARRAY_SIZE(s) (sizeof(s) / sizeof((s)[0]))

// Swaps two variables
#define PL_SWAP(a, b)              \
    do {                           \
        __typeof__ (a) _tmp = (a); \
        (a) = (b);                 \
        (b) = _tmp;                \
    } while (0)

// Helper functions for transposing a matrix in-place.
#define PL_TRANSPOSE_DIM(d, m) \
    pl_transpose((d), (float[(d)*(d)]){0}, (const float *)(m))

#define PL_TRANSPOSE_2X2(m) PL_TRANSPOSE_DIM(2, m)
#define PL_TRANSPOSE_3X3(m) PL_TRANSPOSE_DIM(3, m)
#define PL_TRANSPOSE_4X4(m) PL_TRANSPOSE_DIM(4, m)

static inline float *pl_transpose(int dim, float *out, const float *in)
{
    for (int i = 0; i < dim; i++) {
        for (int j = 0; j < dim; j++)
            out[i * dim + j] = in[j * dim + i];
    }

    return out;
}

// Helper functions for some common numeric operations (careful: double-eval)
#define PL_MAX(x, y) ((x) > (y) ? (x) : (y))
#define PL_MAX3(x, y, z) PL_MAX(PL_MAX(x, y), z)
#define PL_MIN(x, y) ((x) < (y) ? (x) : (y))
#define PL_CLAMP(x, l, h) ((x) < (l) ? (l) : (x) > (h) ? (h) : (x))
#define PL_CMP(a, b) (((a) > (b)) - ((a) < (b)))
#define PL_DEF(x, d) ((x) ? (x) : (d))
#define PL_SQUARE(x) ((x) * (x))
#define PL_CUBE(x) ((x) * (x) * (x))
#define PL_MIX(a, b, x) ((x) * (b) + (1 - (x)) * (a))

static inline float pl_smoothstep(float edge0, float edge1, float x)
{
    if (edge0 == edge1)
        return x >= edge0;
    x = (x - edge0) / (edge1 - edge0);
    x = PL_CLAMP(x, 0.0f, 1.0f);
    return x * x * (3.0f - 2.0f * x);
}

// Helpers for doing alignment calculations
static inline size_t pl_gcd(size_t x, size_t y)
{
    assert(x && y);
    while (y) {
        size_t tmp = y;
        y = x % y;
        x = tmp;
    }

    return x;
}

static inline size_t pl_lcm(size_t x, size_t y)
{
    assert(x && y);
    return x * (y / pl_gcd(x, y));
}

// Conditional abort() macro that depends on the configuration option
#ifdef PL_DEBUG_ABORT
# define pl_debug_abort() do {                          \
    fprintf(stderr, "pl_debug_abort() triggered!\n");   \
    abort();                                            \
} while (0)
#else
# define pl_debug_abort() do {} while (0)
#endif

#ifdef PL_HAVE_STDATOMIC

// Refcounting helpers
typedef atomic_uint_fast32_t pl_rc_t;
#define pl_rc_init(rc)  atomic_init(rc, 1)
#define pl_rc_ref(rc)   ((void) atomic_fetch_add_explicit(rc, 1, memory_order_acquire))
#define pl_rc_deref(rc) (atomic_fetch_sub_explicit(rc, 1, memory_order_release) == 1)
#define pl_rc_count(rc)  atomic_load(rc)

#endif

#define pl_unreachable() (assert(!"unreachable"), __builtin_unreachable())

// Helper for parameter validation
#define pl_require(ctx, expr)                                   \
  do {                                                          \
      if (!(expr)) {                                            \
          PL_ERR(ctx, "Validation failed: %s (%s:%d)",          \
                  #expr, __FILE__, __LINE__);                   \
          pl_log_stack_trace(ctx->log, PL_LOG_ERR);             \
          pl_debug_abort();                                     \
          goto error;                                           \
      }                                                         \
  } while (0)