Bug 1551001 [wpt PR 16740] - Don't mark disconnected tree-scopes for style update...
[gecko.git] / mfbt / EndianUtils.h
blob8a8a0e4a8452254fc27d914cb9e4037406bbd7cc
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 addresses read from or written
13 * to may be misaligned (although misaligned accesses may incur
14 * architecture-specific performance costs). The naming scheme is:
16 * {Little,Big}Endian::{read,write}{Uint,Int}<bitsize>
18 * For instance, LittleEndian::readInt32 will read a 32-bit signed
19 * integer from memory in little endian format. Similarly,
20 * BigEndian::writeUint16 will write a 16-bit unsigned integer to memory
21 * in big-endian format.
23 * The class NativeEndian exposes methods for conversion of existing
24 * data to and from the native endianness. These methods are intended
25 * for cases where data needs to be transferred, serialized, etc.
26 * swap{To,From}{Little,Big}Endian byteswap a single value if necessary.
27 * Bulk conversion functions are also provided which optimize the
28 * no-conversion-needed case:
30 * - copyAndSwap{To,From}{Little,Big}Endian;
31 * - swap{To,From}{Little,Big}EndianInPlace.
33 * The *From* variants are intended to be used for reading data and the
34 * *To* variants for writing data.
36 * Methods on NativeEndian work with integer data of any type.
37 * Floating-point data is not supported.
39 * For clarity in networking code, "Network" may be used as a synonym
40 * for "Big" in any of the above methods or class names.
42 * As an example, reading a file format header whose fields are stored
43 * in big-endian format might look like:
45 * class ExampleHeader
46 * {
47 * private:
48 * uint32_t mMagic;
49 * uint32_t mLength;
50 * uint32_t mTotalRecords;
51 * uint64_t mChecksum;
53 * public:
54 * ExampleHeader(const void* data)
55 * {
56 * const uint8_t* ptr = static_cast<const uint8_t*>(data);
57 * mMagic = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
58 * mLength = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
59 * mTotalRecords = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
60 * mChecksum = BigEndian::readUint64(ptr);
61 * }
62 * ...
63 * };
66 #ifndef mozilla_EndianUtils_h
67 #define mozilla_EndianUtils_h
69 #include "mozilla/Assertions.h"
70 #include "mozilla/Attributes.h"
71 #include "mozilla/Compiler.h"
72 #include "mozilla/DebugOnly.h"
73 #include "mozilla/TypeTraits.h"
75 #include <stdint.h>
76 #include <string.h>
78 #if defined(_MSC_VER)
79 # include <stdlib.h>
80 # pragma intrinsic(_byteswap_ushort)
81 # pragma intrinsic(_byteswap_ulong)
82 # pragma intrinsic(_byteswap_uint64)
83 #endif
85 #if defined(_WIN64)
86 # if defined(_M_X64) || defined(_M_AMD64) || defined(_AMD64_)
87 # define MOZ_LITTLE_ENDIAN 1
88 # elif defined(_M_ARM64)
89 # define MOZ_LITTLE_ENDIAN 1
90 # else
91 # error "CPU type is unknown"
92 # endif
93 #elif defined(_WIN32)
94 # if defined(_M_IX86)
95 # define MOZ_LITTLE_ENDIAN 1
96 # elif defined(_M_ARM)
97 # define MOZ_LITTLE_ENDIAN 1
98 # else
99 # error "CPU type is unknown"
100 # endif
101 #elif defined(__APPLE__) || defined(__powerpc__) || defined(__ppc__)
102 # if __LITTLE_ENDIAN__
103 # define MOZ_LITTLE_ENDIAN 1
104 # elif __BIG_ENDIAN__
105 # define MOZ_BIG_ENDIAN 1
106 # endif
107 #elif defined(__GNUC__) && defined(__BYTE_ORDER__) && \
108 defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__)
110 * Some versions of GCC provide architecture-independent macros for
111 * this. Yes, there are more than two values for __BYTE_ORDER__.
113 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
114 # define MOZ_LITTLE_ENDIAN 1
115 # elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
116 # define MOZ_BIG_ENDIAN 1
117 # else
118 # error "Can't handle mixed-endian architectures"
119 # endif
121 * We can't include useful headers like <endian.h> or <sys/isa_defs.h>
122 * here because they're not present on all platforms. Instead we have
123 * this big conditional that ideally will catch all the interesting
124 * cases.
126 #elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || \
127 defined(__hppa) || defined(_MIPSEB) || defined(__ARMEB__) || \
128 defined(__s390__) || defined(__AARCH64EB__) || \
129 (defined(__sh__) && defined(__LITTLE_ENDIAN__)) || \
130 (defined(__ia64) && defined(__BIG_ENDIAN__))
131 # define MOZ_BIG_ENDIAN 1
132 #elif defined(__i386) || defined(__i386__) || defined(__x86_64) || \
133 defined(__x86_64__) || defined(_MIPSEL) || defined(__ARMEL__) || \
134 defined(__alpha__) || defined(__AARCH64EL__) || \
135 (defined(__sh__) && defined(__BIG_ENDIAN__)) || \
136 (defined(__ia64) && !defined(__BIG_ENDIAN__))
137 # define MOZ_LITTLE_ENDIAN 1
138 #endif
140 #if MOZ_BIG_ENDIAN
141 # define MOZ_LITTLE_ENDIAN 0
142 #elif MOZ_LITTLE_ENDIAN
143 # define MOZ_BIG_ENDIAN 0
144 #else
145 # error "Cannot determine endianness"
146 #endif
148 #if defined(__clang__)
149 # if __has_builtin(__builtin_bswap16)
150 # define MOZ_HAVE_BUILTIN_BYTESWAP16 __builtin_bswap16
151 # endif
152 #elif defined(__GNUC__)
153 # define MOZ_HAVE_BUILTIN_BYTESWAP16 __builtin_bswap16
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> {
172 static T swap(T aValue) {
173 #if defined(MOZ_HAVE_BUILTIN_BYTESWAP16)
174 return MOZ_HAVE_BUILTIN_BYTESWAP16(aValue);
175 #else
176 return T(((aValue & 0x00ff) << 8) | ((aValue & 0xff00) >> 8));
177 #endif
181 template <typename T>
182 struct Swapper<T, 4> {
183 static T swap(T aValue) {
184 #if defined(__clang__) || defined(__GNUC__)
185 return T(__builtin_bswap32(aValue));
186 #elif defined(_MSC_VER)
187 return T(_byteswap_ulong(aValue));
188 #else
189 return T(((aValue & 0x000000ffU) << 24) | ((aValue & 0x0000ff00U) << 8) |
190 ((aValue & 0x00ff0000U) >> 8) | ((aValue & 0xff000000U) >> 24));
191 #endif
195 template <typename T>
196 struct Swapper<T, 8> {
197 static inline T swap(T aValue) {
198 #if defined(__clang__) || defined(__GNUC__)
199 return T(__builtin_bswap64(aValue));
200 #elif defined(_MSC_VER)
201 return T(_byteswap_uint64(aValue));
202 #else
203 return T(((aValue & 0x00000000000000ffULL) << 56) |
204 ((aValue & 0x000000000000ff00ULL) << 40) |
205 ((aValue & 0x0000000000ff0000ULL) << 24) |
206 ((aValue & 0x00000000ff000000ULL) << 8) |
207 ((aValue & 0x000000ff00000000ULL) >> 8) |
208 ((aValue & 0x0000ff0000000000ULL) >> 24) |
209 ((aValue & 0x00ff000000000000ULL) >> 40) |
210 ((aValue & 0xff00000000000000ULL) >> 56));
211 #endif
215 enum Endianness { Little, Big };
217 #if MOZ_BIG_ENDIAN
218 # define MOZ_NATIVE_ENDIANNESS detail::Big
219 #else
220 # define MOZ_NATIVE_ENDIANNESS detail::Little
221 #endif
223 class EndianUtils {
225 * Assert that the memory regions [aDest, aDest+aCount) and
226 * [aSrc, aSrc+aCount] do not overlap. aCount is given in bytes.
228 static void assertNoOverlap(const void* aDest, const void* aSrc,
229 size_t aCount) {
230 DebugOnly<const uint8_t*> byteDestPtr = static_cast<const uint8_t*>(aDest);
231 DebugOnly<const uint8_t*> byteSrcPtr = static_cast<const uint8_t*>(aSrc);
232 MOZ_ASSERT(
233 (byteDestPtr <= byteSrcPtr && byteDestPtr + aCount <= byteSrcPtr) ||
234 (byteSrcPtr <= byteDestPtr && byteSrcPtr + aCount <= byteDestPtr));
237 template <typename T>
238 static void assertAligned(T* aPtr) {
239 MOZ_ASSERT((uintptr_t(aPtr) % sizeof(T)) == 0, "Unaligned pointer!");
242 protected:
244 * Return |aValue| converted from SourceEndian encoding to DestEndian
245 * encoding.
247 template <Endianness SourceEndian, Endianness DestEndian, typename T>
248 static inline T maybeSwap(T aValue) {
249 if (SourceEndian == DestEndian) {
250 return aValue;
252 return Swapper<T>::swap(aValue);
256 * Convert |aCount| elements at |aPtr| from SourceEndian encoding to
257 * DestEndian encoding.
259 template <Endianness SourceEndian, Endianness DestEndian, typename T>
260 static inline void maybeSwapInPlace(T* aPtr, size_t aCount) {
261 assertAligned(aPtr);
263 if (SourceEndian == DestEndian) {
264 return;
266 for (size_t i = 0; i < aCount; i++) {
267 aPtr[i] = Swapper<T>::swap(aPtr[i]);
272 * Write |aCount| elements to the unaligned address |aDest| in DestEndian
273 * format, using elements found at |aSrc| in SourceEndian format.
275 template <Endianness SourceEndian, Endianness DestEndian, typename T>
276 static void copyAndSwapTo(void* aDest, const T* aSrc, size_t aCount) {
277 assertNoOverlap(aDest, aSrc, aCount * sizeof(T));
278 assertAligned(aSrc);
280 if (SourceEndian == DestEndian) {
281 memcpy(aDest, aSrc, aCount * sizeof(T));
282 return;
285 uint8_t* byteDestPtr = static_cast<uint8_t*>(aDest);
286 for (size_t i = 0; i < aCount; ++i) {
287 union {
288 T mVal;
289 uint8_t mBuffer[sizeof(T)];
290 } u;
291 u.mVal = maybeSwap<SourceEndian, DestEndian>(aSrc[i]);
292 memcpy(byteDestPtr, u.mBuffer, sizeof(T));
293 byteDestPtr += sizeof(T);
298 * Write |aCount| elements to |aDest| in DestEndian format, using elements
299 * found at the unaligned address |aSrc| in SourceEndian format.
301 template <Endianness SourceEndian, Endianness DestEndian, typename T>
302 static void copyAndSwapFrom(T* aDest, const void* aSrc, size_t aCount) {
303 assertNoOverlap(aDest, aSrc, aCount * sizeof(T));
304 assertAligned(aDest);
306 if (SourceEndian == DestEndian) {
307 memcpy(aDest, aSrc, aCount * sizeof(T));
308 return;
311 const uint8_t* byteSrcPtr = static_cast<const uint8_t*>(aSrc);
312 for (size_t i = 0; i < aCount; ++i) {
313 union {
314 T mVal;
315 uint8_t mBuffer[sizeof(T)];
316 } u;
317 memcpy(u.mBuffer, byteSrcPtr, sizeof(T));
318 aDest[i] = maybeSwap<SourceEndian, DestEndian>(u.mVal);
319 byteSrcPtr += sizeof(T);
324 template <Endianness ThisEndian>
325 class Endian : private EndianUtils {
326 protected:
327 /** Read a uint16_t in ThisEndian endianness from |aPtr| and return it. */
328 static MOZ_MUST_USE uint16_t readUint16(const void* aPtr) {
329 return read<uint16_t>(aPtr);
332 /** Read a uint32_t in ThisEndian endianness from |aPtr| and return it. */
333 static MOZ_MUST_USE uint32_t readUint32(const void* aPtr) {
334 return read<uint32_t>(aPtr);
337 /** Read a uint64_t in ThisEndian endianness from |aPtr| and return it. */
338 static MOZ_MUST_USE uint64_t readUint64(const void* aPtr) {
339 return read<uint64_t>(aPtr);
342 /** Read a uintptr_t in ThisEndian endianness from |aPtr| and return it. */
343 static MOZ_MUST_USE uintptr_t readUintptr(const void* aPtr) {
344 return read<uintptr_t>(aPtr);
347 /** Read an int16_t in ThisEndian endianness from |aPtr| and return it. */
348 static MOZ_MUST_USE int16_t readInt16(const void* aPtr) {
349 return read<int16_t>(aPtr);
352 /** Read an int32_t in ThisEndian endianness from |aPtr| and return it. */
353 static MOZ_MUST_USE int32_t readInt32(const void* aPtr) {
354 return read<uint32_t>(aPtr);
357 /** Read an int64_t in ThisEndian endianness from |aPtr| and return it. */
358 static MOZ_MUST_USE int64_t readInt64(const void* aPtr) {
359 return read<int64_t>(aPtr);
362 /** Read an intptr_t in ThisEndian endianness from |aPtr| and return it. */
363 static MOZ_MUST_USE intptr_t readIntptr(const void* aPtr) {
364 return read<intptr_t>(aPtr);
367 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
368 static void writeUint16(void* aPtr, uint16_t aValue) { write(aPtr, aValue); }
370 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
371 static void writeUint32(void* aPtr, uint32_t aValue) { write(aPtr, aValue); }
373 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
374 static void writeUint64(void* aPtr, uint64_t aValue) { write(aPtr, aValue); }
376 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
377 static void writeUintptr(void* aPtr, uintptr_t aValue) {
378 write(aPtr, aValue);
381 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
382 static void writeInt16(void* aPtr, int16_t aValue) { write(aPtr, aValue); }
384 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
385 static void writeInt32(void* aPtr, int32_t aValue) { write(aPtr, aValue); }
387 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
388 static void writeInt64(void* aPtr, int64_t aValue) { write(aPtr, aValue); }
390 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
391 static void writeIntptr(void* aPtr, intptr_t aValue) { write(aPtr, aValue); }
394 * Converts a value of type T to little-endian format.
396 * This function is intended for cases where you have data in your
397 * native-endian format and you need it to appear in little-endian
398 * format for transmission.
400 template <typename T>
401 MOZ_MUST_USE static T swapToLittleEndian(T aValue) {
402 return maybeSwap<ThisEndian, Little>(aValue);
406 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
407 * them to little-endian format if ThisEndian is Big. |aSrc| as a typed
408 * pointer must be aligned; |aDest| need not be.
410 * As with memcpy, |aDest| and |aSrc| must not overlap.
412 template <typename T>
413 static void copyAndSwapToLittleEndian(void* aDest, const T* aSrc,
414 size_t aCount) {
415 copyAndSwapTo<ThisEndian, Little>(aDest, aSrc, aCount);
419 * Likewise, but converts values in place.
421 template <typename T>
422 static void swapToLittleEndianInPlace(T* aPtr, size_t aCount) {
423 maybeSwapInPlace<ThisEndian, Little>(aPtr, aCount);
427 * Converts a value of type T to big-endian format.
429 template <typename T>
430 MOZ_MUST_USE static T swapToBigEndian(T aValue) {
431 return maybeSwap<ThisEndian, Big>(aValue);
435 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
436 * them to big-endian format if ThisEndian is Little. |aSrc| as a typed
437 * pointer must be aligned; |aDest| need not be.
439 * As with memcpy, |aDest| and |aSrc| must not overlap.
441 template <typename T>
442 static void copyAndSwapToBigEndian(void* aDest, const T* aSrc,
443 size_t aCount) {
444 copyAndSwapTo<ThisEndian, Big>(aDest, aSrc, aCount);
448 * Likewise, but converts values in place.
450 template <typename T>
451 static void swapToBigEndianInPlace(T* aPtr, size_t aCount) {
452 maybeSwapInPlace<ThisEndian, Big>(aPtr, aCount);
456 * Synonyms for the big-endian functions, for better readability
457 * in network code.
460 template <typename T>
461 MOZ_MUST_USE static T swapToNetworkOrder(T aValue) {
462 return swapToBigEndian(aValue);
465 template <typename T>
466 static void copyAndSwapToNetworkOrder(void* aDest, const T* aSrc,
467 size_t aCount) {
468 copyAndSwapToBigEndian(aDest, aSrc, aCount);
471 template <typename T>
472 static void swapToNetworkOrderInPlace(T* aPtr, size_t aCount) {
473 swapToBigEndianInPlace(aPtr, aCount);
477 * Converts a value of type T from little-endian format.
479 template <typename T>
480 MOZ_MUST_USE static T swapFromLittleEndian(T aValue) {
481 return maybeSwap<Little, ThisEndian>(aValue);
485 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
486 * them to little-endian format if ThisEndian is Big. |aDest| as a typed
487 * pointer must be aligned; |aSrc| need not be.
489 * As with memcpy, |aDest| and |aSrc| must not overlap.
491 template <typename T>
492 static void copyAndSwapFromLittleEndian(T* aDest, const void* aSrc,
493 size_t aCount) {
494 copyAndSwapFrom<Little, ThisEndian>(aDest, aSrc, aCount);
498 * Likewise, but converts values in place.
500 template <typename T>
501 static void swapFromLittleEndianInPlace(T* aPtr, size_t aCount) {
502 maybeSwapInPlace<Little, ThisEndian>(aPtr, aCount);
506 * Converts a value of type T from big-endian format.
508 template <typename T>
509 MOZ_MUST_USE static T swapFromBigEndian(T aValue) {
510 return maybeSwap<Big, ThisEndian>(aValue);
514 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
515 * them to big-endian format if ThisEndian is Little. |aDest| as a typed
516 * pointer must be aligned; |aSrc| need not be.
518 * As with memcpy, |aDest| and |aSrc| must not overlap.
520 template <typename T>
521 static void copyAndSwapFromBigEndian(T* aDest, const void* aSrc,
522 size_t aCount) {
523 copyAndSwapFrom<Big, ThisEndian>(aDest, aSrc, aCount);
527 * Likewise, but converts values in place.
529 template <typename T>
530 static void swapFromBigEndianInPlace(T* aPtr, size_t aCount) {
531 maybeSwapInPlace<Big, ThisEndian>(aPtr, aCount);
535 * Synonyms for the big-endian functions, for better readability
536 * in network code.
538 template <typename T>
539 MOZ_MUST_USE static T swapFromNetworkOrder(T aValue) {
540 return swapFromBigEndian(aValue);
543 template <typename T>
544 static void copyAndSwapFromNetworkOrder(T* aDest, const void* aSrc,
545 size_t aCount) {
546 copyAndSwapFromBigEndian(aDest, aSrc, aCount);
549 template <typename T>
550 static void swapFromNetworkOrderInPlace(T* aPtr, size_t aCount) {
551 swapFromBigEndianInPlace(aPtr, aCount);
554 private:
556 * Read a value of type T, encoded in endianness ThisEndian from |aPtr|.
557 * Return that value encoded in native endianness.
559 template <typename T>
560 static T read(const void* aPtr) {
561 union {
562 T mVal;
563 uint8_t mBuffer[sizeof(T)];
564 } u;
565 memcpy(u.mBuffer, aPtr, sizeof(T));
566 return maybeSwap<ThisEndian, MOZ_NATIVE_ENDIANNESS>(u.mVal);
570 * Write a value of type T, in native endianness, to |aPtr|, in ThisEndian
571 * endianness.
573 template <typename T>
574 static void write(void* aPtr, T aValue) {
575 T tmp = maybeSwap<MOZ_NATIVE_ENDIANNESS, ThisEndian>(aValue);
576 memcpy(aPtr, &tmp, sizeof(T));
579 Endian() = delete;
580 Endian(const Endian& aTther) = delete;
581 void operator=(const Endian& aOther) = delete;
584 template <Endianness ThisEndian>
585 class EndianReadWrite : public Endian<ThisEndian> {
586 private:
587 typedef Endian<ThisEndian> super;
589 public:
590 using super::readInt16;
591 using super::readInt32;
592 using super::readInt64;
593 using super::readIntptr;
594 using super::readUint16;
595 using super::readUint32;
596 using super::readUint64;
597 using super::readUintptr;
598 using super::writeInt16;
599 using super::writeInt32;
600 using super::writeInt64;
601 using super::writeIntptr;
602 using super::writeUint16;
603 using super::writeUint32;
604 using super::writeUint64;
605 using super::writeUintptr;
608 } /* namespace detail */
610 class LittleEndian final : public detail::EndianReadWrite<detail::Little> {};
612 class BigEndian final : public detail::EndianReadWrite<detail::Big> {};
614 typedef BigEndian NetworkEndian;
616 class NativeEndian final : public detail::Endian<MOZ_NATIVE_ENDIANNESS> {
617 private:
618 typedef detail::Endian<MOZ_NATIVE_ENDIANNESS> super;
620 public:
622 * These functions are intended for cases where you have data in your
623 * native-endian format and you need the data to appear in the appropriate
624 * endianness for transmission, serialization, etc.
626 using super::copyAndSwapToBigEndian;
627 using super::copyAndSwapToLittleEndian;
628 using super::copyAndSwapToNetworkOrder;
629 using super::swapToBigEndian;
630 using super::swapToBigEndianInPlace;
631 using super::swapToLittleEndian;
632 using super::swapToLittleEndianInPlace;
633 using super::swapToNetworkOrder;
634 using super::swapToNetworkOrderInPlace;
637 * These functions are intended for cases where you have data in the
638 * given endianness (e.g. reading from disk or a file-format) and you
639 * need the data to appear in native-endian format for processing.
641 using super::copyAndSwapFromBigEndian;
642 using super::copyAndSwapFromLittleEndian;
643 using super::copyAndSwapFromNetworkOrder;
644 using super::swapFromBigEndian;
645 using super::swapFromBigEndianInPlace;
646 using super::swapFromLittleEndian;
647 using super::swapFromLittleEndianInPlace;
648 using super::swapFromNetworkOrder;
649 using super::swapFromNetworkOrderInPlace;
652 #undef MOZ_NATIVE_ENDIANNESS
654 } /* namespace mozilla */
656 #endif /* mozilla_EndianUtils_h */