Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Nov 10, 2024
1 parent b3ac1c8 commit 350b605
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 38 deletions.
10 changes: 6 additions & 4 deletions include/bela/__strings/int128_have_intrinsic.inc
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,29 @@ inline int128::operator float() const {
// complement overwhelms the precision of the mantissa.
//
// Also check to make sure we don't negate Int128Min()
constexpr float pow_2_64 = 18446744073709551616.0f;
return v_ < 0 && *this != Int128Min()
? -static_cast<float>(-*this)
: static_cast<float>(Int128Low64(*this)) +
std::ldexp(static_cast<float>(Int128High64(*this)), 64);
static_cast<float>(Int128High64(*this)) * pow_2_64;
}

inline int128::operator double() const {
// See comment in int128::operator float() above.
constexpr double pow_2_64 = 18446744073709551616.0;
return v_ < 0 && *this != Int128Min()
? -static_cast<double>(-*this)
: static_cast<double>(Int128Low64(*this)) +
std::ldexp(static_cast<double>(Int128High64(*this)), 64);
static_cast<double>(Int128High64(*this)) * pow_2_64;
}

inline int128::operator long double() const {
// See comment in int128::operator float() above.
constexpr long double pow_2_64 = 18446744073709551616.0L;
return v_ < 0 && *this != Int128Min()
? -static_cast<long double>(-*this)
: static_cast<long double>(Int128Low64(*this)) +
std::ldexp(static_cast<long double>(Int128High64(*this)),
64);
static_cast<long double>(Int128High64(*this)) * pow_2_64;
}
#endif // Clang on PowerPC

Expand Down
9 changes: 6 additions & 3 deletions include/bela/__strings/int128_no_intrinsic.inc
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,24 @@ inline int128::operator float() const {
// complement overwhelms the precision of the mantissa.
//
// Also check to make sure we don't negate Int128Min()
constexpr float pow_2_64 = 18446744073709551616.0f;
return hi_ < 0 && *this != Int128Min() ? -static_cast<float>(-*this)
: static_cast<float>(lo_) + std::ldexp(static_cast<float>(hi_), 64);
: static_cast<float>(lo_) + static_cast<float>(hi_) * pow_2_64;;
}

inline int128::operator double() const {
// See comment in int128::operator float() above.
constexpr double pow_2_64 = 18446744073709551616.0;
return hi_ < 0 && *this != Int128Min() ? -static_cast<double>(-*this)
: static_cast<double>(lo_) + std::ldexp(static_cast<double>(hi_), 64);
: static_cast<double>(lo_) + static_cast<double>(hi_) * pow_2_64;;
}

inline int128::operator long double() const {
// See comment in int128::operator float() above.
constexpr long double pow_2_64 = 18446744073709551616.0L;
return hi_ < 0 && *this != Int128Min()
? -static_cast<long double>(-*this)
: static_cast<long double>(lo_) + std::ldexp(static_cast<long double>(hi_), 64);
: static_cast<long double>(lo_) + static_cast<long double>(hi_) * pow_2_64;;
}

