common.h 2.82 KB
Newer Older
Niklas Haas's avatar
Niklas Haas committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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

20
#define __STDC_FORMAT_MACROS
Niklas Haas's avatar
Niklas Haas committed
21 22 23 24
#include <stddef.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
25
#include <inttypes.h>
Niklas Haas's avatar
Niklas Haas committed
26 27 28

#include "ta/talloc.h"
#include "config.h"
29
#include "pl_assert.h"
Niklas Haas's avatar
Niklas Haas committed
30 31 32 33 34 35 36 37

// 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) ? ((x) + (align) - 1) / (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))

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

Niklas Haas's avatar
Niklas Haas committed
41 42 43 44
// Returns the size of a static array with known size.
#define PL_ARRAY_SIZE(s) (sizeof(s) / sizeof((s)[0]))

// Swaps two variables
45 46 47 48 49
#define PL_SWAP(a, b)             \
    do {                          \
        __typeof__ (a) tmp = (a); \
        (a) = (b);                \
        (b) = tmp;                \
Niklas Haas's avatar
Niklas Haas committed
50
    } while (0)
Niklas Haas's avatar
Niklas Haas committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

// 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;
}
69

70
// Helper functions for some common numeric operations (careful: double-eval)
71 72
#define PL_MAX(x, y) ((x) > (y) ? (x) : (y))
#define PL_MIN(x, y) ((x) < (y) ? (x) : (y))
73
#define PL_CMP(a, b) ((a) < (b) ? -1 : (a) > (b) ? 1 : 0)
74
#define PL_DEF(x, d) ((x) ? (x) : (d))
75 76
#define PL_SQUARE(x) ((x) * (x))
#define PL_CUBE(x) ((x) * (x) * (x))
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

// Helpers for doing alignment calculations
static inline size_t pl_gcd(size_t x, size_t 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)
{
    return x * (y / pl_gcd(x, y));
}