From: Max Reitz <mreitz@redhat.com>
To: Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
qemu-devel@nongnu.org, qemu-block@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()
Date: Wed, 5 Jul 2017 19:00:45 +0200 [thread overview]
Message-ID: <fabe727c-7024-9f4b-99b3-fecb5da26acf@redhat.com> (raw)
In-Reply-To: <33f5a5f9-0a58-6b86-d3e1-2bee632a8493@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 2294 bytes --]
On 2017-07-05 18:29, Eric Blake wrote:
> On 07/05/2017 11:22 AM, Max Reitz wrote:
>
>>>>> return (double)x == x && x == y;
>>>>
>>>> Yes, that would do, too; and spares me of having to think about how well
>>>> comparing an arbitrary double to UINT64_MAX actually works. :-)
>>>
>>> On second thought, this won't do, because (double)x == x is always true
>>> if x is an integer (because this will implicitly cast the second x to a
>>> double, too). However, (uint64_t)(double)x == x should work.
>>
>> Hm, well, the nice thing with this is that (double)UINT64_MAX is
>> actually UINT64_MAX + 1, and now (uint64_t)(UINT64_MAX + 1) is
>> undefined... Urgs.
>
> (uint64_t)(UINT64_MAX + 1) is well-defined - it is 0.
>
> (Adding in unsigned integers is always well-defined - it wraps around on
> mathematical overflow modulo the integer size. You're thinking of
> overflow addition on signed integers, which is indeed undefined)
It's not. See the standard:
When a finite value of real floating type is converted to an integer
type other than _Bool, the fractional part is discarded (i.e., the value
is truncated toward zero). If the value of the integral part cannot be
represented by the integer type, the behavior is undefined. [61]
[61] The remaindering operation performed when a value of integer type
is converted to unsigned type need not be performed when a value of real
floating type is converted to unsigned type. Thus, the range of portable
real floating values is (−1, Utype_MAX+1).
See for yourself:
printf("%i\n", (uint64_t)(double)UINT64_MAX == UINT64_MAX);
This yields 1 with gcc and -O3.
>>
>> So I guess one thing that isn't very obvious but that should *always*
>> work (and is always well-defined) is this:
>>
>> For uint64_t: y < 0x1p64 && (uint64_t)y == x
>>
>> For int64_t: y >= -0x1p63 && y < 0x1p63 && (int64_t)y == x
>
> That's harder to read, compared to the double-cast method which is
> well-defined after all.
>
>>
>> I hope. :-/
>>
>> (But finally a chance to use binary exponents! Yay!)
>
> Justifying the use of binary exponents is going to be harder than that,
> and would need a really good comment in the code, compared to just using
> a safe double-cast.
It's not safe.
Max
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 498 bytes --]
next prev parent reply other threads:[~2017-07-05 17:01 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-03 12:25 [Qemu-devel] [PATCH v3 0/5] block: Don't compare strings in bdrv_reopen_prepare() Max Reitz
2017-07-03 12:25 ` [Qemu-devel] [PATCH v3 1/5] qapi/qnull: Add own header Max Reitz
2017-07-03 12:35 ` Eric Blake
2017-07-03 12:25 ` [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal() Max Reitz
2017-07-03 12:44 ` Eric Blake
2017-07-05 7:07 ` Markus Armbruster
2017-07-05 13:48 ` Max Reitz
2017-07-05 14:15 ` Eric Blake
2017-07-05 16:11 ` Markus Armbruster
2017-07-05 16:05 ` Max Reitz
2017-07-05 16:22 ` Max Reitz
2017-07-05 16:29 ` Eric Blake
2017-07-05 17:00 ` Max Reitz [this message]
2017-07-05 17:04 ` Max Reitz
2017-07-05 17:22 ` Eric Blake
2017-07-05 17:18 ` Eric Blake
2017-07-05 16:30 ` Max Reitz
2017-07-03 12:25 ` [Qemu-devel] [PATCH v3 3/5] block: qobject_is_equal() in bdrv_reopen_prepare() Max Reitz
2017-07-03 12:51 ` Eric Blake
2017-07-03 13:01 ` Max Reitz
2017-07-03 14:29 ` Eric Blake
2017-07-05 7:14 ` Markus Armbruster
2017-07-05 17:50 ` Max Reitz
2017-07-03 12:25 ` [Qemu-devel] [PATCH v3 4/5] iotests: Add test for non-string option reopening Max Reitz
2017-07-03 12:25 ` [Qemu-devel] [PATCH v3 5/5] tests: Add check-qobject for equality tests Max Reitz
2017-07-03 14:15 ` Eric Blake
2017-07-03 16:13 ` Max Reitz
2017-07-05 7:22 ` Markus Armbruster
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=fabe727c-7024-9f4b-99b3-fecb5da26acf@redhat.com \
--to=mreitz@redhat.com \
--cc=armbru@redhat.com \
--cc=eblake@redhat.com \
--cc=kwolf@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).