Home | History | Annotate | Download | only in Support
      1 //===- Endian.h - Utilities for IO with endian specific data ----*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file declares generic functions to read and write endian specific data.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_SUPPORT_ENDIAN_H
     15 #define LLVM_SUPPORT_ENDIAN_H
     16 
     17 #include "llvm/Support/AlignOf.h"
     18 #include "llvm/Support/Host.h"
     19 #include "llvm/Support/SwapByteOrder.h"
     20 
     21 namespace llvm {
     22 namespace support {
     23 enum endianness {big, little, native};
     24 
     25 // These are named values for common alignments.
     26 enum {aligned = 0, unaligned = 1};
     27 
     28 namespace detail {
     29   /// \brief ::value is either alignment, or alignof(T) if alignment is 0.
     30   template<class T, int alignment>
     31   struct PickAlignment {
     32     enum {value = alignment == 0 ? AlignOf<T>::Alignment : alignment};
     33   };
     34 } // end namespace detail
     35 
     36 namespace endian {
     37 /// Swap the bytes of value to match the given endianness.
     38 template<typename value_type, endianness endian>
     39 inline value_type byte_swap(value_type value) {
     40   if (endian != native && sys::IsBigEndianHost != (endian == big))
     41     sys::swapByteOrder(value);
     42   return value;
     43 }
     44 
     45 /// Read a value of a particular endianness from memory.
     46 template<typename value_type,
     47          endianness endian,
     48          std::size_t alignment>
     49 inline value_type read(const void *memory) {
     50   value_type ret;
     51 
     52   memcpy(&ret,
     53          LLVM_ASSUME_ALIGNED(memory,
     54            (detail::PickAlignment<value_type, alignment>::value)),
     55          sizeof(value_type));
     56   return byte_swap<value_type, endian>(ret);
     57 }
     58 
     59 /// Read a value of a particular endianness from a buffer, and increment the
     60 /// buffer past that value.
     61 template<typename value_type, endianness endian, std::size_t alignment>
     62 inline value_type readNext(const unsigned char *&memory) {
     63   value_type ret = read<value_type, endian, alignment>(memory);
     64   memory += sizeof(value_type);
     65   return ret;
     66 }
     67 
     68 /// Write a value to memory with a particular endianness.
     69 template<typename value_type,
     70          endianness endian,
     71          std::size_t alignment>
     72 inline void write(void *memory, value_type value) {
     73   value = byte_swap<value_type, endian>(value);
     74   memcpy(LLVM_ASSUME_ALIGNED(memory,
     75            (detail::PickAlignment<value_type, alignment>::value)),
     76          &value,
     77          sizeof(value_type));
     78 }
     79 } // end namespace endian
     80 
     81 namespace detail {
     82 template<typename value_type,
     83          endianness endian,
     84          std::size_t alignment>
     85 struct packed_endian_specific_integral {
     86   operator value_type() const {
     87     return endian::read<value_type, endian, alignment>(
     88       (const void*)Value.buffer);
     89   }
     90 
     91   void operator=(value_type newValue) {
     92     endian::write<value_type, endian, alignment>(
     93       (void*)Value.buffer, newValue);
     94   }
     95 
     96 private:
     97   AlignedCharArray<PickAlignment<value_type, alignment>::value,
     98                    sizeof(value_type)> Value;
     99 };
    100 } // end namespace detail
    101 
    102 typedef detail::packed_endian_specific_integral
    103                   <uint8_t, little, unaligned>  ulittle8_t;
    104 typedef detail::packed_endian_specific_integral
    105                   <uint16_t, little, unaligned> ulittle16_t;
    106 typedef detail::packed_endian_specific_integral
    107                   <uint32_t, little, unaligned> ulittle32_t;
    108 typedef detail::packed_endian_specific_integral
    109                   <uint64_t, little, unaligned> ulittle64_t;
    110 
    111 typedef detail::packed_endian_specific_integral
    112                    <int8_t, little, unaligned>  little8_t;
    113 typedef detail::packed_endian_specific_integral
    114                    <int16_t, little, unaligned> little16_t;
    115 typedef detail::packed_endian_specific_integral
    116                    <int32_t, little, unaligned> little32_t;
    117 typedef detail::packed_endian_specific_integral
    118                    <int64_t, little, unaligned> little64_t;
    119 
    120 typedef detail::packed_endian_specific_integral
    121                     <uint8_t, little, aligned>  aligned_ulittle8_t;
    122 typedef detail::packed_endian_specific_integral
    123                     <uint16_t, little, aligned> aligned_ulittle16_t;
    124 typedef detail::packed_endian_specific_integral
    125                     <uint32_t, little, aligned> aligned_ulittle32_t;
    126 typedef detail::packed_endian_specific_integral
    127                     <uint64_t, little, aligned> aligned_ulittle64_t;
    128 
    129 typedef detail::packed_endian_specific_integral
    130                      <int8_t, little, aligned>  aligned_little8_t;
    131 typedef detail::packed_endian_specific_integral
    132                      <int16_t, little, aligned> aligned_little16_t;
    133 typedef detail::packed_endian_specific_integral
    134                      <int32_t, little, aligned> aligned_little32_t;
    135 typedef detail::packed_endian_specific_integral
    136                      <int64_t, little, aligned> aligned_little64_t;
    137 
    138 typedef detail::packed_endian_specific_integral
    139                   <uint8_t, big, unaligned>     ubig8_t;
    140 typedef detail::packed_endian_specific_integral
    141                   <uint16_t, big, unaligned>    ubig16_t;
    142 typedef detail::packed_endian_specific_integral
    143                   <uint32_t, big, unaligned>    ubig32_t;
    144 typedef detail::packed_endian_specific_integral
    145                   <uint64_t, big, unaligned>    ubig64_t;
    146 
    147 typedef detail::packed_endian_specific_integral
    148                    <int8_t, big, unaligned>     big8_t;
    149 typedef detail::packed_endian_specific_integral
    150                    <int16_t, big, unaligned>    big16_t;
    151 typedef detail::packed_endian_specific_integral
    152                    <int32_t, big, unaligned>    big32_t;
    153 typedef detail::packed_endian_specific_integral
    154                    <int64_t, big, unaligned>    big64_t;
    155 
    156 typedef detail::packed_endian_specific_integral
    157                     <uint8_t, big, aligned>     aligned_ubig8_t;
    158 typedef detail::packed_endian_specific_integral
    159                     <uint16_t, big, aligned>    aligned_ubig16_t;
    160 typedef detail::packed_endian_specific_integral
    161                     <uint32_t, big, aligned>    aligned_ubig32_t;
    162 typedef detail::packed_endian_specific_integral
    163                     <uint64_t, big, aligned>    aligned_ubig64_t;
    164 
    165 typedef detail::packed_endian_specific_integral
    166                      <int8_t, big, aligned>     aligned_big8_t;
    167 typedef detail::packed_endian_specific_integral
    168                      <int16_t, big, aligned>    aligned_big16_t;
    169 typedef detail::packed_endian_specific_integral
    170                      <int32_t, big, aligned>    aligned_big32_t;
    171 typedef detail::packed_endian_specific_integral
    172                      <int64_t, big, aligned>    aligned_big64_t;
    173 
    174 typedef detail::packed_endian_specific_integral
    175                   <uint16_t, native, unaligned> unaligned_uint16_t;
    176 typedef detail::packed_endian_specific_integral
    177                   <uint32_t, native, unaligned> unaligned_uint32_t;
    178 typedef detail::packed_endian_specific_integral
    179                   <uint64_t, native, unaligned> unaligned_uint64_t;
    180 
    181 typedef detail::packed_endian_specific_integral
    182                    <int16_t, native, unaligned> unaligned_int16_t;
    183 typedef detail::packed_endian_specific_integral
    184                    <int32_t, native, unaligned> unaligned_int32_t;
    185 typedef detail::packed_endian_specific_integral
    186                    <int64_t, native, unaligned> unaligned_int64_t;
    187 } // end namespace llvm
    188 } // end namespace support
    189 
    190 #endif
    191