Skip to content

Commit

Permalink
Use std::bit_cast (#7567)
Browse files Browse the repository at this point in the history
Backport #7492

Co-authored-by: Tyler Veness <[email protected]>
  • Loading branch information
rzblue and calcmogul authored Dec 22, 2024
1 parent d631fa8 commit 0c99073
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 34 deletions.
4 changes: 2 additions & 2 deletions ntcore/src/main/native/cpp/net3/WireDecoder3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#include "WireDecoder3.h"

#include <algorithm>
#include <bit>
#include <string>
#include <utility>

#include <fmt/format.h>
#include <wpi/MathExtras.h>
#include <wpi/SpanExtras.h>

#include "Message3.h"
Expand Down Expand Up @@ -74,7 +74,7 @@ std::optional<uint64_t> WireDecoder3::SimpleValueReader::Read64(
std::optional<double> WireDecoder3::SimpleValueReader::ReadDouble(
std::span<const uint8_t>* in) {
if (auto val = Read64(in)) {
return wpi::bit_cast<double>(val.value());
return std::bit_cast<double>(val.value());
} else {
return std::nullopt;
}
Expand Down
5 changes: 3 additions & 2 deletions ntcore/src/main/native/cpp/net3/WireEncoder3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

#include "WireEncoder3.h"

#include <bit>

#include <wpi/Endian.h>
#include <wpi/MathExtras.h>
#include <wpi/SmallVector.h>
#include <wpi/leb128.h>
#include <wpi/raw_ostream.h>
Expand Down Expand Up @@ -33,7 +34,7 @@ static void Write32(wpi::raw_ostream& os, uint32_t val) {

static void WriteDouble(wpi::raw_ostream& os, double val) {
uint8_t buf[8];
wpi::support::endian::write64be(buf, wpi::bit_cast<uint64_t>(val));
wpi::support::endian::write64be(buf, std::bit_cast<uint64_t>(val));
os << buf;
}

Expand Down
26 changes: 13 additions & 13 deletions simulation/halsim_xrp/src/main/native/cpp/XRP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

#include "XRP.h"

#include <bit>
#include <string>

#include <fmt/format.h>
#include <wpi/Endian.h>
#include <wpi/MathExtras.h>
#include <wpi/json.h>

using namespace wpilibxrp;
Expand Down Expand Up @@ -245,7 +245,7 @@ void XRP::SetupMotorTag(wpi::raw_uv_ostream& buf) {

// Convert the value
wpi::support::endian::write32be(value,
wpi::bit_cast<uint32_t>(motor.second));
std::bit_cast<uint32_t>(motor.second));
buf << value[0] << value[1] << value[2] << value[3];
}
}
Expand All @@ -261,7 +261,7 @@ void XRP::SetupServoTag(wpi::raw_uv_ostream& buf) {

// Convert the value
wpi::support::endian::write32be(value,
wpi::bit_cast<uint32_t>(servo.second));
std::bit_cast<uint32_t>(servo.second));
buf << value[0] << value[1] << value[2] << value[3];
}
}
Expand All @@ -283,17 +283,17 @@ void XRP::ReadGyroTag(std::span<const uint8_t> packet) {

packet = packet.subspan(2); // Skip past the size and tag
float rate_x =
wpi::bit_cast<float>(wpi::support::endian::read32be(&packet[0]));
std::bit_cast<float>(wpi::support::endian::read32be(&packet[0]));
float rate_y =
wpi::bit_cast<float>(wpi::support::endian::read32be(&packet[4]));
std::bit_cast<float>(wpi::support::endian::read32be(&packet[4]));
float rate_z =
wpi::bit_cast<float>(wpi::support::endian::read32be(&packet[8]));
std::bit_cast<float>(wpi::support::endian::read32be(&packet[8]));
float angle_x =
wpi::bit_cast<float>(wpi::support::endian::read32be(&packet[12]));
std::bit_cast<float>(wpi::support::endian::read32be(&packet[12]));
float angle_y =
wpi::bit_cast<float>(wpi::support::endian::read32be(&packet[16]));
std::bit_cast<float>(wpi::support::endian::read32be(&packet[16]));
float angle_z =
wpi::bit_cast<float>(wpi::support::endian::read32be(&packet[20]));
std::bit_cast<float>(wpi::support::endian::read32be(&packet[20]));

// Make the json object
wpi::json gyroJson;
Expand All @@ -314,11 +314,11 @@ void XRP::ReadAccelTag(std::span<const uint8_t> packet) {

packet = packet.subspan(2); // Skip past the size and tag
float accel_x =
wpi::bit_cast<float>(wpi::support::endian::read32be(&packet[0]));
std::bit_cast<float>(wpi::support::endian::read32be(&packet[0]));
float accel_y =
wpi::bit_cast<float>(wpi::support::endian::read32be(&packet[4]));
std::bit_cast<float>(wpi::support::endian::read32be(&packet[4]));
float accel_z =
wpi::bit_cast<float>(wpi::support::endian::read32be(&packet[8]));
std::bit_cast<float>(wpi::support::endian::read32be(&packet[8]));

wpi::json accelJson;
accelJson["type"] = "Accel";
Expand Down Expand Up @@ -409,7 +409,7 @@ void XRP::ReadAnalogTag(std::span<const uint8_t> packet) {

packet = packet.subspan(3);
float voltage =
wpi::bit_cast<float>(wpi::support::endian::read32be(&packet[0]));
std::bit_cast<float>(wpi::support::endian::read32be(&packet[0]));

wpi::json analogJson;
analogJson["type"] = "AI";
Expand Down
5 changes: 2 additions & 3 deletions wpinet/src/netconsoleServer/native/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
#include <pty.h>
#endif

#include <bit>
#include <cstdio>
#include <memory>
#include <string>

#include <fmt/format.h>
#include <wpi/MathExtras.h>
#include <wpi/SmallVector.h>
#include <wpi/StringExtras.h>
#include <wpi/bit.h>
#include <wpi/print.h>
#include <wpi/timestamp.h>

Expand Down Expand Up @@ -52,7 +51,7 @@ static bool NewlineBuffer(std::string& rem, uv::Buffer& buf, size_t len,
if (tcp) {
// Header is 2 byte len, 1 byte type, 4 byte timestamp, 2 byte sequence num
uint32_t ts =
wpi::bit_cast<uint32_t, float>((wpi::Now() - startTime) * 1.0e-6);
std::bit_cast<uint32_t, float>((wpi::Now() - startTime) * 1.0e-6);
uint16_t len = rem.size() + toCopy.size() + 1 + 4 + 2;
const uint8_t header[] = {static_cast<uint8_t>((len >> 8) & 0xff),
static_cast<uint8_t>(len & 0xff),
Expand Down
5 changes: 2 additions & 3 deletions wpinet/src/netconsoleTee/native/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

#include <bit>
#include <cstdio>
#include <memory>
#include <string>

#include <fmt/format.h>
#include <wpi/MathExtras.h>
#include <wpi/SmallVector.h>
#include <wpi/StringExtras.h>
#include <wpi/bit.h>
#include <wpi/print.h>
#include <wpi/timestamp.h>

Expand Down Expand Up @@ -43,7 +42,7 @@ static bool NewlineBuffer(std::string& rem, uv::Buffer& buf, size_t len,
if (tcp) {
// Header is 2 byte len, 1 byte type, 4 byte timestamp, 2 byte sequence num
uint32_t ts =
wpi::bit_cast<uint32_t, float>((wpi::Now() - startTime) * 1.0e-6);
std::bit_cast<uint32_t, float>((wpi::Now() - startTime) * 1.0e-6);
uint16_t len = rem.size() + toCopy.size() + 1 + 4 + 2;
const uint8_t header[] = {static_cast<uint8_t>((len >> 8) & 0xff),
static_cast<uint8_t>(len & 0xff),
Expand Down
12 changes: 6 additions & 6 deletions wpiutil/src/main/native/cpp/DataLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ void DataLog::AppendFloat(int entry, float value, int64_t timestamp) {
if constexpr (std::endian::native == std::endian::little) {
std::memcpy(buf, &value, 4);
} else {
wpi::support::endian::write32le(buf, wpi::bit_cast<uint32_t>(value));
wpi::support::endian::write32le(buf, std::bit_cast<uint32_t>(value));
}
}

Expand All @@ -393,7 +393,7 @@ void DataLog::AppendDouble(int entry, double value, int64_t timestamp) {
if constexpr (std::endian::native == std::endian::little) {
std::memcpy(buf, &value, 8);
} else {
wpi::support::endian::write64le(buf, wpi::bit_cast<uint64_t>(value));
wpi::support::endian::write64le(buf, std::bit_cast<uint64_t>(value));
}
}

Expand Down Expand Up @@ -508,14 +508,14 @@ void DataLog::AppendFloatArray(int entry, std::span<const float> arr,
while ((arr.size() * 4) > kBlockSize) {
buf = Reserve(kBlockSize);
for (auto val : arr.subspan(0, kBlockSize / 4)) {
wpi::support::endian::write32le(buf, wpi::bit_cast<uint32_t>(val));
wpi::support::endian::write32le(buf, std::bit_cast<uint32_t>(val));
buf += 4;
}
arr = arr.subspan(kBlockSize / 4);
}
buf = Reserve(arr.size() * 4);
for (auto val : arr) {
wpi::support::endian::write32le(buf, wpi::bit_cast<uint32_t>(val));
wpi::support::endian::write32le(buf, std::bit_cast<uint32_t>(val));
buf += 4;
}
}
Expand All @@ -540,14 +540,14 @@ void DataLog::AppendDoubleArray(int entry, std::span<const double> arr,
while ((arr.size() * 8) > kBlockSize) {
buf = Reserve(kBlockSize);
for (auto val : arr.subspan(0, kBlockSize / 8)) {
wpi::support::endian::write64le(buf, wpi::bit_cast<uint64_t>(val));
wpi::support::endian::write64le(buf, std::bit_cast<uint64_t>(val));
buf += 8;
}
arr = arr.subspan(kBlockSize / 8);
}
buf = Reserve(arr.size() * 8);
for (auto val : arr) {
wpi::support::endian::write64le(buf, wpi::bit_cast<uint64_t>(val));
wpi::support::endian::write64le(buf, std::bit_cast<uint64_t>(val));
buf += 8;
}
}
Expand Down
10 changes: 5 additions & 5 deletions wpiutil/src/main/native/cpp/DataLogReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

#include "wpi/DataLogReader.h"

#include <bit>
#include <utility>

#include "wpi/DataLog.h"
#include "wpi/Endian.h"
#include "wpi/MathExtras.h"

using namespace wpi::log;

Expand Down Expand Up @@ -97,15 +97,15 @@ bool DataLogRecord::GetFloat(float* value) const {
if (m_data.size() != 4) {
return false;
}
*value = wpi::bit_cast<float>(wpi::support::endian::read32le(m_data.data()));
*value = std::bit_cast<float>(wpi::support::endian::read32le(m_data.data()));
return true;
}

bool DataLogRecord::GetDouble(double* value) const {
if (m_data.size() != 8) {
return false;
}
*value = wpi::bit_cast<double>(wpi::support::endian::read64le(m_data.data()));
*value = std::bit_cast<double>(wpi::support::endian::read64le(m_data.data()));
return true;
}

Expand Down Expand Up @@ -143,7 +143,7 @@ bool DataLogRecord::GetFloatArray(std::vector<float>* arr) const {
arr->reserve(m_data.size() / 4);
for (size_t pos = 0; pos < m_data.size(); pos += 4) {
arr->push_back(
wpi::bit_cast<float>(wpi::support::endian::read32le(&m_data[pos])));
std::bit_cast<float>(wpi::support::endian::read32le(&m_data[pos])));
}
return true;
}
Expand All @@ -156,7 +156,7 @@ bool DataLogRecord::GetDoubleArray(std::vector<double>* arr) const {
arr->reserve(m_data.size() / 8);
for (size_t pos = 0; pos < m_data.size(); pos += 8) {
arr->push_back(
wpi::bit_cast<double>(wpi::support::endian::read64le(&m_data[pos])));
std::bit_cast<double>(wpi::support::endian::read64le(&m_data[pos])));
}
return true;
}
Expand Down

0 comments on commit 0c99073

Please sign in to comment.