Make the endian test more C++-like

This commit is contained in:
Chris Robinson
2021-01-21 22:17:40 -08:00
parent 87dbeed853
commit 5729e1004d
6 changed files with 55 additions and 31 deletions
+1 -1
View File
@@ -613,6 +613,7 @@ check_symbol_exists(getopt unistd.h HAVE_GETOPT)
# Common sources used by both the OpenAL implementation library, the OpenAL # Common sources used by both the OpenAL implementation library, the OpenAL
# router, and certain tools and examples. # router, and certain tools and examples.
set(COMMON_OBJS set(COMMON_OBJS
common/albit.h
common/albyte.h common/albyte.h
common/alcomplex.cpp common/alcomplex.cpp
common/alcomplex.h common/alcomplex.h
@@ -628,7 +629,6 @@ set(COMMON_OBJS
common/atomic.h common/atomic.h
common/dynload.cpp common/dynload.cpp
common/dynload.h common/dynload.h
common/endiantest.h
common/intrusive_ptr.h common/intrusive_ptr.h
common/math_defs.h common/math_defs.h
common/opthelpers.h common/opthelpers.h
+15 -11
View File
@@ -32,11 +32,11 @@
#include <thread> #include <thread>
#include <functional> #include <functional>
#include "albit.h"
#include "alcmain.h" #include "alcmain.h"
#include "alu.h" #include "alu.h"
#include "compat.h" #include "compat.h"
#include "core/logging.h" #include "core/logging.h"
#include "endiantest.h"
#include "ringbuffer.h" #include "ringbuffer.h"
#include "threads.h" #include "threads.h"
@@ -56,7 +56,7 @@ namespace {
constexpr char opensl_device[] = "OpenSL"; constexpr char opensl_device[] = "OpenSL";
SLuint32 GetChannelMask(DevFmtChannels chans) constexpr SLuint32 GetChannelMask(DevFmtChannels chans) noexcept
{ {
switch(chans) switch(chans)
{ {
@@ -83,7 +83,7 @@ SLuint32 GetChannelMask(DevFmtChannels chans)
} }
#ifdef SL_ANDROID_DATAFORMAT_PCM_EX #ifdef SL_ANDROID_DATAFORMAT_PCM_EX
SLuint32 GetTypeRepresentation(DevFmtType type) constexpr SLuint32 GetTypeRepresentation(DevFmtType type) noexcept
{ {
switch(type) switch(type)
{ {
@@ -102,7 +102,14 @@ SLuint32 GetTypeRepresentation(DevFmtType type)
} }
#endif #endif
const char *res_str(SLresult result) constexpr SLuint32 GetByteOrderEndianness() noexcept
{
if /*constexpr*/(al::endian::native == al::endian::little)
return SL_BYTEORDER_LITTLEENDIAN;
return SL_BYTEORDER_BIGENDIAN;
}
const char *res_str(SLresult result) noexcept
{ {
switch(result) switch(result)
{ {
@@ -459,7 +466,7 @@ bool OpenSLPlayback::reset()
format_pcm_ex.bitsPerSample = mDevice->bytesFromFmt() * 8; format_pcm_ex.bitsPerSample = mDevice->bytesFromFmt() * 8;
format_pcm_ex.containerSize = format_pcm_ex.bitsPerSample; format_pcm_ex.containerSize = format_pcm_ex.bitsPerSample;
format_pcm_ex.channelMask = GetChannelMask(mDevice->FmtChans); format_pcm_ex.channelMask = GetChannelMask(mDevice->FmtChans);
format_pcm_ex.endianness = IS_LITTLE_ENDIAN ? SL_BYTEORDER_LITTLEENDIAN : SL_BYTEORDER_BIGENDIAN; format_pcm_ex.endianness = GetByteOrderEndianness();
format_pcm_ex.representation = GetTypeRepresentation(mDevice->FmtType); format_pcm_ex.representation = GetTypeRepresentation(mDevice->FmtType);
audioSrc.pLocator = &loc_bufq; audioSrc.pLocator = &loc_bufq;
@@ -490,8 +497,7 @@ bool OpenSLPlayback::reset()
format_pcm.bitsPerSample = mDevice->bytesFromFmt() * 8; format_pcm.bitsPerSample = mDevice->bytesFromFmt() * 8;
format_pcm.containerSize = format_pcm.bitsPerSample; format_pcm.containerSize = format_pcm.bitsPerSample;
format_pcm.channelMask = GetChannelMask(mDevice->FmtChans); format_pcm.channelMask = GetChannelMask(mDevice->FmtChans);
format_pcm.endianness = IS_LITTLE_ENDIAN ? SL_BYTEORDER_LITTLEENDIAN : format_pcm.endianness = GetByteOrderEndianness();
SL_BYTEORDER_BIGENDIAN;
audioSrc.pLocator = &loc_bufq; audioSrc.pLocator = &loc_bufq;
audioSrc.pFormat = &format_pcm; audioSrc.pFormat = &format_pcm;
@@ -733,8 +739,7 @@ void OpenSLCapture::open(const char* name)
format_pcm_ex.bitsPerSample = mDevice->bytesFromFmt() * 8; format_pcm_ex.bitsPerSample = mDevice->bytesFromFmt() * 8;
format_pcm_ex.containerSize = format_pcm_ex.bitsPerSample; format_pcm_ex.containerSize = format_pcm_ex.bitsPerSample;
format_pcm_ex.channelMask = GetChannelMask(mDevice->FmtChans); format_pcm_ex.channelMask = GetChannelMask(mDevice->FmtChans);
format_pcm_ex.endianness = IS_LITTLE_ENDIAN ? SL_BYTEORDER_LITTLEENDIAN : format_pcm_ex.endianness = GetByteOrderEndianness();
SL_BYTEORDER_BIGENDIAN;
format_pcm_ex.representation = GetTypeRepresentation(mDevice->FmtType); format_pcm_ex.representation = GetTypeRepresentation(mDevice->FmtType);
audioSnk.pLocator = &loc_bq; audioSnk.pLocator = &loc_bq;
@@ -757,8 +762,7 @@ void OpenSLCapture::open(const char* name)
format_pcm.bitsPerSample = mDevice->bytesFromFmt() * 8; format_pcm.bitsPerSample = mDevice->bytesFromFmt() * 8;
format_pcm.containerSize = format_pcm.bitsPerSample; format_pcm.containerSize = format_pcm.bitsPerSample;
format_pcm.channelMask = GetChannelMask(mDevice->FmtChans); format_pcm.channelMask = GetChannelMask(mDevice->FmtChans);
format_pcm.endianness = IS_LITTLE_ENDIAN ? SL_BYTEORDER_LITTLEENDIAN : format_pcm.endianness = GetByteOrderEndianness();
SL_BYTEORDER_BIGENDIAN;
audioSnk.pLocator = &loc_bq; audioSnk.pLocator = &loc_bq;
audioSnk.pFormat = &format_pcm; audioSnk.pFormat = &format_pcm;
+2 -2
View File
@@ -33,6 +33,7 @@
#include <functional> #include <functional>
#include <thread> #include <thread>
#include "albit.h"
#include "albyte.h" #include "albyte.h"
#include "alcmain.h" #include "alcmain.h"
#include "alconfig.h" #include "alconfig.h"
@@ -41,7 +42,6 @@
#include "alu.h" #include "alu.h"
#include "compat.h" #include "compat.h"
#include "core/logging.h" #include "core/logging.h"
#include "endiantest.h"
#include "strutils.h" #include "strutils.h"
#include "threads.h" #include "threads.h"
#include "vector.h" #include "vector.h"
@@ -149,7 +149,7 @@ int WaveBackend::mixerProc()
mDevice->renderSamples(mBuffer.data(), mDevice->UpdateSize, frameStep); mDevice->renderSamples(mBuffer.data(), mDevice->UpdateSize, frameStep);
done += mDevice->UpdateSize; done += mDevice->UpdateSize;
if /*constexpr*/(!IS_LITTLE_ENDIAN) if /*constexpr*/(al::endian::native != al::endian::little)
{ {
const uint bytesize{mDevice->bytesFromFmt()}; const uint bytesize{mDevice->bytesFromFmt()};
+2 -2
View File
@@ -40,6 +40,7 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include "albit.h"
#include "alcmain.h" #include "alcmain.h"
#include "alconfig.h" #include "alconfig.h"
#include "alfstream.h" #include "alfstream.h"
@@ -49,7 +50,6 @@
#include "alspan.h" #include "alspan.h"
#include "core/filters/splitter.h" #include "core/filters/splitter.h"
#include "core/logging.h" #include "core/logging.h"
#include "endiantest.h"
#include "math_defs.h" #include "math_defs.h"
#include "opthelpers.h" #include "opthelpers.h"
#include "polyphase_resampler.h" #include "polyphase_resampler.h"
@@ -471,7 +471,7 @@ inline T readle(std::istream &data)
static_assert(num_bits <= sizeof(T)*8, "num_bits is too large for the type"); static_assert(num_bits <= sizeof(T)*8, "num_bits is too large for the type");
T ret{}; T ret{};
if(IS_LITTLE_ENDIAN) if /*constexpr*/(al::endian::native == al::endian::little)
{ {
if(!data.read(reinterpret_cast<char*>(&ret), num_bits/8)) if(!data.read(reinterpret_cast<char*>(&ret), num_bits/8))
return static_cast<T>(EOF); return static_cast<T>(EOF);
+35
View File
@@ -0,0 +1,35 @@
#ifndef AL_BIT_H
#define AL_BIT_H
namespace al {
#ifdef __BYTE_ORDER__
enum class endian {
little = __ORDER_LITTLE_ENDIAN__,
big = __ORDER_BIG_ENDIAN__,
native = __BYTE_ORDER__
};
#else
/* This doesn't support mixed-endian. */
namespace detail_ {
constexpr inline bool EndianTest() noexcept
{
static_assert(sizeof(char) < sizeof(int), "char is too big");
constexpr int test_val{1};
return static_cast<const char&>(test_val);
}
} // namespace detail_
enum class endian {
little = 0,
big = 1,
native = detail_::EndianTest() ? little : big
};
#endif
} // namespace al
#endif /* AL_BIT_H */
-15
View File
@@ -1,15 +0,0 @@
#ifndef AL_ENDIANTEST_H
#define AL_ENDIANTEST_H
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
#define IS_LITTLE_ENDIAN (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
#else
constexpr inline bool EndianTest() noexcept
{
constexpr int test_val{1};
return static_cast<const char&>(test_val);
}
#define IS_LITTLE_ENDIAN (EndianTest())
#endif
#endif /* AL_ENDIANTEST_H */