Skip to content

Commit

Permalink
[ntcore] Use Endian.h in WireEncoder3
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Oct 22, 2024
1 parent 9a6ef9f commit 207df78
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions ntcore/src/main/native/cpp/net3/WireEncoder3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "WireEncoder3.h"

#include <wpi/Endian.h>
#include <wpi/MathExtras.h>
#include <wpi/SmallVector.h>
#include <wpi/leb128.h>
Expand All @@ -19,28 +20,21 @@ static void Write8(wpi::raw_ostream& os, uint8_t val) {
}

static void Write16(wpi::raw_ostream& os, uint16_t val) {
os << std::span<const uint8_t>{{static_cast<uint8_t>((val >> 8) & 0xff),
static_cast<uint8_t>(val & 0xff)}};
uint8_t buf[2];
wpi::support::endian::write16be(buf, val);
os << buf;
}

static void Write32(wpi::raw_ostream& os, uint32_t val) {
os << std::span<const uint8_t>{{static_cast<uint8_t>((val >> 24) & 0xff),
static_cast<uint8_t>((val >> 16) & 0xff),
static_cast<uint8_t>((val >> 8) & 0xff),
static_cast<uint8_t>(val & 0xff)}};
uint8_t buf[4];
wpi::support::endian::write32be(buf, val);
os << buf;
}

static void WriteDouble(wpi::raw_ostream& os, double val) {
// The highest performance way to do this, albeit non-portable.
uint64_t v = wpi::bit_cast<uint64_t>(val);
os << std::span<const uint8_t>{{static_cast<uint8_t>((v >> 56) & 0xff),
static_cast<uint8_t>((v >> 48) & 0xff),
static_cast<uint8_t>((v >> 40) & 0xff),
static_cast<uint8_t>((v >> 32) & 0xff),
static_cast<uint8_t>((v >> 24) & 0xff),
static_cast<uint8_t>((v >> 16) & 0xff),
static_cast<uint8_t>((v >> 8) & 0xff),
static_cast<uint8_t>(v & 0xff)}};
uint8_t buf[8];
wpi::support::endian::write64be(buf, wpi::bit_cast<uint64_t>(val));
os << buf;
}

static void WriteString(wpi::raw_ostream& os, std::string_view str) {
Expand Down

0 comments on commit 207df78

Please sign in to comment.