Unsigned long to double and back
David Lloyd
david.lloyd at redhat.com
Fri Nov 11 14:38:31 UTC 2022
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20221111/f1b36a38/attachment-0001.htm>
More information about the core-libs-dev
mailing list