Skip to content
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

Template parameters (and locals) are unintuitively affected by endianness #119

Open
BobSmun opened this issue Aug 26, 2024 · 1 comment
Open

Comments

@BobSmun
Copy link
Contributor

BobSmun commented Aug 26, 2024

import std.io;
struct Test<auto t> {
    std::print("{}", t);
    u32 a = t;
};

le Test<5> test1;
be Test<5> test2;

std::print("{} {}",test1,test2);

results in:

I: 5
I: 5
I: bitfield Test { a = 5, t = 5 } bitfield Test { a = 83886080, t = 6646139978924579364519035301401722880 }

Inside the template, the parameter is read the same way (both being 5). This works as expected and allows consistent used for e.g. bitfield lengths.

However, the endianness seems to take effect when reading the template parameters (and locals) from outside the template. This makes it difficult to determine what they were, such as from a format function

@BobSmun BobSmun changed the title Template parameters (and locals) are unituitively affected by endianness Template parameters (and locals) are unintuitively affected by endianness Aug 26, 2024
@BobSmun
Copy link
Contributor Author

BobSmun commented Dec 15, 2024

A work around for this:

If any of the template parameters are guaranteed to always be smaller (or larger) than its endian flipped value, then both endian orderings can be compared to detect which was applied and allow for a way to deal with it. For example, template parameters have a type of auto - a 128bit value. If practical usage limits the value to be at most 64bit, then you would be able to confidently recover that value with this approach

If there are multiple template parameters / locals to fix-up, then only one of them needs to be checked and the result of its comparison can be used to correct the others

If none of the template parameters are guaranteed to always be smaller (or larger), then a default parameter can be added (e.g. via a using statement), using a known value that differs based on endian, to identify the appropriate endian to read the values as

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

No branches or pull requests

1 participant