-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcop_conversions.h
More file actions
134 lines (118 loc) · 3.78 KB
/
cop_conversions.h
File metadata and controls
134 lines (118 loc) · 3.78 KB
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
/* Copyright (c) 2016 Nick Appleton
*
* 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. */
/* C Compiler, OS and Platform Abstractions - Type/memory conversions */
#ifndef COP_CONVERSIONS
#define COP_CONVERSIONS
#include "cop_attributes.h"
#include <stdint.h>
#include <string.h>
static COP_ATTR_UNUSED uint_fast16_t cop_ld_ule16(const void *buf)
{
const unsigned char *b = buf;
return
((uint_fast16_t)b[0] << 0) |
((uint_fast16_t)b[1] << 8);
}
static COP_ATTR_UNUSED uint_fast16_t cop_ld_ube16(const void *buf)
{
const unsigned char *b = buf;
return
((uint_fast16_t)b[1] << 0) |
((uint_fast16_t)b[0] << 8);
}
static COP_ATTR_UNUSED uint_fast32_t cop_ld_ule24(const void *buf)
{
const unsigned char *b = buf;
return
((uint_fast32_t)b[0] << 0) |
((uint_fast32_t)b[1] << 8) |
((uint_fast32_t)b[2] << 16);
}
static COP_ATTR_UNUSED uint_fast32_t cop_ld_ube24(const void *buf)
{
const unsigned char *b = buf;
return
((uint_fast32_t)b[2] << 0) |
((uint_fast32_t)b[1] << 8) |
((uint_fast32_t)b[0] << 16);
}
static COP_ATTR_UNUSED int_fast32_t cop_ld_sle24(const void *buf)
{
#if defined(__clang__) && defined(__amd64)
int32_t val = 0;
memcpy((char *)&val + 1, (const unsigned char *)buf, 3);
val >>= 8;
return val;
#else
uint_fast32_t val;
val = ((const unsigned char *)buf)[2];
val = (val << 8) | ((const unsigned char *)buf)[1];
val = (val << 8) | ((const unsigned char *)buf)[0];
return (val & 0x800000u) ? -(int_fast32_t)(((~val) & 0x7FFFFFu) + 1) : (int_fast32_t)val;
#endif
}
static COP_ATTR_UNUSED uint_fast32_t cop_ld_ule32(const void *buf)
{
const unsigned char *b = buf;
return
((uint_fast32_t)b[0] << 0) |
((uint_fast32_t)b[1] << 8) |
((uint_fast32_t)b[2] << 16) |
((uint_fast32_t)b[3] << 24);
}
static COP_ATTR_UNUSED uint_fast32_t cop_ld_ube32(const void *buf)
{
const unsigned char *b = buf;
return
((uint_fast32_t)b[3] << 0) |
((uint_fast32_t)b[2] << 8) |
((uint_fast32_t)b[1] << 16) |
((uint_fast32_t)b[0] << 24);
}
static COP_ATTR_UNUSED void cop_st_ule16(void *buf, uint_fast16_t val)
{
unsigned char *b = buf;
b[0] = val & 0xFFu;
b[1] = (val >> 8) & 0xFFu;
}
static COP_ATTR_UNUSED void cop_st_sle16(void *buf, int_fast16_t val)
{
cop_st_ule16(buf, ((uint_fast16_t)val) & 0xFFFF);
}
static COP_ATTR_UNUSED void cop_st_ule24(void *buf, uint_fast32_t val)
{
unsigned char *b = buf;
b[0] = val & 0xFFu;
b[1] = (val >> 8) & 0xFFu;
b[2] = (val >> 16) & 0xFFu;
}
static COP_ATTR_UNUSED void cop_st_sle24(void *buf, int_fast32_t val)
{
cop_st_ule24(buf, ((uint_fast32_t)val) & 0xFFFFFF);
}
static COP_ATTR_UNUSED void cop_st_ule32(void *buf, uint_fast32_t val)
{
unsigned char *b = buf;
b[0] = val & 0xFFu;
b[1] = (val >> 8) & 0xFFu;
b[2] = (val >> 16) & 0xFFu;
b[3] = (val >> 24) & 0xFFu;
}
#endif /* COP_CONVERSIONS */