Unsigned long to double and back
David Lloyd
david.lloyd at redhat.com
Fri Nov 11 16:08:48 UTC 2022
I'd be hesitant to do that in decimal; it's probably better to add
`0x1.0p63` instead. But I guess the approaches are similar: shave off one
bit, and then test that bit and adjust the result accordingly.
On Fri, Nov 11, 2022 at 9:59 AM Remi Forax <forax at univ-mlv.fr> wrote:
> Hi all,
> I don't know if it's the correct way to do it or if there is a better way,
> but i'm using
>
> var result = (double) (value & 0x7fffffffffffffffL);
> if (value < 0) {
> result = result + 9.223372036854776E18;
> }
>
>
> The idea is to mask the sign bit, do the conversion to double and if the
> sign bit was present (if value < 0) add Long.MAX_VALUE + 1
> (9223372036854775808) as a double.
>
> I've got 9.223372036854776E18 as the result of
> BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE).doubleValue()
>
> Rémi
>
> ------------------------------
>
> *From: *"David Lloyd" <david.lloyd at redhat.com>
> *To: *"Johannes Kuhn" <info at j-kuhn.de>
> *Cc: *"core-libs-dev" <core-libs-dev at openjdk.java.net>
> *Sent: *Friday, November 11, 2022 3:38:31 PM
> *Subject: *Re: Unsigned long to double and back
>
> Well, I typed this out from memory so there's an error, of course. `(tmp &
> 1)` should be `(input & 1)`.
>
> On Fri, Nov 11, 2022 at 8:31 AM David Lloyd <david.lloyd at redhat.com>
> wrote:
>
>> I encountered this issue as well; for now I'm using the following
>> transformation:
>>
>> long tmp = input >>> 1;
>> double output = ((double) tmp) * 2.0 + (tmp & 1);
>>
>> I... *think* it's correct but I'm not 100% sure and have a long-standing
>> TODO to try and figure it out...
>>
>> On Sat, Nov 5, 2022 at 7:17 PM Johannes Kuhn <info at j-kuhn.de> wrote:
>>
>>> When I tried to implement an WASM transpiler, I noticed some missing
>>> conversion methods from unsigned types to floating point, for example
>>> from unsigned long to a double.
>>>
>>> For the meaning of unsigned long, see Long.toUnsignedString(long i).
>>>
>>> Converting between unsigned long and floating point is not a trivial
>>> task, as it probably requires some bit manipulation.
>>>
>>> In particular, I would love to see the following methods added*:
>>>
>>> - double Double.fromUnsignedLong(long i)
>>> - long Double.toUnsignedLong(double d)
>>> - float Float.fromUnsignedLong(long i)
>>> - long Float.toUnsignedLong(float f)
>>>
>>> * Subject to bikeshedding - I don't care about the name, or if it is
>>> added to the Long class.
>>>
>>> Currently, I don't think that additional methods for unsigned int are
>>> necessary - as it is possible to cast between long and int, but feel
>>> free to correct me.
>>>
>>> In WASM, the specification for those methods can be found here:
>>>
>>> https://www.w3.org/TR/wasm-core-1/#op-trunc-u
>>> https://www.w3.org/TR/wasm-core-1/#op-convert-u
>>>
>>> Note that the WASM specification is undefined for some values, notably
>>> NaN, infinities, and values that fall out of the range.
>>>
>>> As *I* want to use it to implement WASM instructions, I do not have any
>>> strong opinion on the undefined cases - for example, returning the
>>> nearest unsigned long value or throwing an exception is fine for me.
>>>
>>> What do you think?
>>>
>>> - Johannes
>>>
>>>
>>
>> --
>> - DML • he/him
>>
>
>
> --
> - DML • he/him
>
>
--
- DML • he/him
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20221111/9cd16c3a/attachment.htm>
More information about the core-libs-dev
mailing list