diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-07-29 19:35:28 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-08-15 10:04:38 +0900 |
| commit | 412854c38e1dd79f59b48d5e43ab16c86e587145 (patch) | |
| tree | afd1d9a1a020e995acef7650caf2ffdddcea3c3f | |
| parent | 109d36631d5b95e0b0654b46eacccde37b52397d (diff) | |
| download | ducc-412854c38e1dd79f59b48d5e43ab16c86e587145.tar.gz ducc-412854c38e1dd79f59b48d5e43ab16c86e587145.tar.zst ducc-412854c38e1dd79f59b48d5e43ab16c86e587145.zip | |
refactor: extract alignment calculation
| -rw-r--r-- | ast.c | 20 |
1 files changed, 7 insertions, 13 deletions
@@ -82,6 +82,10 @@ int type_alignof(Type* ty) { } } +int to_aligned(int n, int a) { + return (n + a - 1) / a * a; +} + enum AstNodeKind { AstNodeKind_unknown, AstNodeKind_nop, @@ -279,20 +283,13 @@ int type_sizeof_struct(Type* ty) { int size = type_sizeof(member->ty); int align = type_alignof(member->ty); - if (next_offset % align != 0) { - padding = align - next_offset % align; - next_offset += padding; - } + next_offset = to_aligned(next_offset, align); next_offset += size; if (struct_align < align) { struct_align = align; } } - if (next_offset % struct_align != 0) { - padding = struct_align - next_offset % struct_align; - next_offset += padding; - } - return next_offset; + return to_aligned(next_offset, struct_align); } int type_alignof_struct(Type* ty) { @@ -323,10 +320,7 @@ int type_offsetof(Type* ty, const String* name) { int size = type_sizeof(member->ty); int align = type_alignof(member->ty); - if (next_offset % align != 0) { - int padding = align - next_offset % align; - next_offset += padding; - } + next_offset = to_aligned(next_offset, align); if (string_equals(&member->name, name)) { return next_offset; } |
