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

@byteOffsetOf returns zero sometimes with packed struct #5528

Closed
LiterallyVoid opened this issue Jun 4, 2020 · 1 comment
Closed

@byteOffsetOf returns zero sometimes with packed struct #5528

LiterallyVoid opened this issue Jun 4, 2020 · 1 comment

Comments

@LiterallyVoid
Copy link
Contributor

Code:

const std = @import("std");

fn test_size(comptime size: comptime_int) void {
    const T = packed struct {
        a: [size]u8,
        b: u8,
    };
    std.debug.warn("{:<4}: byte: {:<5} / bit: {:<5}\n", .{
        size,
        @byteOffsetOf(T, "b"),
        @bitOffsetOf(T, "b")
    });
}

test "@byteOffsetOf" {
    std.debug.warn("\n", .{});
    {
        comptime var i = 0;
        inline while(i < 48) : (i += 1) {
            test_size(i);
        }
    }
}

Output (on x86_64):

1/1 test "@byteOffsetOf"...
   0: byte:     0 / bit:     0
   1: byte:     1 / bit:     8
   2: byte:     2 / bit:    16
   3: byte:     0 / bit:    24
   4: byte:     4 / bit:    32
   5: byte:     0 / bit:    40
   6: byte:     0 / bit:    48
   7: byte:     0 / bit:    56
   8: byte:     8 / bit:    64
   9: byte:     0 / bit:    72
  10: byte:     0 / bit:    80
  11: byte:     0 / bit:    88
  12: byte:     0 / bit:    96
  13: byte:     0 / bit:   104
  14: byte:     0 / bit:   112
  15: byte:     0 / bit:   120
  16: byte:    16 / bit:   128
  17: byte:     0 / bit:   136
  18: byte:     0 / bit:   144
  19: byte:     0 / bit:   152
  20: byte:     0 / bit:   160
  21: byte:     0 / bit:   168
  22: byte:     0 / bit:   176
  23: byte:     0 / bit:   184
  24: byte:    24 / bit:   192
  25: byte:     0 / bit:   200
  26: byte:     0 / bit:   208
  27: byte:     0 / bit:   216
  28: byte:     0 / bit:   224
  29: byte:     0 / bit:   232
  30: byte:     0 / bit:   240
  31: byte:     0 / bit:   248
  32: byte:    32 / bit:   256
  33: byte:     0 / bit:   264
  34: byte:     0 / bit:   272
  35: byte:     0 / bit:   280
  36: byte:     0 / bit:   288
  37: byte:     0 / bit:   296
  38: byte:     0 / bit:   304
  39: byte:     0 / bit:   312
  40: byte:    40 / bit:   320
  41: byte:     0 / bit:   328
  42: byte:     0 / bit:   336
  43: byte:     0 / bit:   344
  44: byte:     0 / bit:   352
  45: byte:     0 / bit:   360
  46: byte:     0 / bit:   368
  47: byte:     0 / bit:   376
OK
All 1 tests passed.

Output (on i386):

1/1 test "@byteOffsetOf"...
   0: byte:     0 / bit:     0
   1: byte:     1 / bit:     8
   2: byte:     2 / bit:    16
   3: byte:     0 / bit:    24
   4: byte:     4 / bit:    32
   5: byte:     0 / bit:    40
   6: byte:     0 / bit:    48
   7: byte:     0 / bit:    56
   8: byte:     8 / bit:    64
   9: byte:     0 / bit:    72
  10: byte:     0 / bit:    80
  11: byte:     0 / bit:    88
  12: byte:    12 / bit:    96
  13: byte:     0 / bit:   104
  14: byte:     0 / bit:   112
  15: byte:     0 / bit:   120
  16: byte:    16 / bit:   128
  17: byte:     0 / bit:   136
  18: byte:     0 / bit:   144
  19: byte:     0 / bit:   152
  20: byte:    20 / bit:   160
  21: byte:     0 / bit:   168
  22: byte:     0 / bit:   176
  23: byte:     0 / bit:   184
  24: byte:    24 / bit:   192
  25: byte:     0 / bit:   200
  26: byte:     0 / bit:   208
  27: byte:     0 / bit:   216
  28: byte:    28 / bit:   224
  29: byte:     0 / bit:   232
  30: byte:     0 / bit:   240
  31: byte:     0 / bit:   248
  32: byte:    32 / bit:   256
  33: byte:     0 / bit:   264
  34: byte:     0 / bit:   272
  35: byte:     0 / bit:   280
  36: byte:    36 / bit:   288
  37: byte:     0 / bit:   296
  38: byte:     0 / bit:   304
  39: byte:     0 / bit:   312
  40: byte:    40 / bit:   320
  41: byte:     0 / bit:   328
  42: byte:     0 / bit:   336
  43: byte:     0 / bit:   344
  44: byte:    44 / bit:   352
  45: byte:     0 / bit:   360
  46: byte:     0 / bit:   368
  47: byte:     0 / bit:   376
OK
All 1 tests passed.

Zig version: 0.6.0+fc48c232e

@daurnimator
Copy link
Contributor

Duplicate of #2627

@daurnimator daurnimator marked this as a duplicate of #2627 Jun 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants