Browse Source

Merge pull request #3374 from laytan/fix-128-ints-alignment-arm64

fix 128 bit int alignment on arm64
gingerBill 1 year ago
parent
commit
7f0ca315b3
5 changed files with 69 additions and 5 deletions
  1. 1 1
      src/build_settings.cpp
  2. 1 1
      src/llvm_abi.cpp
  3. 5 2
      tests/internal/Makefile
  4. 3 1
      tests/internal/build.bat
  5. 59 0
      tests/internal/test_128.odin

+ 1 - 1
src/build_settings.cpp

@@ -521,7 +521,7 @@ gb_global TargetMetrics target_darwin_amd64 = {
 gb_global TargetMetrics target_darwin_arm64 = {
 gb_global TargetMetrics target_darwin_arm64 = {
 	TargetOs_darwin,
 	TargetOs_darwin,
 	TargetArch_arm64,
 	TargetArch_arm64,
-	8, 8, 8, 16,
+	8, 8, 16, 16,
 	str_lit("arm64-apple-macosx"), // NOTE: Changes during initialization based on build flags.
 	str_lit("arm64-apple-macosx"), // NOTE: Changes during initialization based on build flags.
 	str_lit("e-m:o-i64:64-i128:128-n32:64-S128"),
 	str_lit("e-m:o-i64:64-i128:128-n32:64-S128"),
 };
 };

+ 1 - 1
src/llvm_abi.cpp

@@ -275,7 +275,7 @@ gb_internal i64 lb_alignof(LLVMTypeRef type) {
 	case LLVMIntegerTypeKind:
 	case LLVMIntegerTypeKind:
 		{
 		{
 			unsigned w = LLVMGetIntTypeWidth(type);
 			unsigned w = LLVMGetIntTypeWidth(type);
-			return gb_clamp((w + 7)/8, 1, build_context.ptr_size);
+			return gb_clamp((w + 7)/8, 1, build_context.max_align);
 		}
 		}
 	case LLVMHalfTypeKind:
 	case LLVMHalfTypeKind:
 		return 2;
 		return 2;

+ 5 - 2
tests/internal/Makefile

@@ -1,6 +1,6 @@
 ODIN=../../odin
 ODIN=../../odin
 
 
-all: rtti_test map_test pow_test
+all: rtti_test map_test pow_test 128_test
 
 
 rtti_test:
 rtti_test:
 	$(ODIN) run test_rtti.odin -file -vet -strict-style -o:minimal
 	$(ODIN) run test_rtti.odin -file -vet -strict-style -o:minimal
@@ -9,4 +9,7 @@ map_test:
 	$(ODIN) run test_map.odin -file -vet -strict-style -o:minimal
 	$(ODIN) run test_map.odin -file -vet -strict-style -o:minimal
 
 
 pow_test:
 pow_test:
-	$(ODIN) run test_pow.odin -file -vet -strict-style -o:minimal
+	$(ODIN) run test_pow.odin -file -vet -strict-style -o:minimal
+
+128_test:
+	$(ODIN) run test_128.odin -file -vet -strict-style -o:minimal

+ 3 - 1
tests/internal/build.bat

@@ -3,4 +3,6 @@ set PATH_TO_ODIN==..\..\odin
 rem %PATH_TO_ODIN% run test_rtti.odin -file -vet -strict-style -o:minimal || exit /b
 rem %PATH_TO_ODIN% run test_rtti.odin -file -vet -strict-style -o:minimal || exit /b
 %PATH_TO_ODIN% run test_map.odin  -file -vet -strict-style -o:minimal || exit /b
 %PATH_TO_ODIN% run test_map.odin  -file -vet -strict-style -o:minimal || exit /b
 rem -define:SEED=42
 rem -define:SEED=42
-%PATH_TO_ODIN% run test_pow.odin  -file -vet -strict-style -o:minimal || exit /b
+%PATH_TO_ODIN% run test_pow.odin  -file -vet -strict-style -o:minimal || exit /b
+
+%PATH_TO_ODIN% run test_128.odin  -file -vet -strict-style -o:minimal || exit /b

+ 59 - 0
tests/internal/test_128.odin

@@ -0,0 +1,59 @@
+package test_128
+
+import "core:fmt"
+import "core:os"
+import "core:testing"
+
+TEST_count := 0
+TEST_fail  := 0
+
+when ODIN_TEST {
+	expect  :: testing.expect
+	log     :: testing.log
+} else {
+	expect  :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
+		TEST_count += 1
+		if !condition {
+			TEST_fail += 1
+			fmt.printf("[%v] %v\n", loc, message)
+			return
+		}
+	}
+	log     :: proc(t: ^testing.T, v: any, loc := #caller_location) {
+		fmt.printf("[%v] ", loc)
+		fmt.printf("log: %v\n", v)
+	}
+}
+
+main :: proc() {
+	t := testing.T{}
+
+	test_128_align(&t)
+
+	fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
+	if TEST_fail > 0 {
+		os.exit(1)
+	}
+}
+
+@test
+test_128_align :: proc(t: ^testing.T) {
+	Danger_Struct :: struct {
+		x: u128,
+		y: u64,
+	}
+
+	list := [?]Danger_Struct{{0, 0}, {1, 0}, {2, 0}, {3, 0}}
+
+	expect(t, list[0].x == 0, fmt.tprintf("[0].x (%v) != 0", list[0].x))
+	expect(t, list[0].y == 0, fmt.tprintf("[0].y (%v) != 0", list[0].y))
+
+	expect(t, list[1].x == 1, fmt.tprintf("[1].x (%v) != 1", list[1].x))
+	expect(t, list[1].y == 0, fmt.tprintf("[1].y (%v) != 0", list[1].y))
+
+	expect(t, list[2].x == 2, fmt.tprintf("[2].x (%v) != 2", list[2].x))
+	expect(t, list[2].y == 0, fmt.tprintf("[2].y (%v) != 0", list[2].y))
+
+	expect(t, list[3].x == 3, fmt.tprintf("[3].x (%v) != 3", list[3].x))
+	expect(t, list[3].y == 0, fmt.tprintf("[3].y (%v) != 0", list[3].y))
+}