Eehhh, what the hell is going on here!?

SELECT
    printf("0x%x", (1 << 63) - 2),
    printf("0x%x", (1 << 63) - 1),
    printf("0x%x",  1 << 63     ),
    printf("0x%x", (1 << 63) + 1),
    printf("0x%x", (1 << 63) + 2)

SQLite yields:

0x8000000000000000 (instead of 0x7ffffffffffffffe)
0x8000000000000000 (instead of 0x7fffffffffffffff)
0x8000000000000000 (correct)
0x8000000000000001 (correct)
0x8000000000000002 (correct)

Huh!? O_o Am I stupid? What am I missing here? Or is this actually a bug? :-?

With 62 bits, everything is spot on:

0x3ffffffffffffffe
0x3fffffffffffffff
0x4000000000000000
0x4000000000000001
0x4000000000000002

And 64 bits rather unsurprisingly also yield:

0xfffffffffffffffe
0xffffffffffffffff
0x0
0x1
0x2

โค‹ Read More

@lyse@lyse.isobeef.org

Disclaimer: Canโ€™t guarantee that Iโ€™m fully awake and Iโ€™m being trained at work not to use my brain anymore, so maybe this is complete bullshit. ๐Ÿ˜ช๐ŸงŸโ€โ™€๏ธ

It says here that SQLite uses signed integers:

https://sqlite.org/datatype3.html

In pure bits, 1 << 63 would be 0x8000000000000000, but as a signed value, it gets interpreted as -9223372036854775808. Subtracting 1 yields -9223372036854775809 โ€“ but that doesnโ€™t fit in 64 bits anymore. Itโ€™s possible that SQLite doesnโ€™t want to wrap around but instead saturates? Havenโ€™t checked. ๐Ÿค”

With 62 bits, there is enough room.

With 1 << 64, I have no idea how SQLite wants to handle this, because this should immediately trigger a warning, because it doesnโ€™t fit right away. Maybe it gets truncated to 0?

sqlite> select printf('0x%x', 2 * (1 << 64));
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ printf('0x%x', 2 ... โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 0x0                  โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
sqlite> select printf('0x%x', 0 - 1);
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ printf('0x%x', 0 ... โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 0xffffffffffffffff   โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
sqlite> select printf('0x%x', 0 - 2);
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ printf('0x%x', 0 ... โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 0xfffffffffffffffe   โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

โค‹ Read More

@lyse@lyse.isobeef.org AI result ahead, feel free to ignore.

I โ€œaskedโ€ the AI at work the same question out of morbid curiousity. It โ€œsaidโ€ that SQLite converts that integer to floating point internally on overflows and then, when converting back, the x86 instruction cvttsd2si will turn it into 0x8000000000000000, even if the actual floating point value is outside of that range. So, yes, it allegedly actually saturates, as a side effect of the type conversion.

I couldnโ€™t find anything about that automatic conversion in SQLiteโ€™s manual, yet, but an experiment looks like it might be true:

sqlite> select typeof(1 << 63);
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ typeof(1 << 63) โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ integer         โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

sqlite> select typeof((1 << 63) - 1);
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ typeof((1 << 63) ... โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ real                 โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

As for cvttsd2si, this source confirms the handling of 0x8000000000000000 on range errors: https://www.felixcloutier.com/x86/cvttsd2si

The following C program also confirms it (run through gdb to see cvttsd2si in action):

<a href="https://we.loveprivacy.club/search?q=%23include">#include</a> <stdint.h>
<a href="https://we.loveprivacy.club/search?q=%23include">#include</a> <stdio.h>

int
main()
{
    int64_t i;
    double d;

    /* -3000 instead of -1, because `double` canโ€™t represent a
     * difference of -1 at this scale. */
    d = -9223372036854775808.0 - 3000;

    i = d;
    printf("%lf, 0x%lx, %ld\n", d, i, i);

    return 0;
}

(Remark about AI usage: Fine, I got an answer and maybe itโ€™s even correct. But doing this completely ruined it for me. It would have been much more satisfying to figure this out myself. I actually suspected some floating point stuff going on here, but instead of verifying this myself I reached for the unethical tool and denied myself a little bit of fun at the weekend. Wonโ€™t do that again.)

โค‹ Read More

@movq@www.uninformativ.de Yup, Iโ€™ve also seen the floating point conversion happening with (1 << 63) - 1 yesterday night. But instead of pausing to think about it for a second, somehow all I had in mind was โ€œgive me a better representation, ainโ€™t gonna have time for this shitโ€, so I turned it to hex. Beyond my comprehension what I was thinking there. O_o Thatโ€™s embarrassing, unbelievable. Well, I blame late oโ€™clock where my brain had already quit on me and went to bed.

Very interesting data point you raise there. The fun part didnโ€™t cross my mind yet or at least I couldnโ€™t pinpoint it. In hindsight itโ€™s totally obvious, though. Past experience also tells me the exact same. Dealing with a problem and researching something myself is a so much more better teacher. The longer I faced up with a topic, the higher the chance to really manifest in long- or at least mid-term memory. If I just get told something, the odds are that itโ€™s completely erased from memory in a matter of days if not hours.

โค‹ Read More

@lyse@lyse.isobeef.org Yes, and thatโ€™s why Iโ€™m 100% convinced that weโ€™ll see a massive brain drain in a couple of years. This will affect young people even more, because they donโ€™t have all the โ€œoldโ€ knowledge to fall back on.

Itโ€™s concerning, Iโ€™ve warned about it many times, nobody listens.

I think the best thing one can do is explicitly not use any AI tools but keep your actual skills intact. Might be out of a (good) job for a while, but once this bubble bursts, this is who is going to get hired again. (I think.)

And considering how insanely expensive all this is, Iโ€™m still (mostly) convinced that the bubble will actually burst. This stuff just isnโ€™t sustainable.

โ€ฆ or I might be wrong. And if so, I see an even darker future that I donโ€™t want to put into words right now.

โค‹ Read More

Participate

Login or Register to join in on this yarn.