Skip to content

Commit 28ac8ec

Browse files
authored
util/chdcodec: use std::endian for native byte order
Use std::endian instead of manually detecting native byte order
1 parent 7c91963 commit 28ac8ec

1 file changed

Lines changed: 5 additions & 12 deletions

File tree

src/lib/util/chdcodec.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <zlib.h>
2222
#include <zstd.h>
2323

24+
#include <bit>
2425
#include <cstring>
2526
#include <new>
2627

@@ -1465,9 +1466,7 @@ chd_flac_compressor::chd_flac_compressor(chd_file &chd, uint32_t hunkbytes, bool
14651466
: chd_compressor(chd, hunkbytes, lossy)
14661467
{
14671468
// determine whether we want native or swapped samples
1468-
uint16_t native_endian = 0;
1469-
*reinterpret_cast<uint8_t *>(&native_endian) = 1;
1470-
m_big_endian = (native_endian == 0x100);
1469+
m_big_endian = (std::endian::native == std::endian::big);
14711470

14721471
// configure the encoder
14731472
m_encoder.set_sample_rate(44100);
@@ -1542,9 +1541,7 @@ chd_flac_decompressor::chd_flac_decompressor(chd_file &chd, uint32_t hunkbytes,
15421541
: chd_decompressor(chd, hunkbytes, lossy)
15431542
{
15441543
// determine whether we want native or swapped samples
1545-
uint16_t native_endian = 0;
1546-
*reinterpret_cast<uint8_t *>(&native_endian) = 1;
1547-
m_big_endian = (native_endian == 0x100);
1544+
m_big_endian = (std::endian::native == std::endian::big);
15481545
}
15491546

15501547

@@ -1593,9 +1590,7 @@ chd_cd_flac_compressor::chd_cd_flac_compressor(chd_file &chd, uint32_t hunkbytes
15931590
throw std::error_condition(chd_file::error::CODEC_ERROR);
15941591

15951592
// determine whether we want native or swapped samples
1596-
uint16_t native_endian = 0;
1597-
*reinterpret_cast<uint8_t *>(&native_endian) = 1;
1598-
m_swap_endian = (native_endian == 1);
1593+
m_swap_endian = (std::endian::native == std::endian::little);
15991594

16001595
// configure the encoder
16011596
m_encoder.set_sample_rate(44100);
@@ -1722,9 +1717,7 @@ chd_cd_flac_decompressor::chd_cd_flac_decompressor(chd_file &chd, uint32_t hunkb
17221717
throw std::error_condition(chd_file::error::CODEC_ERROR);
17231718

17241719
// determine whether we want native or swapped samples
1725-
uint16_t native_endian = 0;
1726-
*reinterpret_cast<uint8_t *>(&native_endian) = 1;
1727-
m_swap_endian = (native_endian == 1);
1720+
m_swap_endian = (std::endian::native == std::endian::little);
17281721

17291722
// init the inflater
17301723
m_inflater.next_in = (Bytef *)this; // bogus, but that's ok

0 commit comments

Comments
 (0)