-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDKEncoding.h
193 lines (135 loc) · 6.09 KB
/
DKEncoding.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
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
/*****************************************************************************************
DKEncoding.h
Copyright (c) 2014 Derek W. Nylen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*****************************************************************************************/
#ifndef _DK_ENCODING_H_
#define _DK_ENCODING_H_
#ifdef __cplusplus
extern "C"
{
#endif
#define DKEncodingVersion 1
typedef enum
{
DKEncodingNull = 0,
// Special Types
DKEncodingTypeClass,
DKEncodingTypeSelector,
// Object Types
DKEncodingTypeObject,
DKEncodingTypeKeyedObject,
// Data Types
DKEncodingTypeTextData,
DKEncodingTypeBinaryData,
// Reserved
__DKEncodingTypeReserved,
// Integers
DKEncodingTypeInt8 = 8,
DKEncodingTypeInt16,
__DKEncodingTypeInvalid3ByteInt,
DKEncodingTypeInt32,
__DKEncodingTypeInvalid5ByteInt,
__DKEncodingTypeInvalid6ByteInt,
__DKEncodingTypeInvalid7ByteInt,
DKEncodingTypeInt64,
// Unsigned Integers
DKEncodingTypeUInt8,
DKEncodingTypeUInt16,
__DKEncodingTypeInvalid3ByteUInt,
DKEncodingTypeUInt32,
__DKEncodingTypeInvalid5ByteUInt,
__DKEncodingTypeInvalid6ByteUInt,
__DKEncodingTypeInvalid7ByteUInt,
DKEncodingTypeUInt64,
// Floats
DKEncodingTypeFloat,
DKEncodingTypeDouble,
DKMaxEncodingTypes
} DKEncodingType;
typedef uint32_t DKEncoding;
#define DKMaxEncodingCount 0x00ffffff // 2^24
#define DKMaxEncodingRows 0x00000fff
#define DKMaxEncodingCols 0x00000fff
#define DKEncodingMatrixBit 0x80000000
#define DKEncodingTypeBits 0x7f000000
#define DKEncodingTypeShift 24
#define DKEncodingRowsShift 12
#define DKEncode( baseType, count ) \
((((baseType) << DKEncodingTypeShift) & DKEncodingTypeBits) | \
((count) & DKMaxEncodingCount))
#define DKEncodeMatrix( baseType, rows, cols ) \
(DKEncodingMatrixBit | \
(((baseType) << DKEncodingTypeShift) & DKEncodingTypeBits) | \
(((rows) & DKMaxEncodingRows) << DKEncodingRowsShift) | \
((cols) & DKMaxEncodingCols))
// Macros for building integer encodings from built-in C types (i.e. enums)
#define DKEncodeIntegerType( ctype ) DKEncode( (DKEncodingTypeInt8 + sizeof(ctype) - 1), 1 )
#define DKEncodeIntegerVectorType( ctype, count ) DKEncode( (DKEncodingTypeInt8 + sizeof(ctype) - 1), (count) )
#define DKEncodeIntegerMatrixType( ctype, rows, cols ) DKEncodeMatrix( (DKEncodingTypeInt8 + sizeof(ctype) - 1), (rows), (cols) )
// Base type tests
#define DKEncodingTypeIsValid( baseType ) (((baseType) >= 0) && ((baseType) < DKMaxEncodingTypes))
#define DKEncodingTypeIsObject( baseType ) (((baseType) >= DKEncodingTypeClass) && ((baseType) <= DKEncodingTypeKeyedObject))
#define DKEncodingTypeIsInteger( baseType ) (((baseType) >= DKEncodingTypeInt8) && ((baseType) <= DKEncodingTypeUInt64))
#define DKEncodingTypeIsSigned( baseType ) (((baseType) >= DKEncodingTypeInt8) && ((baseType) <= DKEncodingTypeInt64))
#define DKEncodingTypeIsUnsigned( baseType ) (((baseType) >= DKEncodingTypeUInt8) && ((baseType) <= DKEncodingTypeUInt64))
#define DKEncodingTypeIsReal( baseType ) (((baseType) >= DKEncodingTypeFloat) && ((baseType) <= DKEncodingTypeDouble))
#define DKEncodingTypeIsNumber( baseType ) (((baseType) >= DKEncodingTypeInt8) && ((baseType) <= DKEncodingTypeDouble))
#define DKEncodingIsMatrix( encoding ) (((encoding) & DKEncodingMatrixBit) != 0)
static inline DKEncodingType DKEncodingGetType( DKEncoding encoding )
{
return (DKEncodingType)((encoding & DKEncodingTypeBits) >> DKEncodingTypeShift);
}
static inline unsigned int DKEncodingGetCount( DKEncoding encoding )
{
if( encoding & DKEncodingMatrixBit )
{
unsigned int rows = (encoding >> DKEncodingRowsShift) & DKMaxEncodingRows;
unsigned int cols = encoding & DKMaxEncodingCols;
return rows * cols;
}
return (encoding & DKMaxEncodingCount);
}
static inline unsigned int DKEncodingGetRows( DKEncoding encoding )
{
if( encoding & DKEncodingMatrixBit )
return (encoding >> DKEncodingRowsShift) & DKMaxEncodingRows;
return 1;
}
static inline unsigned int DKEncodingGetCols( DKEncoding encoding )
{
if( encoding & DKEncodingMatrixBit )
return encoding & DKMaxEncodingCols;
return encoding & DKMaxEncodingCount;
}
static inline bool DKEncodingEqv( DKEncoding a, DKEncoding b )
{
return (DKEncodingGetType( a ) == DKEncodingGetType( b )) &&
(DKEncodingGetCount( a ) == DKEncodingGetCount( b ));
}
DK_API size_t DKEncodingTypeGetSize( DKEncodingType encodingType );
DK_API size_t DKEncodingGetSize( DKEncoding encoding );
DK_API size_t DKEncodingGetTypeSize( DKEncoding encoding );
DK_API const char * DKEncodingGetTypeName( DKEncoding encoding );
DK_API DKStringRef DKEncodingGetDescription( DKEncoding encoding );
DK_API bool DKEncodingIsNumber( DKEncoding encoding );
DK_API bool DKEncodingIsInteger( DKEncoding encoding );
DK_API bool DKEncodingIsReal( DKEncoding encoding );
#ifdef __cplusplus
}
#endif
#endif // _DK_ENCODING_H_