Skip to content

Commit

Permalink
optimize toAscii function with early exit
Browse files Browse the repository at this point in the history
GCC is able to see that the branch is useless as it can be done at
compile time. Produces shorter assembly with both -O2 and -O3.

https://godbolt.org/z/ejhfxh8Tx

Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Jul 13, 2023
1 parent c686bed commit 81576d7
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/jp2image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ Jp2Image::Jp2Image(BasicIo::UniquePtr io, bool create) : Image(ImageType::jp2, m
// Obtains the ascii version from the box.type
std::string Jp2Image::toAscii(uint32_t n) {
const auto p = reinterpret_cast<const char*>(&n);
if (isBigEndianPlatform())
return std::string(p, p + 4);
std::string result(p, p + 4);
if (!isBigEndianPlatform())
std::reverse(result.begin(), result.end());
std::reverse(result.begin(), result.end());
return result;
}

Expand Down

0 comments on commit 81576d7

Please sign in to comment.