Browse Source

runtime: map_cell_index_static produced wrong results when the number of elements per cell was a power of 2

fleandro 8 months ago
parent
commit
e3de02eaa8
2 changed files with 30 additions and 6 deletions
  1. 5 5
      base/runtime/dynamic_map_internal.odin
  2. 25 1
      tests/core/runtime/test_core_runtime.odin

+ 5 - 5
base/runtime/dynamic_map_internal.odin

@@ -161,11 +161,11 @@ map_cell_index_static :: #force_inline proc "contextless" (cells: [^]Map_Cell($T
 		// Compute the integer log 2 of N, this is the shift amount to index the
 		// correct cell. Odin's intrinsics.count_leading_zeros does not produce a
 		// constant, hence this approach. We only need to check up to N = 64.
-		SHIFT :: 1 when N < 2  else
-		         2 when N < 4  else
-		         3 when N < 8  else
-		         4 when N < 16 else
-		         5 when N < 32 else 6
+		SHIFT :: 1 when N == 2  else
+		         2 when N == 4  else
+		         3 when N == 8  else
+		         4 when N == 16 else
+		         5 when N == 32 else 6
 		#assert(SHIFT <= MAP_CACHE_LINE_LOG2)
 		// Unique case, no need to index data here since only one element.
 		when N == 1 {

+ 25 - 1
tests/core/runtime/test_core_runtime.odin

@@ -63,4 +63,28 @@ test_init_cap_map_dynarray :: proc(t: ^testing.T) {
         defer delete(d2)
         testing.expect(t, cap(d2) == 0)
         testing.expect(t, d2.allocator.procedure == ally.procedure)
-}
+}
+
+@(test)
+test_map_get :: proc(t: ^testing.T) {
+	m := map[int][3]int{
+		1 = {10, 100, 1000},
+		2 = {20, 200, 2000},
+		3 = {30, 300, 3000},
+	}
+
+	k1, v1, ok1 := runtime.map_get(m, 1)
+	testing.expect_value(t, k1, 1)
+	testing.expect_value(t, v1, [3]int{10, 100, 1000})
+	testing.expect_value(t, ok1, true)
+
+	k2, v2, ok2 := runtime.map_get(m, 2)
+	testing.expect_value(t, k2, 2)
+	testing.expect_value(t, v2, [3]int{20, 200, 2000})
+	testing.expect_value(t, ok2, true)
+
+	k3, v3, ok3 := runtime.map_get(m, 3)
+	testing.expect_value(t, k3, 3)
+	testing.expect_value(t, v3, [3]int{30, 300, 3000})
+	testing.expect_value(t, ok3, true)
+}