]> mj.ucw.cz Git - home-hw.git/blob - lib/util.h
a84d09975475d03fe05a7bca1715aeb57b3c25b6
[home-hw.git] / lib / util.h
1 #include <stdbool.h>
2 #include <stddef.h>
3 #include <stdint.h>
4
5 #include "config.h"
6
7 typedef unsigned int uint;
8 typedef uint8_t byte;
9 typedef uint16_t u16;
10 typedef int16_t s16;
11 typedef uint32_t u32;
12 typedef int32_t s32;
13
14 #define MIN(x,y) ((x) < (y) ? (x) : (y))
15 #define MAX(x,y) ((x) > (y) ? (x) : (y))
16
17 #define UNUSED __attribute__((unused))
18
19 static inline uint get_u16_le(byte *p)
20 {
21         return (p[1] << 8) | p[0];
22 }
23
24 static inline uint get_u16_be(byte *p)
25 {
26         return (p[0] << 8) | p[1];
27 }
28
29 static inline uint get_u32_le(byte *p)
30 {
31         return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
32 }
33
34 static inline uint get_u32_be(byte *p)
35 {
36         return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
37 }
38
39 static inline void put_u16_le(byte *p, u16 x)
40 {
41         p[0] = x;
42         p[1] = x >> 8;
43 }
44
45 static inline void put_u16_be(byte *p, u16 x)
46 {
47         p[0] = x >> 8;
48         p[1] = x;
49 }
50
51 static inline void put_u32_be(byte *p, u32 x)
52 {
53         p[0] = x >> 24;
54         p[1] = (x >> 16) & 0xff;
55         p[2] = (x >> 8) & 0xff;
56         p[3] = x & 0xff;
57 }
58
59 static inline void put_u32_le(byte *p, u32 x)
60 {
61         p[3] = x >> 24;
62         p[2] = (x >> 16) & 0xff;
63         p[1] = (x >> 8) & 0xff;
64         p[0] = x & 0xff;
65 }
66
67 // debug.c
68
69 void debug_printf(const char *fmt, ...);
70 void debug_puts(const char *s);
71 void debug_putc(int c);