This repository has been archived by the owner on Nov 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.h
122 lines (108 loc) · 3.5 KB
/
Common.h
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
/* ============================================================================
* Common.h: Common definitions, macros, etc.
*
* AUDIOSIM: AUDIO Interface SIMulator.
* Copyright (C) 2013, Tyler J. Stachecki.
* All rights reserved.
*
* This file is subject to the terms and conditions defined in
* file 'LICENSE', which is part of this source code package.
* ========================================================================= */
#ifndef __COMMON_H__
#define __COMMON_H__
/* ============================================================================
* Attempt to define standard integer and boolean types.
* ========================================================================= */
#if (__STDC_VERSION__ >= 199901L)
#include <stdbool.h>
#include <stdint.h>
#elif (__cplusplus > 201100L)
#include <cstdint>
#elif defined (_MSC_VER)
#if (_MSC_VER < 1300)
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
#else
typedef signed __int8 int8_t;
typedef signed __int16 int16_t;
typedef signed __int32 int32_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
#endif
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
typedef char bool;
#define false 0
#define true 1
#else
#error "Couldn't determine standard integer types."
#error "Use C99/C11/C++0x/C++11, or add typedefs to 'Common.h'"
#endif
/* ============================================================================
* debug(x): Prints messages only when DNDEBUG is not defined.
* ========================================================================= */
#ifndef NDEBUG
#ifdef __cplusplus
#include <cstdio>
#else
#include <stdio.h>
#endif
#define debug(msg) fprintf(stderr, "[AIF]: "#msg"\n")
#define debugarg(msg, arg) fprintf(stderr, "[AIF]: "#msg"\n", arg)
#else
#define debug(msg)
#define debugarg(msg, arg)
#endif
/* ============================================================================
* likely(x) and unlikely(x): Specifies branch weights.
* ========================================================================= */
#ifdef __GNUC__
#define likely(expr) __builtin_expect(!!(expr), !0)
#define unlikely(expr) __builtin_expect(!!(expr), 0)
#else
#define likely(expr)
#define unlikely(expr)
#endif
/* ============================================================================
* unused(x): Marks unused variables.
* ========================================================================= */
#ifdef __GNUC__
#define uninit(var) var = var
#define unused(var) UNUSED__##var __attribute__((unused))
#else
#define uninit(var) var
#define unused(var)
#endif
/* ============================================================================
* Host byte order swap functions.
* ========================================================================= */
static inline uint16_t ByteOrderSwap16(uint16_t halfword) {
#ifndef LITTLE_ENDIAN
return halfword;
#elif defined(_MSC_VER)
return _byteswap_ushort(halfword);
#else
return (((halfword) >> 8) | ((halfword) << 8));
#endif
}
static inline uint32_t ByteOrderSwap32(uint32_t word) {
#ifndef LITTLE_ENDIAN
return word;
#elif defined(_MSC_VER)
return _byteswap_ulong(word);
#elif defined(__GNUC__)
return __builtin_bswap32(word);
#else
return
(((((word) >> 24) & 0x000000FF) | \
(((word) >> 8) & 0x0000FF00) | \
(((word) << 8) & 0x00FF0000) | \
(((word) << 24) & 0xFF000000));
#endif
}
#endif