1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28namespace WTF {
29
30inline bool needToFlipBytesIfLittleEndian(bool littleEndian)
31{
32#if CPU(BIG_ENDIAN)
33 return littleEndian;
34#else
35 return !littleEndian;
36#endif
37}
38
39inline uint16_t flipBytes(uint16_t value)
40{
41 return ((value & 0x00ff) << 8)
42 | ((value & 0xff00) >> 8);
43}
44
45inline uint32_t flipBytes(uint32_t value)
46{
47 return ((value & 0x000000ff) << 24)
48 | ((value & 0x0000ff00) << 8)
49 | ((value & 0x00ff0000) >> 8)
50 | ((value & 0xff000000) >> 24);
51}
52
53inline uint64_t flipBytes(uint64_t value)
54{
55 return ((value & 0x00000000000000ffull) << 56)
56 | ((value & 0x000000000000ff00ull) << 40)
57 | ((value & 0x0000000000ff0000ull) << 24)
58 | ((value & 0x00000000ff000000ull) << 8)
59 | ((value & 0x000000ff00000000ull) >> 8)
60 | ((value & 0x0000ff0000000000ull) >> 24)
61 | ((value & 0x00ff000000000000ull) >> 40)
62 | ((value & 0xff00000000000000ull) >> 56);
63}
64
65template<typename T>
66inline T flipBytes(T value)
67{
68 if (sizeof(value) == 1)
69 return value;
70 if (sizeof(value) == 2) {
71 union {
72 T original;
73 uint16_t word;
74 } u;
75 u.original = value;
76 u.word = flipBytes(u.word);
77 return u.original;
78 }
79 if (sizeof(value) == 4) {
80 union {
81 T original;
82 uint32_t word;
83 } u;
84 u.original = value;
85 u.word = flipBytes(u.word);
86 return u.original;
87 }
88 if (sizeof(value) == 8) {
89 union {
90 T original;
91 uint64_t word;
92 } u;
93 u.original = value;
94 u.word = flipBytes(u.word);
95 return u.original;
96 }
97 RELEASE_ASSERT_NOT_REACHED();
98 return T();
99}
100
101template<typename T>
102inline T flipBytesIfLittleEndian(T value, bool littleEndian)
103{
104 if (needToFlipBytesIfLittleEndian(littleEndian))
105 return flipBytes(value);
106 return value;
107}
108
109} // namespace WTF
110
111using WTF::needToFlipBytesIfLittleEndian;
112using WTF::flipBytes;
113using WTF::flipBytesIfLittleEndian;
114