// Comparison operators.
Expand Down
20 changes: 16 additions & 4 deletions include/bela/int128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,11 @@ class int128 {

// Support for bela::Hash.
template <typename H> friend H AbslHashValue(H h, int128 v) {
return H::combine(std::move(h), Int128High64(v), Int128Low64(v));
#if defined(BELA_HAVE_INTRINSIC_INT128)
return H::combine(std::move(h), static_cast<unsigned __int128>(v));
#else
return H::combine(std::move(h), Uint128High64(v), Uint128Low64(v));
#endif
}

private:
Expand Down Expand Up @@ -720,12 +724,20 @@ constexpr uint128::operator unsigned __int128() const { return (static_cast<unsi

// Conversion operators to floating point types.

inline uint128::operator float() const { return static_cast<float>(lo_) + std::ldexp(static_cast<float>(hi_), 64); }
inline uint128::operator float() const {
// Note: This method might return Inf.
constexpr float pow_2_64 = 18446744073709551616.0f;
return static_cast<float>(lo_) + static_cast<float>(hi_) * pow_2_64;
}

inline uint128::operator double() const { return static_cast<double>(lo_) + std::ldexp(static_cast<double>(hi_), 64); }
inline uint128::operator double() const {
constexpr double pow_2_64 = 18446744073709551616.0;
return static_cast<double>(lo_) + static_cast<double>(hi_) * pow_2_64;
}

inline uint128::operator long double() const {
return static_cast<long double>(lo_) + std::ldexp(static_cast<long double>(hi_), 64);
constexpr long double pow_2_64 = 18446744073709551616.0L;
return static_cast<long double>(lo_) + static_cast<long double>(hi_) * pow_2_64;
}

// Comparison operators.
Expand Down
8 changes: 4 additions & 4 deletions src/bela/ascii.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void RemoveExtraAsciiWhitespace(std::wstring *str) {

auto input_it = stripped.begin();
auto input_end = stripped.end();
auto output_it = &(*str)[0];
auto output_it = str->data();
bool is_ws = false;

for (; input_it < input_end; ++input_it) {
Expand All @@ -189,7 +189,7 @@ void RemoveExtraAsciiWhitespace(std::wstring *str) {
++output_it;
}

str->erase(output_it - &(*str)[0]);
str->erase(output_it - str->data());
}

void RemoveExtraAsciiWhitespace(std::string *str) {
Expand All @@ -202,7 +202,7 @@ void RemoveExtraAsciiWhitespace(std::string *str) {

auto input_it = stripped.begin();
auto input_end = stripped.end();
auto output_it = &(*str)[0];
auto output_it = str->data();
bool is_ws = false;

for (; input_it < input_end; ++input_it) {
Expand All @@ -220,7 +220,7 @@ void RemoveExtraAsciiWhitespace(std::string *str) {
++output_it;
}

str->erase(output_it - &(*str)[0]);
str->erase(output_it - str->data());
}

} // namespace bela
2 changes: 1 addition & 1 deletion src/bela/int128.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ inline void DivModImpl(uint128 dividend, uint128 divisor, uint128 *quotient_ret,
}

template <typename T> uint128 MakeUint128FromFloat(T v) {
static_assert(std::is_floating_point<T>::value);
static_assert(std::is_floating_point_v<T>);

// Rounding behavior is towards zero, same as for built-in types.

Expand Down
2 changes: 1 addition & 1 deletion src/bela/str_split.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace bela {
// AnyOf delimiter will use std::wstring_view::find_first_of().
template <typename FindPolicy>
std::wstring_view GenericFind(std::wstring_view text, std::wstring_view delimiter, size_t pos, FindPolicy find_policy) {
if (delimiter.empty() && text.length() > 0) {
if (delimiter.empty() && !text.empty()) {
// Special case for empty std::string delimiters: always return a
// zero-length std::wstring_view referring to the item at position 1 past
// pos.
Expand Down
2 changes: 1 addition & 1 deletion src/bela/str_split_narrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace bela::narrow {
// AnyOf delimiter will use std::string_view::find_first_of().
template <typename FindPolicy>
std::string_view GenericFind(std::string_view text, std::string_view delimiter, size_t pos, FindPolicy find_policy) {
if (delimiter.empty() && text.length() > 0) {
if (delimiter.empty() && !text.empty()) {
// Special case for empty std::string delimiters: always return a
// zero-length std::string_view referring to the item at position 1 past
// pos.
Expand Down
18 changes: 10 additions & 8 deletions src/belahash/sha256-intel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ static const union {
};

// Initial hash value (see FIPS 180-4 5.3.3)
#define H0 0x6a09e667
#define H1 0xbb67ae85
#define H2 0x3c6ef372
#define H3 0xa54ff53a
#define H4 0x510e527f
#define H5 0x9b05688c
#define H6 0x1f83d9ab
#define H7 0x5be0cd19
enum {
H0 = 0x6a09e667,
H1 = 0xbb67ae85,
H2 = 0x3c6ef372,
H3 = 0xa54ff53a,
H4 = 0x510e527f,
H5 = 0x9b05688c,
H6 = 0x1f83d9ab,
H7 = 0x5be0cd19
};

void SHA256H::Initialize() {
h0145 = _mm_set_epi32(H0, H1, H4, H5);
Expand Down
4 changes: 2 additions & 2 deletions src/belatime/dos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ constexpr int sinceWindowsEpochDays(int Y) {
// Windows Epoch start 1601
bela::Time FromDosDateTime(uint16_t dosDate, uint16_t dosTime) {
auto year = (static_cast<int>(dosDate) >> 9) + 1980;
auto mon = static_cast<int>((dosDate >> 5) & 0xf);
auto mon = ((dosDate >> 5) & 0xf);
auto day = static_cast<int>(dosDate & 0x1f);
auto hour = static_cast<int>(dosTime) >> 11;
auto minute = static_cast<int>((dosTime >> 5) & 0x3f);
auto minute = ((dosTime >> 5) & 0x3f);
auto sec = static_cast<int>(dosTime & 0x1f) << 1;
if (sec < 0 || sec > 59 || minute > 59 || minute < 0 || hour < 0 || hour > 23 || mon < 1 || mon > 12 || year < 1601) {
return bela::UnixEpoch();
Expand Down
2 changes: 1 addition & 1 deletion src/belawin/pe/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ bool File::parseFile(bela::error_code &ec) {
if (!fd.ReadAt(sign, signoff, ec)) {
return false;
}
if (!(sign[0] == 'P' && sign[1] == 'E' && sign[2] == 0 && sign[3] == 0)) {
if (sign[0] != 'P' || sign[1] != 'E' || sign[2] != 0 || sign[3] != 0) {
ec = bela::make_error_code(ErrGeneral, L"Invalid PE COFF file signature of ['", int(sign[0]), L"','",
int(sign[1]), L"','", int(sign[2]), L"','", int(sign[3]), L"']");
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/belawin/pe/version.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ bool VersionLoader::getValue(const wchar_t *name, std::wstring &value) const {
bela::Hex(lc.codePage, bela::kZeroPad4), L"\\", name);
LPVOID valuePtr = nullptr;
uint32_t size;
if (VerQueryValueW(buffer.data(), block.data(), reinterpret_cast<LPVOID *>(&valuePtr), &size) == TRUE &&
if (VerQueryValueW(buffer.data(), block.data(), (&valuePtr), &size) == TRUE &&
valuePtr != nullptr && size > 0) {
value.assign(cleanupString(valuePtr, size - 1));
}
Expand Down
6 changes: 3 additions & 3 deletions src/belawin/realpath.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ inline bool DecodeMountPoint(const REPARSE_DATA_BUFFER *buffer, std::wstring &ta
/* but that's confusing for programs since they wouldn't be able to */
/* actually understand such a path when returned by uv_readlink(). */
/* UNC paths are never valid for junctions so we don't care about them. */
if (!(wlen >= 6 && wstr[0] == L'\\' && wstr[1] == L'?' && wstr[2] == L'?' && wstr[3] == L'\\' &&
((wstr[4] >= L'A' && wstr[4] <= L'Z') || (wstr[4] >= L'a' && wstr[4] <= L'z')) && wstr[5] == L':' &&
(wlen == 6 || wstr[6] == L'\\'))) {
if (wlen < 6 || wstr[0] != L'\\' || wstr[1] != L'?' || wstr[2] != L'?' || wstr[3] != L'\\' ||
((wstr[4] < L'A' || wstr[4] > L'Z') && (wstr[4] < L'a' || wstr[4] > L'z')) || wstr[5] != L':' ||
(wlen != 6 && wstr[6] != L'\\')) {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/hazel/fs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ static bool DecodeMountPoint(const REPARSE_DATA_BUFFER *p, FileReparsePoint &frp
/* but that's confusing for programs since they wouldn't be able to */
/* actually understand such a path when returned by uv_readlink(). */
/* UNC paths are never valid for junctions so we don't care about them. */
if (!(wlen >= 6 && wstr[0] == L'\\' && wstr[1] == L'?' && wstr[2] == L'?' && wstr[3] == L'\\' &&
((wstr[4] >= L'A' && wstr[4] <= L'Z') || (wstr[4] >= L'a' && wstr[4] <= L'z')) && wstr[5] == L':' &&
(wlen == 6 || wstr[6] == L'\\'))) {
if (wlen < 6 || wstr[0] != L'\\' || wstr[1] != L'?' || wstr[2] != L'?' || wstr[3] != L'\\' ||
((wstr[4] < L'A' || wstr[4] > L'Z') && (wstr[4] < L'a' || wstr[4] > L'z')) || wstr[5] != L':' ||
(wlen != 6 && wstr[6] != L'\\')) {
ec = bela::make_error_code(bela::ErrGeneral, L"Unresolved reparse point MountPoint'");
return false;
}
Expand Down
3 changes: 1 addition & 2 deletions src/hazel/ina/text.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
////////////////
#include "hazelinc.hpp"
#define UTF8_ACCEPT 0
#define UTF8_REJECT 1
enum { UTF8_ACCEPT = 0, UTF8_REJECT = 1 };

namespace hazel::internal {
// check text details
Expand Down

0 comments on commit 350b605

Please sign in to comment.