|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [Xen-devel] [PATCH v11 3/9] xen: introduce DECLARE_BOUNDS
>>> On 06.03.19 at 21:55, <sstabellini@xxxxxxxxxx> wrote:
> On Wed, 6 Mar 2019, Jan Beulich wrote:
>> > +static inline ptrdiff_t name ## _bytediff(const type s1[],
>> > \
>> > + const struct abstract_ ## name
>> > s2[])\
>> > +{
>> > \
>> > + BUILD_BUG_ON(alignof(*s1) != alignof(*s2));
>> > \
>> > + return (ptrdiff_t)((uintptr_t)s2 - (uintptr_t)s1);
>> > \
>> > +}
>>
>> What's the value of the intermediate casting to uintptr_t? Why not
>> cast to ptrdiff_t right away?
>
> uintptr_t is the integer type corresponding to a pointer, so we should
> use uintptr_t first. ptrdiff_t is the difference type so we should cast
> to it afterwards. Specifically, uintptr_t is unsigned and ptrdiff_t is
> signed. So I don't think it would be correct to do:
>
> return (ptrdiff_t)((ptrdiff_t)s2 - (ptrdiff_t)s1);
>
> Or am I missing your point?
Well, I really mean
return (ptrdiff_t)s2 - (ptrdiff_t)s1;
But that aside - let's consider all three cases:
1) sizeof(ptrdiff_t) == sizeof(void *)
All fine. And you'll have some difficulty finding a platform where this
isn't the case.
2) sizeof(ptrdiff_t) > sizeof(void *)
Still all fine as long as the conversion void * -> ptrdiff_t doesn't differ
from the two step void * -> uintptr_t -> ptrdiff_t one. In fact in this
case yours would necessarily be wrong: You'd need to use intptr_t
instead, or else would-be-negative values wouldn't end up negative,
because the uintptr_t -> ptrdiff_t conversion is a zero extension then.
Whether my variant would suffer the same issue would depend on
whether void * -> ptrdiff_t is sign- or zero-extending.
Now if you went the intptr_t route, I'd be fine too, because then
again the outer cast can be dropped, which is all I'm after.
3) sizeof(ptrdiff_t) < sizeof(void *)
In this case neither variant will work for all possible pointer inputs.
Jan
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |