Repository navigation
Support primitive types with unused bits (Julia 1.14) - #17
Open
AntonOresten wants to merge 1 commit into
Open
AntonOresten wants to merge 1 commit into
AntonOresten wants to merge 1 commit into
Conversation
Julia 1.14 lets a primitive type declare fewer bits than it stores (`primitive type Float4 4 end`), exposed as `Core.bitsizeof`, and its `sizeof` now rounds such types up to their alignment (a 24-bit type takes 4 bytes). `bitwidth` falls back to `Core.bitsizeof` where it exists, so these types need no overload and byte-multiple types keep their real width. `reinterpret` refuses these types, both as scalars and as array or tuple elements. New public `raw_bits`/`from_raw_bits` widen and truncate them instead, and `NArray`, `NarrowTuple` and `NarrowArray` go through them, with `NarrowArray` routing dense arrays of such types through their raw bits. `copy(::NarrowArray)` now builds concrete `SVector`s, which keeps it inferable on that path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #17 +/- ##
==========================================
- Coverage 98.44% 97.47% -0.98%
==========================================
Files 7 7
Lines 578 593 +15
==========================================
+ Hits 569 578 +9
- Misses 9 15 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Julia 1.14 lets a primitive type declare fewer bits than it stores (JuliaLang/julia#61359), e.g.
primitive type Float4 4 end, which occupies a byte and hasCore.bitsizeof(Float4) == 4. JuliaLang/julia#62852, also in 1.14, roundssizeofof such types up to their alignment, so a 24-bit primitive now takes 4 bytes.Changes
bitwidthfallback:Core.bitsizeof(T)where it exists (@static if isdefined(Core, :bitsizeof)), else8 * sizeof(T)as before. These types need no overload, and byte-multiple types like a 24-bit primitive keep their real width; the old fallback reports 32 on 1.14.Bool,Pad,NArrayandNarrowTuplekeep their own methods.raw_bits(x)/from_raw_bits(T, bits)(new, public):reinterpretrejects these types (bitcastrequires equal bit sizes), so these zero-extend and truncate them instead, and fall back toreinterpretfor everything else.NArray,NarrowTuple: use the helpers. On 1.14,NVectorandNarrowTupleof such types threw.NarrowArray:reinterpretalso refuses arrays of these types, so the constructor,copy,T.(narr)anddest .= xroute them through raw bits (one temporary). Other types take the samereinterpretpath as before.copynow builds concreteSVector{L,T}s instead ofmap(SArray, …), which keeps it inferable on the new path.Testing
test/unused_bits.jlruns only whereCore.bitsizeofexists, covering native 4-, 6- and 24-bit types across the scalar helpers,NVector,NarrowTupleandNarrowArray. It uses@inferredbecause GPU broadcasts need these to infer (an earlierfrom_raw_bits.(T, x)with a static parameterTdid not).CuArray): construction,copy,T.(narr)andNarrow(T).(d)work for a native 4-bit type, andcopy/UInt8.(…)forBool. I didn't run the opt-in cuTile tests locally; they don't use the changed paths.Not in this PR
dest .= cuarrayinto a GPU-backedNarrowArraythrows a kernel exception. It does the same onmainwithBool: GPUArrays'copyto!takes over and callssetindex!on theNarrowArray. It needs a method that's aware of GPUArrays, so I left it for a separate change.🤖 Generated with Claude Code