<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div>Hi all,<br data-mce-bogus="1"></div><div>I don't know if it's the correct way to do it or if there is a better way, but i'm using<br></div><div><br data-mce-bogus="1"></div><div>  var result = (double) (value & <span class="pl-c1">0x7fffffffffffffffL</span>);<br>  if (value < 0) {<br>    result = result + 9.223372036854776E18;<br>  }<br>  <br></div><div><br data-mce-bogus="1"></div><div>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.</div><div><br data-mce-bogus="1"></div><div>I've got 9.223372036854776E18 as the result of<br data-mce-bogus="1"></div><div>  BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE).doubleValue()</div><div><br data-mce-bogus="1"></div><div>Rémi<br data-mce-bogus="1"></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"David Lloyd" <david.lloyd@redhat.com><br><b>To: </b>"Johannes Kuhn" <info@j-kuhn.de><br><b>Cc: </b>"core-libs-dev" <core-libs-dev@openjdk.java.net><br><b>Sent: </b>Friday, November 11, 2022 3:38:31 PM<br><b>Subject: </b>Re: Unsigned long to double and back<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Well, I typed this out from memory so there's an error, of course. `(tmp & 1)` should be `(input & 1)`.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Nov 11, 2022 at 8:31 AM David Lloyd <<a href="mailto:david.lloyd@redhat.com" target="_blank">david.lloyd@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I encountered this issue as well; for now I'm using the following transformation:</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">long tmp = input >>> 1;</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">double output = ((double) tmp) * 2.0 + (tmp & 1);</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I... *think* it's correct but I'm not 100% sure and have a long-standing TODO to try and figure it out...</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Nov 5, 2022 at 7:17 PM Johannes Kuhn <<a href="mailto:info@j-kuhn.de" target="_blank">info@j-kuhn.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">When I tried to implement an WASM transpiler, I noticed some missing <br>
conversion methods from unsigned types to floating point, for example <br>
from unsigned long to a double.<br>
<br>
For the meaning of unsigned long, see Long.toUnsignedString(long i).<br>
<br>
Converting between unsigned long and floating point is not a trivial <br>
task, as it probably requires some bit manipulation.<br>
<br>
In particular, I would love to see the following methods added*:<br>
<br>
- double Double.fromUnsignedLong(long i)<br>
- long Double.toUnsignedLong(double d)<br>
- float Float.fromUnsignedLong(long i)<br>
- long Float.toUnsignedLong(float f)<br>
<br>
* Subject to bikeshedding - I don't care about the name, or if it is <br>
added to the Long class.<br>
<br>
Currently, I don't think that additional methods for unsigned int are <br>
necessary - as it is possible to cast between long and int, but feel <br>
free to correct me.<br>
<br>
In WASM, the specification for those methods can be found here:<br>
<br>
<a href="https://www.w3.org/TR/wasm-core-1/#op-trunc-u" rel="noreferrer" target="_blank">https://www.w3.org/TR/wasm-core-1/#op-trunc-u</a><br>
<a href="https://www.w3.org/TR/wasm-core-1/#op-convert-u" rel="noreferrer" target="_blank">https://www.w3.org/TR/wasm-core-1/#op-convert-u</a><br>
<br>
Note that the WASM specification is undefined for some values, notably <br>
NaN, infinities, and values that fall out of the range.<br>
<br>
As *I* want to use it to implement WASM instructions, I do not have any <br>
strong opinion on the undefined cases - for example, returning the <br>
nearest unsigned long value or throwing an exception is fine for me.<br>
<br>
What do you think?<br>
<br>
- Johannes<br>
<br>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr"><div dir="ltr">- DML • he/him<br></div></div>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr">- DML • he/him</div></div><br></blockquote></div></div></body></html>