Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sema: implement @splat for arrays #21635

Merged
merged 1 commit into from
Oct 10, 2024
Merged

Sema: implement @splat for arrays #21635

merged 1 commit into from
Oct 10, 2024

Conversation

mlugg
Copy link
Member

@mlugg mlugg commented Oct 8, 2024

Resolves: #20433

@mlugg mlugg enabled auto-merge (rebase) October 8, 2024 22:36
src/Sema.zig Show resolved Hide resolved
src/Sema.zig Outdated
Comment on lines 24844 to 24845
@memset(elems[0..len], scalar_val.toIntern());
elems[len] = sentinel.toIntern();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these need to be @intCasted, prob just cast the const len =

src/Sema.zig Outdated
Comment on lines 24859 to 24860
const elems = try sema.arena.alloc(Air.Inst.Ref, len + @intFromBool(maybe_sentinel != null));
@memset(elems[0..len], scalar);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two will also need to be int casted. ditto above.

@mlugg mlugg force-pushed the splat-array branch 2 times, most recently from 8d150aa to e261adc Compare October 10, 2024 06:27
@mlugg mlugg merged commit c96f9a0 into ziglang:master Oct 10, 2024
10 checks passed
@andrewrk
Copy link
Member

Release notes please!

@andrewrk andrewrk added the release notes This PR should be mentioned in the release notes. label Oct 10, 2024
@mlugg
Copy link
Member Author

mlugg commented Oct 17, 2024

Release Notes

Zig 0.14.0 expands the @splat builtin to apply not only to vectors, but to arrays. This is useful when default-initializing an array to a constant value. For instance, in conjunction with [Decl Literals}(link to relevent release notes section), we can elegantly initialize an array of "color" values:

const Rgba = struct {
    r: u8,
    b: u8,
    g: u8,
    a: u8,
    pub const black: Rgba = .{ .r = 0, .g = 0, .b = 0, .a = 255 };
};
var pixels: [width][height]Rgba = @splat(@splat(.black));

The operand may be comptime-known or runtime-known. In addition, this builtin can also be used to initialize sentinel-terminated arrays.

const std = @import("std");
const assert = std.debug.assert;
const expect = std.testing.expect;
test "initialize sentinel-terminated array" {
    // the sentinel does not need to match the value 
    const arr: [2:0]u8 = @splat(10);
    try expect(arr[0] == 10);
    try expect(arr[1] == 10);
    try expect(arr[2] == 0);
}
test "initialize runtime array" {
    var runtime_known: u8 = undefined;
    runtime_known = 123;
    // the operand can be runtime-known, giving a runtime-known array
    const arr: [2]u8 = @splat(runtime_known);
    try expect(arr[0] == 123);
    try expect(arr[1] == 123);
}
test "initialize zero-length sentinel-terminated array" {
    var runtime_known: u8 = undefined;
    runtime_known = 123;
    const arr: [0:10]u8 = @splat(runtime_known);
    // the operand was runtime-known, but since the array length was zero, the result is comptime-known
    comptime assert(arr[0] == 10);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes This PR should be mentioned in the release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: allow @splat for arrays
3 participants