Stream: compiler development

Topic: packing f32's


view this post on Zulip Luke Boswell (Oct 19 2024 at 02:24):

I'm not sure if this is a roc issue... but I had a crack at packing two F32's into a U64 and can't quite get it to work. I've had a heap of help from different LLMs, but I'm getting a single test failure which I think may be roc related.

https://gist.github.com/lukewilliamboswell/3257ea8f5d7251a86e396b590987011c

Just wondering if we fuzzed these functions? is it worth exploring further?

view this post on Zulip Luke Boswell (Oct 19 2024 at 02:26):

── EXPECT FAILED in platform/InternalVector2.roc ───────────────────────────────

This expectation failed:

123│>  expect
124│>      # Test 8: Different exponents with fractions
125│>      parts1 = { sign: Bool.false, exponent: 130, fraction: 0x00555555 }
126│>      parts2 = { sign: Bool.false, exponent: 125, fraction: 0x00AAAAAA }
127│>      num1 = Num.f32FromParts parts1
128│>      num2 = Num.f32FromParts parts2
129│>      components = { x: num1, y: num2 }
130│>      packed = pack components
131│>      unpacked = unpack packed
132│>      num1unpacked = Num.f32ToParts unpacked.x
133│>      num2unpacked = Num.f32ToParts unpacked.y
134│>      num1unpacked == parts1 && num2unpacked == parts2

When it failed, these variables had these values:

parts1 : {
    exponent : Int Unsigned8,
    fraction : Int Unsigned32,
    sign : Bool,
}
parts1 = { exponent: 130, fraction: 5592405, sign: Bool.false }

parts2 : {
    exponent : Int Unsigned8,
    fraction : Int Unsigned32,
    sign : Bool,
}
parts2 = { exponent: 125, fraction: 11184810, sign: Bool.false }

num1 : F32
num1 = 13.333333

num2 : F32
num2 = 0.3333333

components : {
    x : F32,
    y : F32,
}
components = { x: 13.333333, y: 0.3333333 }

packed : Vector2D
packed = @Vector2D 4515609224609617237

unpacked : {
    x : F32,
    y : F32,
}
unpacked = { x: 13.333333, y: 0.3333333 }

num1unpacked : {
    exponent : Int Unsigned8,
    fraction : Int Unsigned32,
    sign : Bool,
}
num1unpacked = { exponent: 130, fraction: 5592405, sign: Bool.false }

num2unpacked : {
    exponent : Int Unsigned8,
    fraction : Int Unsigned32,
    sign : Bool,
}
num2unpacked = { exponent: 125, fraction: 2796202, sign: Bool.false }


1 failed and 10 passed in 606 ms.

view this post on Zulip Brendan Hansknecht (Oct 19 2024 at 03:34):

So the bug is the fraction of the second number

view this post on Zulip Luke Boswell (Oct 19 2024 at 03:38):

It looks that way

view this post on Zulip Brendan Hansknecht (Oct 19 2024 at 03:43):

Oh, actually the bug is your input

view this post on Zulip Brendan Hansknecht (Oct 19 2024 at 03:44):

Your fraction is getting truncated cause it is too large

view this post on Zulip Luke Boswell (Oct 19 2024 at 03:44):

Ahk, that's good to know

view this post on Zulip Brendan Hansknecht (Oct 19 2024 at 03:44):

That first A should be a 2

view this post on Zulip Brendan Hansknecht (Oct 19 2024 at 03:45):

Cause the highest bit is invalid to set in the fraction. It is bit 24

view this post on Zulip Brendan Hansknecht (Oct 19 2024 at 03:45):

You only get 23 bits

view this post on Zulip Brendan Hansknecht (Oct 19 2024 at 03:45):

This is where arbitrary precision ints would be really nice

view this post on Zulip Luke Boswell (Oct 19 2024 at 03:47):

It's probably worth giving this a shot over RocVector2 := { x : F32, y : F32, unused : I64, unused2 : I64, unused3 : I64, unused4 : I64 } eh?

view this post on Zulip Luke Boswell (Oct 19 2024 at 03:47):

Should be more efficient?

view this post on Zulip Brendan Hansknecht (Oct 19 2024 at 03:54):

Either is probably fast enough

view this post on Zulip Luke Boswell (Oct 19 2024 at 03:54):

Ok, I'll leave it for now then. What I have now is much simpler and easy to change

view this post on Zulip Brendan Hansknecht (Oct 19 2024 at 03:54):

Just didn't use a list of the of the wasteful one

view this post on Zulip Luke Boswell (Oct 19 2024 at 03:56):

Yeah, there's going to be lot's of extra allocations and unused space until we fix the ABI bug


Last updated: Jul 06 2025 at 12:14 UTC