Conversation
|
These are very short functions but getting them right thanks to value range propagation is a pain. Now you just have to call a function. |
tab -> whitespace
0f9d6e5 to
9fcecd9
Compare
Current coverage is 88.73% (diff: 100%)
|
|
So what is it that makes just writing out the operations "a pain"? I'm not convinced that extra functions on this level are needed (low-level bit twiddling for those who know what they are doing, and logical higher-level bit fields, ... for a more user-friendly API). |
|
@klickverbot it inserts the right casts at the right spots so you don't have to remember that byte become ints when you bitwise (or|and) them. of cause some people know, but calling one of those functions is way easier. |
|
Maybe Bit Setting Functions is enough? |
| assert(testBit(a, 0)); | ||
| assert(!testBit(a, 1)); | ||
| assert(testBit(a, 5)); | ||
| assert(!testBit(a, 6)); |
There was a problem hiding this comment.
Needs tests for more types.
|
This PR looks like overengineering, imho |
|
OK, so what about just keeping bool testBit(T)(const T bitfield, const ulong idx) if(isIntegral!T);
T setBit(T)(const T bitfield, const ulong idx, bool value) if(isIntegral!T);
T flipBit(T)(const T bitfield, const ulong idx) if(isIntegral!T);and adding an %B to format so it pretty prints like the bits as my prettyPrint function does? |
|
Yes, this looks good to me |
|
Why |
|
one less cast |
|
Why |
|
I do not understand the question? |
|
I mean that this will be bad code for 32 bit DMD: ulong value = cast(ulong) bitfield;
ulong mask = 1UL << idx;
value |= mask;
return cast(T) value; |
|
If that is bad code (I assume by bad code you, will have a bug, not bad codegen), than the autotester is broken. Because I test that case and the autotester does not complain. |
|
Sorry for my explanation. I mean bad code optimisation |
|
what do you mean by bad code optimization? |
32 bit CPU has not 64 bit integer registers. So using |
|
I get it now. I'll work on making the cast to uint if possible |
|
@burner btw what about core.bitop?
|
|
they work only on size_t* |
| enum upTo = T.sizeof * 8UL; | ||
| } | ||
|
|
||
| private template minUnsignedIntegral(T) |
There was a problem hiding this comment.
What's wrong with just using Unsigned!T?
|
This kind of stuff should not be in Phobos. |
|
@andralex For a module called I think that closing this was a bit premature. |
|
I'll reopen for further discussion, but one-liners such as |
|
I'm sorry this has been a fair amount of work for you guys, in the light of what I'm about to say. But it falls into something I've argued against for years - a mass of trivial one-liners that take more time to read the documentation than just using & | and ^ operators. Avoiding learning about the trivial integral promotion rules is not a justification for this - and this is much, much more to learn than that - and the programmer will still have to learn about integral promotion. And if & | ^ are too hard for a user and must be put in a library, are + - * / next? 441 lines of source text to support 8 one liners and 2 trivial functions is an indication as well that something has gone awry. This needs a much more compelling rationale, but I agree with Andrei that it's hard to see much promise in them. I apologize for not noticing this sooner, I wish I could have saved you the effort. |
FWIW I will try to give a rationale:
cast(T)(cast(ulong)(bitfield) ^ (-cast(ulong)(value) ^ cast(ulong)(bitfield)) & (1UL << 8));... instead of:
Note: in previous discussions this PR has been boiled down to |
This example has a lot of pointless casts. I agree with @WalterBright |
|
The corollary here is that the set of people who simultaneously need to set bits in integrals and don't know how to is near empty. |
|
As mentioned much earlier, I agree with Walter's and Andrei's position – I should have probably phrased my comment less politely. Higher-level wrappers like
|
Instead, I should have phrased my comment more politely. Thanks for being civil, my way of putting things is not called for. |
@WalterBright I disagree, algebra and integer promotion makes sense, bit fiddling and integer promotion does not. That is why I created these functions. I properly got carried away with some functions, but setting and testing individual bits via functions makes room in my harddrive to focus on more elaborate tasks. |
Only one cast to ulong is needed, the rest will automatically happen. Library functions are not a solution to not understanding integral promotions rules - they are a fundamental, basic feature of the language, not some weird corner case. core.bitop exists for two reasons:
|
Actually, they are! For normal math you don't need to know integer promotion rules, they just work 99.999% of the time. And that is why they are good. For bit fiddling you need to know, or you use these functions. |
|
Sadly I don't see anything salvageable in here. It would be nice to migrate the bits printer to |
Bit Testing Functions
Bit Setting Functions
Bit Pretty Printing
github: https://github.com/burner/BitFiddle
dub: http://code.dlang.org/packages/bitfiddle