Note that there are some explanatory texts on larger screens.

plurals
  1. POC "double to num" conversion code: why is it written like this?
    primarykey
    data
    text
    <p>I don't understand how the following C conversion functions work (and why they're written this way); I'm fairly certain that the original author knew what he was doing:</p> <pre><code>typedef union TValue { uint64_t u64; double n; struct { uint32_t lo; /* Lower 32 bits of number. */ uint32_t hi; /* Upper 32 bits of number. */ } u32; [...] } TValue; static int32_t num2bit(double n) { TValue o; o.n = n + 6755399441055744.0; /* 2^52 + 2^51 */ return (int32_t)o.u32.lo; } static uint64_t num2u64(double n) { #ifdef _MSC_VER if (n &gt;= 9223372036854775808.0) /* They think it's a feature. */ return (uint64_t)(int64_t)(n - 18446744073709551616.0); else #endif return (uint64_t)n; } </code></pre> <ul> <li>Does num2bit actually just cast a <code>double</code> into <code>int32_t</code>? Why the addition? Why write it like this?</li> <li>What is this "feature" that is alluded to in num2u64? (I believe _MSC_VER means it's the code-path for Microsofts C compiler).</li> </ul> <p>Note that those functions are not <em>always</em> used (depending on CPU architecture), this is for little-endian (I resolved some preprocessor macros to simplify).</p> <p>Links to online browseable mirror (the code is from the <a href="http://luajit.org" rel="nofollow">LuaJIT</a> project): Surrounding <a href="http://repo.or.cz/w/luajit-2.0.git/blob_plain/HEAD:/src/lj_obj.h" rel="nofollow">Header file</a> (or <a href="http://repo.or.cz/w/luajit-2.0.git/" rel="nofollow">whole project</a>).</p> <p>Every hint is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload