#determine size of comptime_int and comptime_float

1 messages · Page 1 of 1 (latest)

hidden basin
#

I thought I could write code to determine these sizes, but the following does not work. The reported size is zero. Is there another way I can do this?

const std = @import("std");
const expectEqual = std.testing.expectEqual;

test "comptime sizes" {
    const i = 7;
    try expectEqual(comptime_int, @TypeOf(i));
    // try expectEqual(8, @sizeOf(@TypeOf(i))); // fails

    const f = 3.14;
    try expectEqual(comptime_float, @TypeOf(f));
    // try expectEqual(8, @sizeOf(@TypeOf(f))); // fails
}
brisk seal
#

the size is zero and that is correct

#

no runtime size

#

why do you even expect it to be 8?

hidden basin
#

Is that because the value gets embedded into the generate code?

brisk seal
#

no it doesn't. Comptime int no show up in .rodata

#

cant

hidden basin
#

Can you explain that more? What is "rodata"?

brisk seal
#

what did you mean by embedded into the generated code

#

if you know how assembly works / what a binary file is, .rodata is a label for the read only data section in the final binary/executable/object file

hidden basin
#

Okay, I didn't know about that. Thanks!

#

What if I had an array of comptime_int values? Wouldn't that have a non-zero size?

brisk seal
#

look man, its not gonna show up in your final binary so the concept of size doesnt really make sense

#

it only takes up space in the compilers memory. not in your binaries

hidden basin
#

Interesting! Is it fair to say the comptime_int and comptime_float values are inlined in the generated code? For example, if I write a program that reads numbers from a file, iterates over them, and prints the result of multiplying each number by some comptime_int value then it seems the comptime_int value must be known at run-time. I'm trying to learn what happens to that value in the generated code.

brisk seal
#

it shows up directly in the instruction stream

#

as something called an immediate value