Thread overview | |||||
---|---|---|---|---|---|
|
September 05, 2011 integer cast in object_.d | ||||
---|---|---|---|---|
| ||||
I'm looking at compare() in class TypeInfo_Array and it's defined as: override int compare(in void* p1, in void* p2) { void[] a1 = *cast(void[]*)p1; void[] a2 = *cast(void[]*)p2; size_t sz = value.tsize(); size_t len = a1.length; if (a2.length < len) len = a2.length; for (size_t u = 0; u < len; u++) { int result = value.compare(a1.ptr + u * sz, a2.ptr + u * sz); if (result) return result; } return cast(int)a1.length - cast(int)a2.length; } Shouldn't that return line be: return cast(int)(a1.length - a2.length); To make it 64-bit safe? |
September 05, 2011 Re: integer cast in object_.d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 09/05/2011 04:57 AM, Andrej Mitrovic wrote:
> I'm looking at compare() in class TypeInfo_Array and it's defined as:
>
> override int compare(in void* p1, in void* p2)
> {
> void[] a1 = *cast(void[]*)p1;
> void[] a2 = *cast(void[]*)p2;
> size_t sz = value.tsize();
> size_t len = a1.length;
>
> if (a2.length< len)
> len = a2.length;
> for (size_t u = 0; u< len; u++)
> {
> int result = value.compare(a1.ptr + u * sz, a2.ptr + u * sz);
> if (result)
> return result;
> }
> return cast(int)a1.length - cast(int)a2.length;
> }
>
> Shouldn't that return line be:
> return cast(int)(a1.length - a2.length);
>
> To make it 64-bit safe?
Both are not 64-bit safe.
|
September 05, 2011 Re: integer cast in object_.d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 2011-09-05 02:57:33 +0000, Andrej Mitrovic <andrej.mitrovich@gmail.com> said: > I'm looking at compare() in class TypeInfo_Array and it's defined as: > > override int compare(in void* p1, in void* p2) > { > void[] a1 = *cast(void[]*)p1; > void[] a2 = *cast(void[]*)p2; > size_t sz = value.tsize(); > size_t len = a1.length; > > if (a2.length < len) > len = a2.length; > for (size_t u = 0; u < len; u++) > { > int result = value.compare(a1.ptr + u * sz, a2.ptr + u * sz); > if (result) > return result; > } > return cast(int)a1.length - cast(int)a2.length; > } > > Shouldn't that return line be: > return cast(int)(a1.length - a2.length); > > To make it 64-bit safe? Per the rules of modular arithmetic substraction, both will give you the same result actually. And no it isn't 64-bit safe. -- Michel Fortin michel.fortin@michelf.com http://michelf.com/ |
Copyright © 1999-2021 by the D Language Foundation