Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:29

0001 //===- EndianStream.h - Stream ops with endian specific data ----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines utilities for operating on streams that have endian
0010 // specific data.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_SUPPORT_ENDIANSTREAM_H
0015 #define LLVM_SUPPORT_ENDIANSTREAM_H
0016 
0017 #include "llvm/ADT/ArrayRef.h"
0018 #include "llvm/ADT/SmallVector.h"
0019 #include "llvm/Support/Endian.h"
0020 #include "llvm/Support/raw_ostream.h"
0021 
0022 namespace llvm {
0023 namespace support {
0024 
0025 namespace endian {
0026 
0027 template <typename value_type>
0028 inline void write_array(raw_ostream &os, ArrayRef<value_type> values,
0029                         endianness endian) {
0030   for (const auto orig : values) {
0031     value_type value = byte_swap<value_type>(orig, endian);
0032     os.write((const char *)&value, sizeof(value_type));
0033   }
0034 }
0035 
0036 template <typename value_type>
0037 inline void write(raw_ostream &os, value_type value, endianness endian) {
0038   value = byte_swap<value_type>(value, endian);
0039   os.write((const char *)&value, sizeof(value_type));
0040 }
0041 
0042 template <>
0043 inline void write<float>(raw_ostream &os, float value, endianness endian) {
0044   write(os, llvm::bit_cast<uint32_t>(value), endian);
0045 }
0046 
0047 template <>
0048 inline void write<double>(raw_ostream &os, double value,
0049                           endianness endian) {
0050   write(os, llvm::bit_cast<uint64_t>(value), endian);
0051 }
0052 
0053 template <typename value_type>
0054 inline void write(raw_ostream &os, ArrayRef<value_type> vals,
0055                   endianness endian) {
0056   for (value_type v : vals)
0057     write(os, v, endian);
0058 }
0059 
0060 template <typename value_type>
0061 inline void write(SmallVectorImpl<char> &Out, value_type V, endianness E) {
0062   V = byte_swap<value_type>(V, E);
0063   Out.append((const char *)&V, (const char *)&V + sizeof(value_type));
0064 }
0065 
0066 /// Adapter to write values to a stream in a particular byte order.
0067 struct Writer {
0068   raw_ostream &OS;
0069   endianness Endian;
0070   Writer(raw_ostream &OS, endianness Endian) : OS(OS), Endian(Endian) {}
0071   template <typename value_type> void write(ArrayRef<value_type> Val) {
0072     endian::write(OS, Val, Endian);
0073   }
0074   template <typename value_type> void write(value_type Val) {
0075     endian::write(OS, Val, Endian);
0076   }
0077 };
0078 
0079 } // end namespace endian
0080 
0081 } // end namespace support
0082 } // end namespace llvm
0083 
0084 #endif