Bumping manifests a=b2g-bump
[gecko.git] / mfbt / Endian.h
blob7dea6eaec79f4e0414388f67c82b05dd738bbb9a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Functions for reading and writing integers in various endiannesses. */
9 /*
10 * The classes LittleEndian and BigEndian expose static methods for
11 * reading and writing 16-, 32-, and 64-bit signed and unsigned integers
12 * in their respective endianness. The naming scheme is:
14 * {Little,Big}Endian::{read,write}{Uint,Int}<bitsize>
16 * For instance, LittleEndian::readInt32 will read a 32-bit signed
17 * integer from memory in little endian format. Similarly,
18 * BigEndian::writeUint16 will write a 16-bit unsigned integer to memory
19 * in big-endian format.
21 * The class NativeEndian exposes methods for conversion of existing
22 * data to and from the native endianness. These methods are intended
23 * for cases where data needs to be transferred, serialized, etc.
24 * swap{To,From}{Little,Big}Endian byteswap a single value if necessary.
25 * Bulk conversion functions are also provided which optimize the
26 * no-conversion-needed case:
28 * - copyAndSwap{To,From}{Little,Big}Endian;
29 * - swap{To,From}{Little,Big}EndianInPlace.
31 * The *From* variants are intended to be used for reading data and the
32 * *To* variants for writing data.
34 * Methods on NativeEndian work with integer data of any type.
35 * Floating-point data is not supported.
37 * For clarity in networking code, "Network" may be used as a synonym
38 * for "Big" in any of the above methods or class names.
40 * As an example, reading a file format header whose fields are stored
41 * in big-endian format might look like:
43 * class ExampleHeader
44 * {
45 * private:
46 * uint32_t mMagic;
47 * uint32_t mLength;
48 * uint32_t mTotalRecords;
49 * uint64_t mChecksum;
51 * public:
52 * ExampleHeader(const void* data)
53 * {
54 * const uint8_t* ptr = static_cast<const uint8_t*>(data);
55 * mMagic = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
56 * mLength = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
57 * mTotalRecords = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
58 * mChecksum = BigEndian::readUint64(ptr);
59 * }
60 * ...
61 * };
64 #ifndef mozilla_Endian_h
65 #define mozilla_Endian_h
67 #include "mozilla/Assertions.h"
68 #include "mozilla/Attributes.h"
69 #include "mozilla/Compiler.h"
70 #include "mozilla/DebugOnly.h"
71 #include "mozilla/TypeTraits.h"
73 #include <stdint.h>
74 #include <string.h>
76 #if defined(_MSC_VER)
77 # include <stdlib.h>
78 # pragma intrinsic(_byteswap_ushort)
79 # pragma intrinsic(_byteswap_ulong)
80 # pragma intrinsic(_byteswap_uint64)
81 #endif
83 #if defined(_WIN64)
84 # if defined(_M_X64) || defined(_M_AMD64) || defined(_AMD64_)
85 # define MOZ_LITTLE_ENDIAN 1
86 # else
87 # error "CPU type is unknown"
88 # endif
89 #elif defined(_WIN32)
90 # if defined(_M_IX86)
91 # define MOZ_LITTLE_ENDIAN 1
92 # else
93 # error "CPU type is unknown"
94 # endif
95 #elif defined(__APPLE__) || defined(__powerpc__) || defined(__ppc__)
96 # if __LITTLE_ENDIAN__
97 # define MOZ_LITTLE_ENDIAN 1
98 # elif __BIG_ENDIAN__
99 # define MOZ_BIG_ENDIAN 1
100 # endif
101 #elif defined(__GNUC__) && \
102 defined(__BYTE_ORDER__) && \
103 defined(__ORDER_LITTLE_ENDIAN__) && \
104 defined(__ORDER_BIG_ENDIAN__)
106 * Some versions of GCC provide architecture-independent macros for
107 * this. Yes, there are more than two values for __BYTE_ORDER__.
109 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
110 # define MOZ_LITTLE_ENDIAN 1
111 # elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
112 # define MOZ_BIG_ENDIAN 1
113 # else
114 # error "Can't handle mixed-endian architectures"
115 # endif
117 * We can't include useful headers like <endian.h> or <sys/isa_defs.h>
118 * here because they're not present on all platforms. Instead we have
119 * this big conditional that ideally will catch all the interesting
120 * cases.
122 #elif defined(__sparc) || defined(__sparc__) || \
123 defined(_POWER) || defined(__hppa) || \
124 defined(_MIPSEB) || defined(__ARMEB__) || \
125 defined(__s390__) || defined(__AARCH64EB__) || \
126 (defined(__sh__) && defined(__LITTLE_ENDIAN__)) || \
127 (defined(__ia64) && defined(__BIG_ENDIAN__))
128 # define MOZ_BIG_ENDIAN 1
129 #elif defined(__i386) || defined(__i386__) || \
130 defined(__x86_64) || defined(__x86_64__) || \
131 defined(_MIPSEL) || defined(__ARMEL__) || \
132 defined(__alpha__) || defined(__AARCH64EL__) || \
133 (defined(__sh__) && defined(__BIG_ENDIAN__)) || \
134 (defined(__ia64) && !defined(__BIG_ENDIAN__))
135 # define MOZ_LITTLE_ENDIAN 1
136 #endif
138 #if MOZ_BIG_ENDIAN
139 # define MOZ_LITTLE_ENDIAN 0
140 #elif MOZ_LITTLE_ENDIAN
141 # define MOZ_BIG_ENDIAN 0
142 #else
143 # error "Cannot determine endianness"
144 #endif
146 #if defined(__clang__)
147 # if __has_builtin(__builtin_bswap16)
148 # define MOZ_HAVE_BUILTIN_BYTESWAP16 __builtin_bswap16
149 # endif
150 #elif defined(__GNUC__)
151 # if MOZ_GCC_VERSION_AT_LEAST(4, 8, 0)
152 # define MOZ_HAVE_BUILTIN_BYTESWAP16 __builtin_bswap16
153 # endif
154 #elif defined(_MSC_VER)
155 # define MOZ_HAVE_BUILTIN_BYTESWAP16 _byteswap_ushort
156 #endif
158 namespace mozilla {
160 namespace detail {
163 * We need wrappers here because free functions with default template
164 * arguments and/or partial specialization of function templates are not
165 * supported by all the compilers we use.
167 template<typename T, size_t Size = sizeof(T)>
168 struct Swapper;
170 template<typename T>
171 struct Swapper<T, 2>
173 static T swap(T aValue)
175 #if defined(MOZ_HAVE_BUILTIN_BYTESWAP16)
176 return MOZ_HAVE_BUILTIN_BYTESWAP16(aValue);
177 #else
178 return T(((aValue & 0x00ff) << 8) | ((aValue & 0xff00) >> 8));
179 #endif
183 template<typename T>
184 struct Swapper<T, 4>
186 static T swap(T aValue)
188 #if defined(__clang__) || defined(__GNUC__)
189 return T(__builtin_bswap32(aValue));
190 #elif defined(_MSC_VER)
191 return T(_byteswap_ulong(aValue));
192 #else
193 return T(((aValue & 0x000000ffU) << 24) |
194 ((aValue & 0x0000ff00U) << 8) |
195 ((aValue & 0x00ff0000U) >> 8) |
196 ((aValue & 0xff000000U) >> 24));
197 #endif
201 template<typename T>
202 struct Swapper<T, 8>
204 static inline T swap(T aValue)
206 #if defined(__clang__) || defined(__GNUC__)
207 return T(__builtin_bswap64(aValue));
208 #elif defined(_MSC_VER)
209 return T(_byteswap_uint64(aValue));
210 #else
211 return T(((aValue & 0x00000000000000ffULL) << 56) |
212 ((aValue & 0x000000000000ff00ULL) << 40) |
213 ((aValue & 0x0000000000ff0000ULL) << 24) |
214 ((aValue & 0x00000000ff000000ULL) << 8) |
215 ((aValue & 0x000000ff00000000ULL) >> 8) |
216 ((aValue & 0x0000ff0000000000ULL) >> 24) |
217 ((aValue & 0x00ff000000000000ULL) >> 40) |
218 ((aValue & 0xff00000000000000ULL) >> 56));
219 #endif
223 enum Endianness { Little, Big };
225 #if MOZ_BIG_ENDIAN
226 # define MOZ_NATIVE_ENDIANNESS detail::Big
227 #else
228 # define MOZ_NATIVE_ENDIANNESS detail::Little
229 #endif
231 class EndianUtils
234 * Assert that the memory regions [aDest, aDest+aCount) and
235 * [aSrc, aSrc+aCount] do not overlap. aCount is given in bytes.
237 static void assertNoOverlap(const void* aDest, const void* aSrc,
238 size_t aCount)
240 DebugOnly<const uint8_t*> byteDestPtr = static_cast<const uint8_t*>(aDest);
241 DebugOnly<const uint8_t*> byteSrcPtr = static_cast<const uint8_t*>(aSrc);
242 MOZ_ASSERT((byteDestPtr <= byteSrcPtr &&
243 byteDestPtr + aCount <= byteSrcPtr) ||
244 (byteSrcPtr <= byteDestPtr &&
245 byteSrcPtr + aCount <= byteDestPtr));
248 template<typename T>
249 static void assertAligned(T* aPtr)
251 MOZ_ASSERT((uintptr_t(aPtr) % sizeof(T)) == 0, "Unaligned pointer!");
254 protected:
256 * Return |aValue| converted from SourceEndian encoding to DestEndian
257 * encoding.
259 template<Endianness SourceEndian, Endianness DestEndian, typename T>
260 static inline T maybeSwap(T aValue)
262 if (SourceEndian == DestEndian) {
263 return aValue;
265 return Swapper<T>::swap(aValue);
269 * Convert |aCount| elements at |aPtr| from SourceEndian encoding to
270 * DestEndian encoding.
272 template<Endianness SourceEndian, Endianness DestEndian, typename T>
273 static inline void maybeSwapInPlace(T* aPtr, size_t aCount)
275 assertAligned(aPtr);
277 if (SourceEndian == DestEndian) {
278 return;
280 for (size_t i = 0; i < aCount; i++) {
281 aPtr[i] = Swapper<T>::swap(aPtr[i]);
286 * Write |aCount| elements to the unaligned address |aDest| in DestEndian
287 * format, using elements found at |aSrc| in SourceEndian format.
289 template<Endianness SourceEndian, Endianness DestEndian, typename T>
290 static void copyAndSwapTo(void* aDest, const T* aSrc, size_t aCount)
292 assertNoOverlap(aDest, aSrc, aCount * sizeof(T));
293 assertAligned(aSrc);
295 if (SourceEndian == DestEndian) {
296 memcpy(aDest, aSrc, aCount * sizeof(T));
297 return;
300 uint8_t* byteDestPtr = static_cast<uint8_t*>(aDest);
301 for (size_t i = 0; i < aCount; ++i) {
302 union
304 T mVal;
305 uint8_t mBuffer[sizeof(T)];
306 } u;
307 u.mVal = maybeSwap<SourceEndian, DestEndian>(aSrc[i]);
308 memcpy(byteDestPtr, u.mBuffer, sizeof(T));
309 byteDestPtr += sizeof(T);
314 * Write |aCount| elements to |aDest| in DestEndian format, using elements
315 * found at the unaligned address |aSrc| in SourceEndian format.
317 template<Endianness SourceEndian, Endianness DestEndian, typename T>
318 static void copyAndSwapFrom(T* aDest, const void* aSrc, size_t aCount)
320 assertNoOverlap(aDest, aSrc, aCount * sizeof(T));
321 assertAligned(aDest);
323 if (SourceEndian == DestEndian) {
324 memcpy(aDest, aSrc, aCount * sizeof(T));
325 return;
328 const uint8_t* byteSrcPtr = static_cast<const uint8_t*>(aSrc);
329 for (size_t i = 0; i < aCount; ++i) {
330 union
332 T mVal;
333 uint8_t mBuffer[sizeof(T)];
334 } u;
335 memcpy(u.mBuffer, byteSrcPtr, sizeof(T));
336 aDest[i] = maybeSwap<SourceEndian, DestEndian>(u.mVal);
337 byteSrcPtr += sizeof(T);
342 template<Endianness ThisEndian>
343 class Endian : private EndianUtils
345 protected:
346 /** Read a uint16_t in ThisEndian endianness from |aPtr| and return it. */
347 static MOZ_WARN_UNUSED_RESULT uint16_t readUint16(const void* aPtr)
349 return read<uint16_t>(aPtr);
352 /** Read a uint32_t in ThisEndian endianness from |aPtr| and return it. */
353 static MOZ_WARN_UNUSED_RESULT uint32_t readUint32(const void* aPtr)
355 return read<uint32_t>(aPtr);
358 /** Read a uint64_t in ThisEndian endianness from |aPtr| and return it. */
359 static MOZ_WARN_UNUSED_RESULT uint64_t readUint64(const void* aPtr)
361 return read<uint64_t>(aPtr);
364 /** Read an int16_t in ThisEndian endianness from |aPtr| and return it. */
365 static MOZ_WARN_UNUSED_RESULT int16_t readInt16(const void* aPtr)
367 return read<int16_t>(aPtr);
370 /** Read an int32_t in ThisEndian endianness from |aPtr| and return it. */
371 static MOZ_WARN_UNUSED_RESULT int32_t readInt32(const void* aPtr)
373 return read<uint32_t>(aPtr);
376 /** Read an int64_t in ThisEndian endianness from |aPtr| and return it. */
377 static MOZ_WARN_UNUSED_RESULT int64_t readInt64(const void* aPtr)
379 return read<int64_t>(aPtr);
382 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
383 static void writeUint16(void* aPtr, uint16_t aValue)
385 write(aPtr, aValue);
388 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
389 static void writeUint32(void* aPtr, uint32_t aValue)
391 write(aPtr, aValue);
394 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
395 static void writeUint64(void* aPtr, uint64_t aValue)
397 write(aPtr, aValue);
400 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
401 static void writeInt16(void* aPtr, int16_t aValue)
403 write(aPtr, aValue);
406 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
407 static void writeInt32(void* aPtr, int32_t aValue)
409 write(aPtr, aValue);
412 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
413 static void writeInt64(void* aPtr, int64_t aValue)
415 write(aPtr, aValue);
419 * Converts a value of type T to little-endian format.
421 * This function is intended for cases where you have data in your
422 * native-endian format and you need it to appear in little-endian
423 * format for transmission.
425 template<typename T>
426 MOZ_WARN_UNUSED_RESULT static T swapToLittleEndian(T aValue)
428 return maybeSwap<ThisEndian, Little>(aValue);
432 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
433 * them to little-endian format if ThisEndian is Big.
434 * As with memcpy, |aDest| and |aSrc| must not overlap.
436 template<typename T>
437 static void copyAndSwapToLittleEndian(void* aDest, const T* aSrc,
438 size_t aCount)
440 copyAndSwapTo<ThisEndian, Little>(aDest, aSrc, aCount);
444 * Likewise, but converts values in place.
446 template<typename T>
447 static void swapToLittleEndianInPlace(T* aPtr, size_t aCount)
449 maybeSwapInPlace<ThisEndian, Little>(aPtr, aCount);
453 * Converts a value of type T to big-endian format.
455 template<typename T>
456 MOZ_WARN_UNUSED_RESULT static T swapToBigEndian(T aValue)
458 return maybeSwap<ThisEndian, Big>(aValue);
462 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
463 * them to big-endian format if ThisEndian is Little.
464 * As with memcpy, |aDest| and |aSrc| must not overlap.
466 template<typename T>
467 static void copyAndSwapToBigEndian(void* aDest, const T* aSrc,
468 size_t aCount)
470 copyAndSwapTo<ThisEndian, Big>(aDest, aSrc, aCount);
474 * Likewise, but converts values in place.
476 template<typename T>
477 static void swapToBigEndianInPlace(T* aPtr, size_t aCount)
479 maybeSwapInPlace<ThisEndian, Big>(aPtr, aCount);
483 * Synonyms for the big-endian functions, for better readability
484 * in network code.
487 template<typename T>
488 MOZ_WARN_UNUSED_RESULT static T swapToNetworkOrder(T aValue)
490 return swapToBigEndian(aValue);
493 template<typename T>
494 static void
495 copyAndSwapToNetworkOrder(void* aDest, const T* aSrc, size_t aCount)
497 copyAndSwapToBigEndian(aDest, aSrc, aCount);
500 template<typename T>
501 static void
502 swapToNetworkOrderInPlace(T* aPtr, size_t aCount)
504 swapToBigEndianInPlace(aPtr, aCount);
508 * Converts a value of type T from little-endian format.
510 template<typename T>
511 MOZ_WARN_UNUSED_RESULT static T swapFromLittleEndian(T aValue)
513 return maybeSwap<Little, ThisEndian>(aValue);
517 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
518 * them to little-endian format if ThisEndian is Big.
519 * As with memcpy, |aDest| and |aSrc| must not overlap.
521 template<typename T>
522 static void copyAndSwapFromLittleEndian(T* aDest, const void* aSrc,
523 size_t aCount)
525 copyAndSwapFrom<Little, ThisEndian>(aDest, aSrc, aCount);
529 * Likewise, but converts values in place.
531 template<typename T>
532 static void swapFromLittleEndianInPlace(T* aPtr, size_t aCount)
534 maybeSwapInPlace<Little, ThisEndian>(aPtr, aCount);
538 * Converts a value of type T from big-endian format.
540 template<typename T>
541 MOZ_WARN_UNUSED_RESULT static T swapFromBigEndian(T aValue)
543 return maybeSwap<Big, ThisEndian>(aValue);
547 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
548 * them to big-endian format if ThisEndian is Little.
549 * As with memcpy, |aDest| and |aSrc| must not overlap.
551 template<typename T>
552 static void copyAndSwapFromBigEndian(T* aDest, const void* aSrc,
553 size_t aCount)
555 copyAndSwapFrom<Big, ThisEndian>(aDest, aSrc, aCount);
559 * Likewise, but converts values in place.
561 template<typename T>
562 static void swapFromBigEndianInPlace(T* aPtr, size_t aCount)
564 maybeSwapInPlace<Big, ThisEndian>(aPtr, aCount);
568 * Synonyms for the big-endian functions, for better readability
569 * in network code.
571 template<typename T>
572 MOZ_WARN_UNUSED_RESULT static T swapFromNetworkOrder(T aValue)
574 return swapFromBigEndian(aValue);
577 template<typename T>
578 static void copyAndSwapFromNetworkOrder(T* aDest, const void* aSrc,
579 size_t aCount)
581 copyAndSwapFromBigEndian(aDest, aSrc, aCount);
584 template<typename T>
585 static void swapFromNetworkOrderInPlace(T* aPtr, size_t aCount)
587 swapFromBigEndianInPlace(aPtr, aCount);
590 private:
592 * Read a value of type T, encoded in endianness ThisEndian from |aPtr|.
593 * Return that value encoded in native endianness.
595 template<typename T>
596 static T read(const void* aPtr)
598 union
600 T mVal;
601 uint8_t mBuffer[sizeof(T)];
602 } u;
603 memcpy(u.mBuffer, aPtr, sizeof(T));
604 return maybeSwap<ThisEndian, MOZ_NATIVE_ENDIANNESS>(u.mVal);
608 * Write a value of type T, in native endianness, to |aPtr|, in ThisEndian
609 * endianness.
611 template<typename T>
612 static void write(void* aPtr, T aValue)
614 T tmp = maybeSwap<MOZ_NATIVE_ENDIANNESS, ThisEndian>(aValue);
615 memcpy(aPtr, &tmp, sizeof(T));
618 Endian() = delete;
619 Endian(const Endian& aTther) = delete;
620 void operator=(const Endian& aOther) = delete;
623 template<Endianness ThisEndian>
624 class EndianReadWrite : public Endian<ThisEndian>
626 private:
627 typedef Endian<ThisEndian> super;
629 public:
630 using super::readUint16;
631 using super::readUint32;
632 using super::readUint64;
633 using super::readInt16;
634 using super::readInt32;
635 using super::readInt64;
636 using super::writeUint16;
637 using super::writeUint32;
638 using super::writeUint64;
639 using super::writeInt16;
640 using super::writeInt32;
641 using super::writeInt64;
644 } /* namespace detail */
646 class LittleEndian MOZ_FINAL : public detail::EndianReadWrite<detail::Little>
649 class BigEndian MOZ_FINAL : public detail::EndianReadWrite<detail::Big>
652 typedef BigEndian NetworkEndian;
654 class NativeEndian MOZ_FINAL : public detail::Endian<MOZ_NATIVE_ENDIANNESS>
656 private:
657 typedef detail::Endian<MOZ_NATIVE_ENDIANNESS> super;
659 public:
661 * These functions are intended for cases where you have data in your
662 * native-endian format and you need the data to appear in the appropriate
663 * endianness for transmission, serialization, etc.
665 using super::swapToLittleEndian;
666 using super::copyAndSwapToLittleEndian;
667 using super::swapToLittleEndianInPlace;
668 using super::swapToBigEndian;
669 using super::copyAndSwapToBigEndian;
670 using super::swapToBigEndianInPlace;
671 using super::swapToNetworkOrder;
672 using super::copyAndSwapToNetworkOrder;
673 using super::swapToNetworkOrderInPlace;
676 * These functions are intended for cases where you have data in the
677 * given endianness (e.g. reading from disk or a file-format) and you
678 * need the data to appear in native-endian format for processing.
680 using super::swapFromLittleEndian;
681 using super::copyAndSwapFromLittleEndian;
682 using super::swapFromLittleEndianInPlace;
683 using super::swapFromBigEndian;
684 using super::copyAndSwapFromBigEndian;
685 using super::swapFromBigEndianInPlace;
686 using super::swapFromNetworkOrder;
687 using super::copyAndSwapFromNetworkOrder;
688 using super::swapFromNetworkOrderInPlace;
691 #undef MOZ_NATIVE_ENDIANNESS
693 } /* namespace mozilla */
695 #endif /* mozilla_Endian_h */