Skip to content

various byteSwap and bit_cast cleanups #3227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

various byteSwap and bit_cast cleanups #3227

wants to merge 5 commits into from

Conversation

neheb
Copy link
Collaborator

@neheb neheb commented Apr 3, 2025

No description provided.

Comment on lines 90 to 92
std::array<char, sizeof(uint32_t)> p;
std::memcpy(p.data(), &n, sizeof(uint32_t));
std::string result(p.begin(), p.end());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function might be a cleaner as a loop that mask the bottom byte and then does n >>= 8 on each iteration. That way, there's no need to think about endianness.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried this.

godbolt output ballooned from 39 lines to 287. The code looks incredibly inefficient with operator new and memcpy being called.

Copy link
Collaborator Author

@neheb neheb Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second attempt went from 39 to 49. Maybe that's good enough.

edit: now 31.

src/image.cpp Outdated
Comment on lines 230 to 231
uint16_t v;
std::memcpy(&v, buf.c_data(offset), sizeof(uint16_t));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The read_uint8 method checks for out-of-bound accesses, so it isn't a good idea to replace it with a memcpy.

It would be better to use methods like read_uint16 and read_uint32 which have a ByteOrder parameter. But this function has a bSwap parameter which doesn't mean the same thing, so a bit of refactoring would be needed.

Turns out that even though MSVC is supposed to support std::byteswap, I
can't seem to get it to compile with Godbolt. Whats more, the fallback
path MSVC cannot optimize.

Signed-off-by: Rosen Penev <[email protected]>
@neheb
Copy link
Collaborator Author

neheb commented Apr 19, 2025

Before I go on I should really add a big endian CI here.

neheb added 4 commits April 18, 2025 17:30
Avoids having to deal with endianness.

Signed-off-by: Rosen Penev <[email protected]>
We can just treat the data as big endian with bit shifting and ORing.

Signed-off-by: Rosen Penev <[email protected]>
It's the same function.

Signed-off-by: Rosen Penev <[email protected]>
Signed-off-by: Rosen Penev <[email protected]>
@neheb
Copy link
Collaborator Author

neheb commented Apr 19, 2025

removed bad commits. This was tested on the big endian CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants