All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jan Beulich" <JBeulich@suse.com>
To: Stefano Stabellini <sstabellini@kernel.org>
Cc: Stefano Stabellini <stefanos@xilinx.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	george.dunlap@citrix.com, Julien Grall <julien.grall@arm.com>,
	ian.jackson@citrix.com,
	xen-devel <xen-devel@lists.xenproject.org>
Subject: Re: [PATCH v11 3/9] xen: introduce DECLARE_BOUNDS
Date: Thu, 07 Mar 2019 01:39:27 -0700	[thread overview]
Message-ID: <5C80D8BF020000780021C55C@prv1-mh.provo.novell.com> (raw)
In-Reply-To: <alpine.DEB.2.10.1903061252080.9200@sstabellini-ThinkPad-X260>

>>> On 06.03.19 at 21:55, <sstabellini@kernel.org> 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@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  reply	other threads:[~2019-03-07  8:39 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-05 22:38 [PATCH v11 0/9] misc safety certification fixes Stefano Stabellini
2019-03-05 22:38 ` [PATCH v11 1/9] xen: use __UINTPTR_TYPE__ for uintptr_t Stefano Stabellini
2019-03-06  7:47   ` Jan Beulich
2019-03-06 21:16     ` Stefano Stabellini
2019-03-07  8:29       ` Jan Beulich
2019-03-07 15:43         ` Ian Jackson
2019-03-08  7:23           ` Jan Beulich
2019-03-08 14:18   ` Andrew Cooper
2019-03-08 15:19     ` Ian Jackson
2019-03-05 22:38 ` [PATCH v11 2/9] xen: introduce ptrdiff_t Stefano Stabellini
2019-03-05 22:38 ` [PATCH v11 3/9] xen: introduce DECLARE_BOUNDS Stefano Stabellini
2019-03-06 15:24   ` Jan Beulich
2019-03-06 20:55     ` Stefano Stabellini
2019-03-07  8:39       ` Jan Beulich [this message]
2019-03-07 14:16         ` Ian Jackson
2019-03-08  8:15           ` Jan Beulich
2019-03-08 15:31             ` Ian Jackson
2019-03-05 22:38 ` [PATCH v11 4/9] xen/arm: use DECLARE_BOUNDS as required Stefano Stabellini
2019-03-05 22:38 ` [PATCH v11 5/9] xen/x86: " Stefano Stabellini
2019-03-06 15:33   ` Jan Beulich
2019-03-06 21:05     ` Stefano Stabellini
2019-03-07  8:40       ` Jan Beulich
2019-03-07 14:02       ` Ian Jackson
2019-03-07 14:42         ` George Dunlap
2019-03-08  8:46         ` Jan Beulich
2019-03-08  8:55           ` Juergen Gross
2019-03-08 15:33           ` Ian Jackson
2019-03-06 15:48   ` Jan Beulich
2019-03-06 21:38     ` Stefano Stabellini
2019-03-07  8:50       ` Jan Beulich
2019-03-07 14:43         ` Ian Jackson
2019-03-08  8:22           ` Jan Beulich
2019-03-08 15:36             ` Ian Jackson
2019-03-08 15:57               ` Jan Beulich
2019-03-07 14:00       ` Ian Jackson
2019-03-08 15:43   ` Ian Jackson
2019-03-08 15:49     ` Jan Beulich
2019-03-05 22:38 ` [PATCH v11 6/9] xen/common: " Stefano Stabellini
2019-03-06 15:37   ` Jan Beulich
2019-03-06 21:08     ` Stefano Stabellini
2019-03-05 22:38 ` [PATCH v11 7/9] xen: " Stefano Stabellini
2019-03-05 22:38 ` [PATCH v11 8/9] xen: use DECLARE_BOUNDS in alternative.c Stefano Stabellini
2019-03-06 16:35   ` Jan Beulich
2019-03-06 21:38     ` Stefano Stabellini
2019-03-05 22:38 ` [PATCH v11 9/9] xen: explicit casts when DECLARE_BOUNDS cannot be used Stefano Stabellini
2019-03-07 11:40   ` Jan Beulich
2019-03-07 15:25     ` [PATCH v11 9/9] xen: explicit casts when DECLARE_BOUNDS cannot be used [and 1 more messages] Ian Jackson
2019-03-07 22:55       ` Stefano Stabellini
2019-03-08 15:39         ` [PATCH v11 9/9] xen: explicit casts when DECLARE_BOUNDS cannot be used [and 1 more messages] " Ian Jackson
2019-03-08  8:28       ` [PATCH v11 9/9] xen: explicit casts when DECLARE_BOUNDS cannot be used " Jan Beulich
2019-03-08 15:26 ` SRSL People... [PATCH v11 0/9] misc safety certification fixes Andrew Cooper
2019-03-08 15:44   ` Ian Jackson
2019-03-08 15:46   ` Jan Beulich
2019-03-08 20:14     ` Stefano Stabellini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5C80D8BF020000780021C55C@prv1-mh.provo.novell.com \
    --to=jbeulich@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=george.dunlap@citrix.com \
    --cc=ian.jackson@citrix.com \
    --cc=julien.grall@arm.com \
    --cc=sstabellini@kernel.org \
    --cc=stefanos@xilinx.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.