The open imaging DSP library
Loading...
Searching...
No Matches
utils.h
1
6#ifndef MPIX_UTILS_H
7#define MPIX_UTILS_H
8
9#include <errno.h>
10#include <stddef.h>
11#include <stdint.h>
12#include <string.h>
13#include <stdio.h>
14
15#include <mpix/config.h>
16#include <mpix/port.h>
17#include <mpix/types.h>
18
19/* Logging utilities */
20
21#if CONFIG_MPIX_LOG_LEVEL >= 1
22#define MPIX_ERR(fmt, ...) mpix_port_printf("E: %s: " fmt "\n", __func__, ## __VA_ARGS__)
23#else
24#define MPIX_ERR(fmt, ...) ((void)0)
25#endif
26
27#if CONFIG_MPIX_LOG_LEVEL >= 2
28#define MPIX_WRN(fmt, ...) mpix_port_printf("W: %s: " fmt "\n", __func__, ## __VA_ARGS__)
29#else
30#define MPIX_WRN(fmt, ...) ((void)0)
31#endif
32
33#if CONFIG_MPIX_LOG_LEVEL >= 3
34#define MPIX_INF(fmt, ...) mpix_port_printf("I: %s: " fmt "\n", __func__, ## __VA_ARGS__)
35#else
36#define MPIX_INF(fmt, ...) ((void)0)
37#endif
38
39#if CONFIG_MPIX_LOG_LEVEL >= 4
40#define MPIX_DBG(fmt, ...) mpix_port_printf("D: %s: " fmt "\n", __func__, ## __VA_ARGS__)
41#else
42#define MPIX_DBG(fmt, ...) ((void)0)
43#endif
44
45/* String table utilities */
46
47extern const struct mpix_str mpix_str_fmt[];
48extern const char * const mpix_str_op[];
49extern const char * const mpix_str_cid[];
50extern const char * const mpix_str_kernel[];
51extern const char * const mpix_str_jpeg[];
52
53static inline int mpix_enum(const struct mpix_str *table, const char *name)
54{
55 for (size_t i = 0; table[i].name != NULL; i++) {
56 if (strcmp(table[i].name, name) == 0) {
57 return table[i].value;
58 }
59 }
60
61 return -EINVAL;
62}
63
64/* Math operations */
65
66#ifndef BITS_PER_BYTE
67#define BITS_PER_BYTE 8
68#endif
69
70#ifndef ARRAY_SIZE
71#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
72#endif
73
74#ifndef CLAMP
75#define CLAMP(n, min, max) ((n) < (min) ? (min) : (n) > (max) ? (max) : (n))
76#endif
77
78#ifndef MIN
79#define MIN(a, b) ((a) < (b) ? (a) : (b))
80#endif
81
82#ifndef MAX
83#define MAX(a, b) ((a) > (b) ? (a) : (b))
84#endif
85
86#ifndef RGB32
87#define RGB32(r8, g8, b8) (((uint8_t)(r8) << 16) | ((uint8_t)(g8) << 8) | (uint8_t)(b8))
88#endif
89
90#ifndef IN_RANGE
91#define IN_RANGE(n, min, max) ((n) >= (min) && (n) <= (max))
92#endif
93
94#ifndef WITHIN
95#define WITHIN(n, ref, margin) ((n) >= (ref) - (margin) && (n) <= (ref) + (margin))
96#endif
97
98#ifndef LOG2
99#define _LOG2D(x) (32 - __builtin_clz(x) - 1)
100#define _LOG2Q(x) (64 - __builtin_clzll(x) - 1)
101#define _LOG2(x) (sizeof(__typeof__(x)) > 4 ? _LOG2Q(x) : _LOG2D(x))
102#define LOG2(x) ((x) < 1 ? -1 : _LOG2(x))
103#endif
104
105/* Endianness operations */
106
107static inline uint16_t mpix_bswap16(uint16_t u)
108{
109 return ((u & 0xff00U) >> 8) | ((u & 0x00ffU) << 8);
110}
111
112static inline uint32_t mpix_bswap32(uint32_t u)
113{
114 return ((u & 0xff000000UL) >> 24) | ((u & 0x000000ffUL) << 24) |
115 ((u & 0x00ff0000UL) >> 8) | ((u & 0x0000ff00UL) << 8);
116}
117
118static inline uint64_t mpix_bswap64(uint64_t u)
119{
120 return ((u & 0xff00000000000000ULL) >> 56) | ((u & 0x00000000000000ffULL) << 56) |
121 ((u & 0x00ff000000000000ULL) >> 40) | ((u & 0x000000000000ff00ULL) << 40) |
122 ((u & 0x0000ff0000000000ULL) >> 24) | ((u & 0x0000000000ff0000ULL) << 24) |
123 ((u & 0x000000ff00000000ULL) >> 8) | ((u & 0x00000000ff000000ULL) << 8);
124}
125
126#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
127
128#define MPIX_CONV_BE(bits, u) (u)
129#define MPIX_CONV_LE(bits, u) mpix_bswap##bits(u)
130
131#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
132
133#define MPIX_CONV_BE(bits, u) mpix_bswap##bits(u)
134#define MPIX_CONV_LE(bits, u) (u)
135
136#endif
137
138static inline uint16_t mpix_htobe16(uint16_t u) { return MPIX_CONV_BE(16, u); }
139static inline uint16_t mpix_htole16(uint16_t u) { return MPIX_CONV_LE(16, u); }
140static inline uint16_t mpix_be16toh(uint16_t u) { return MPIX_CONV_BE(16, u); }
141static inline uint16_t mpix_le16toh(uint16_t u) { return MPIX_CONV_LE(16, u); }
142static inline uint32_t mpix_htobe32(uint32_t u) { return MPIX_CONV_BE(32, u); }
143static inline uint32_t mpix_htole32(uint32_t u) { return MPIX_CONV_LE(32, u); }
144static inline uint32_t mpix_be32toh(uint32_t u) { return MPIX_CONV_BE(32, u); }
145static inline uint32_t mpix_le32toh(uint32_t u) { return MPIX_CONV_LE(32, u); }
146static inline uint64_t mpix_htobe64(uint64_t u) { return MPIX_CONV_BE(64, u); }
147static inline uint64_t mpix_htole64(uint64_t u) { return MPIX_CONV_LE(64, u); }
148static inline uint64_t mpix_be64toh(uint64_t u) { return MPIX_CONV_BE(64, u); }
149static inline uint64_t mpix_le64toh(uint64_t u) { return MPIX_CONV_LE(64, u); }
150
151#endif /* @} */
Definition types.h:152
uint32_t value
Definition types.h:156
const char * name
Definition types.h:154