Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/openlaunchd
Path: blob/master/liblaunch/byteswap.h
374 views
1
/*
2
* Copyright (c) 2013 Apple, Inc., R. Tyler Croy, All rights reserved.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*
16
*/
17
18
#ifdef __FreeBSD__
19
#include <sys/endian.h>
20
#else
21
#include <endian.h>
22
#endif
23
24
#define host2wire(x) \
25
({ typeof (x) _X, _x = (x); \
26
switch (sizeof(_x)) { \
27
case 8: \
28
_X = htobe64(_x); \
29
break; \
30
case 4: \
31
_X = htobe32(_x); \
32
break; \
33
case 2: \
34
_X = htobe16(_x); \
35
break; \
36
case 1: \
37
_X = _x; \
38
break; \
39
default: \
40
_X = x; \
41
break; \
42
} \
43
_X; \
44
})
45
46
#define wire2host(x) \
47
({ typeof (x) _X, _x = (x); \
48
switch (sizeof(_x)) { \
49
case 8: \
50
_X = be64toh(_x); \
51
break; \
52
case 4: \
53
_X = be32toh(_x); \
54
break; \
55
case 2: \
56
_X = be16toh(_x); \
57
break; \
58
case 1: \
59
_X = _x; \
60
break; \
61
default: \
62
_X = _x; \
63
break; \
64
} \
65
_X; \
66
})
67
68
69
union _launch_double_u {
70
uint64_t iv;
71
double dv;
72
};
73
74
#define host2wire_f(x) ({ \
75
typeof(x) _F, _f = (x); \
76
union _launch_double_u s; \
77
s.dv = _f; \
78
s.iv = host2wire(s.iv); \
79
_F = s.dv; \
80
_F; \
81
})
82
83
#define wire2host_f(x) ({ \
84
typeof(x) _F, _f = (x); \
85
union _launch_double_u s; \
86
s.dv = _f; \
87
s.iv = wire2host(s.iv); \
88
_F = s.dv; \
89
_F; \
90
})
91
92