Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 11, 2017 Error on negating unsigned types | ||||
---|---|---|---|---|
| ||||
The Weka folks would like to see a compile error on negating unsigned types: ``` uint total = 0; void add(int x) { total += x; } ubyte popCount() { return 5; } add(popCount()); add(-popCount()); writeln(total); // <-- what does this print? (behavior is different from C) ``` After adding the error, a few places in Phobos error out. For example: ``` (std.format.formatIntegral) T arg = val; immutable negative = (base == 10 && arg < 0); if (negative) { arg = -arg; } ``` ``` (std.conv.toTextRange) T value; bool negative = value < 0; Unqual!(Unsigned!T) v = negative ? -value : value; ``` So, adding the error may be nice, but it would make generic code a little more verbose. Ideas? People OK with that? cheers, Johan |
July 11, 2017 Re: Error on negating unsigned types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen | On Tuesday, 11 July 2017 at 19:46:00 UTC, Johan Engelen wrote:
> The Weka folks would like to see a compile error on negating unsigned types:
Also this nice hackery would need a workaround:
```
if ((y&(-y))==y)
```
|
July 11, 2017 Re: Error on negating unsigned types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen | On Tuesday, 11 July 2017 at 19:57:06 UTC, Johan Engelen wrote: > On Tuesday, 11 July 2017 at 19:46:00 UTC, Johan Engelen wrote: >> The Weka folks would like to see a compile error on negating unsigned types: > > Also this nice hackery would need a workaround: > ``` > if ((y&(-y))==y) > ``` http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/ "The two’s complement of x is computed with ~x + 1, which inverts the bits of x and adds 1 (~x + 1 is equivalent to -x, but negation is technically illegal for an unsigned integer)." |
July 11, 2017 Re: Error on negating unsigned types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen | On Tuesday, 11 July 2017 at 20:02:07 UTC, Johan Engelen wrote: > On Tuesday, 11 July 2017 at 19:57:06 UTC, Johan Engelen wrote: >> On Tuesday, 11 July 2017 at 19:46:00 UTC, Johan Engelen wrote: >>> The Weka folks would like to see a compile error on negating unsigned types: >> >> Also this nice hackery would need a workaround: >> ``` >> if ((y&(-y))==y) >> ``` > > http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/ > > "The two’s complement of x is computed with ~x + 1, which inverts the bits of x and adds 1 (~x + 1 is equivalent to -x, but negation is technically illegal for an unsigned integer)." Codegen is the same for both: https://godbolt.org/g/JiHiEe So Phobos's `if ((y&(-y))==y)` can be rewritten to `if ((y&(~y+1))==y)` without consequence. |
July 11, 2017 Re: Error on negating unsigned types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen Attachments:
| I belive this is a bug or it should be fixed and do same as C does.
import std.stdio;
void main()
{
uint total = 0;
byte popCount() { return 5; }
writeln(total + popCount() - popCount());
}
works as expected
On Tue, Jul 11, 2017 at 9:57 PM, Johan Engelen via Digitalmars-d < digitalmars-d@puremagic.com> wrote:
> On Tuesday, 11 July 2017 at 19:46:00 UTC, Johan Engelen wrote:
>
>> The Weka folks would like to see a compile error on negating unsigned types:
>>
>
> Also this nice hackery would need a workaround:
> ```
> if ((y&(-y))==y)
> ```
>
>
>
|
July 11, 2017 Re: Error on negating unsigned types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen | On Tuesday, 11 July 2017 at 19:46:00 UTC, Johan Engelen wrote: > The Weka folks would like to see a compile error on negating unsigned types: The error would be great! I've filed a bug yesterday: https://issues.dlang.org/show_bug.cgi?id=17633 In it I asked to change the result type, but an error works just as well. Unsigned negation bit us with costly bugs multiple times so far. |
July 11, 2017 Re: Error on negating unsigned types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen | On Tuesday, 11 July 2017 at 20:05:43 UTC, Johan Engelen wrote:
> On Tuesday, 11 July 2017 at 20:02:07 UTC, Johan Engelen wrote:
>> On Tuesday, 11 July 2017 at 19:57:06 UTC, Johan Engelen wrote:
>>> On Tuesday, 11 July 2017 at 19:46:00 UTC, Johan Engelen wrote:
>>>> [...]
>>>
>>> Also this nice hackery would need a workaround:
>>> ```
>>> if ((y&(-y))==y)
>>> ```
>>
>> http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/
>>
>> "The two’s complement of x is computed with ~x + 1, which inverts the bits of x and adds 1 (~x + 1 is equivalent to -x, but negation is technically illegal for an unsigned integer)."
>
> Codegen is the same for both: https://godbolt.org/g/JiHiEe
> So Phobos's `if ((y&(-y))==y)` can be rewritten to `if ((y&(~y+1))==y)` without consequence.
Interestingly returning a bool saves one register and a XOR.
|
July 11, 2017 Re: Error on negating unsigned types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen | On 7/11/2017 12:46 PM, Johan Engelen wrote: > uint total = 0; > void add(int x) { total += x; } > ubyte popCount() { return 5; } > > add(popCount()); > add(-popCount()); > writeln(total); // <-- what does this print? (behavior is different from C) The behavior should be the same. https://issues.dlang.org/show_bug.cgi?id=17637 |
July 12, 2017 Re: Error on negating unsigned types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote:
> On 7/11/2017 12:46 PM, Johan Engelen wrote:
>> uint total = 0;
>> void add(int x) { total += x; }
>> ubyte popCount() { return 5; }
>> add(popCount());
>> add(-popCount());
>> writeln(total); // <-- what does this print? (behavior is different from C)
>
> The behavior should be the same.
>
> https://issues.dlang.org/show_bug.cgi?id=17637
so, you want to change promotion rules? just to clarify the things, how *exactly* you want new rules to work, and what will happen with `byte a; a = -a;`, and why?
|
July 12, 2017 Re: Error on negating unsigned types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen | On 07/11/2017 03:46 PM, Johan Engelen wrote:
> So, adding the error may be nice, but it would make generic code a little more verbose.
> Ideas? People OK with that?
A compelling argument is that the rule exposed two bugs in Phobos. -- Andrei
|
Copyright © 1999-2021 by the D Language Foundation