Improve matrix indices to offset logic

This commit is contained in:
gingerBill
2021-11-08 12:15:57 +00:00
parent 23f0fbc376
commit a674e842d0
3 changed files with 18 additions and 11 deletions
+9 -9
View File
@@ -1929,12 +1929,12 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) {
if fi.hash { if fi.hash {
// Printed as it is written // Printed as it is written
io.write_byte(fi.writer, '\n') io.write_byte(fi.writer, '\n')
for col in 0..<info.column_count { for row in 0..<info.row_count {
fmt_write_indent(fi) fmt_write_indent(fi)
for row in 0..<info.row_count { for col in 0..<info.column_count {
if row > 0 { io.write_string(fi.writer, ", ") } if col > 0 { io.write_string(fi.writer, ", ") }
offset := (col + row*info.elem_stride)*info.elem_size offset := (row + col*info.elem_stride)*info.elem_size
data := uintptr(v.data) + uintptr(offset) data := uintptr(v.data) + uintptr(offset)
fmt_arg(fi, any{rawptr(data), info.elem.id}, verb) fmt_arg(fi, any{rawptr(data), info.elem.id}, verb)
@@ -1943,12 +1943,12 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) {
} }
} else { } else {
// Printed in Row-Major layout to match text layout // Printed in Row-Major layout to match text layout
for col in 0..<info.column_count { for row in 0..<info.row_count {
if col > 0 { io.write_string(fi.writer, "; ") } if row > 0 { io.write_string(fi.writer, "; ") }
for row in 0..<info.row_count { for col in 0..<info.column_count {
if row > 0 { io.write_string(fi.writer, ", ") } if col > 0 { io.write_string(fi.writer, ", ") }
offset := (col + row*info.elem_stride)*info.elem_size offset := (row + col*info.elem_stride)*info.elem_size
data := uintptr(v.data) + uintptr(offset) data := uintptr(v.data) + uintptr(offset)
fmt_arg(fi, any{rawptr(data), info.elem.id}, verb) fmt_arg(fi, any{rawptr(data), info.elem.id}, verb)
+6
View File
@@ -1010,6 +1010,11 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
if (op != Token_RangeHalf) { if (op != Token_RangeHalf) {
hi += 1; hi += 1;
} }
GB_ASSERT(0 <= lo && lo <= max_count);
GB_ASSERT(0 <= hi && hi <= max_count);
GB_ASSERT(lo <= hi);
TypeAndValue tav = fv->value->tav; TypeAndValue tav = fv->value->tav;
LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value; LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
for (i64 k = lo; k < hi; k++) { for (i64 k = lo; k < hi; k++) {
@@ -1021,6 +1026,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
TypeAndValue index_tav = fv->field->tav; TypeAndValue index_tav = fv->field->tav;
GB_ASSERT(index_tav.mode == Addressing_Constant); GB_ASSERT(index_tav.mode == Addressing_Constant);
i64 index = exact_value_to_i64(index_tav.value); i64 index = exact_value_to_i64(index_tav.value);
GB_ASSERT(index < max_count);
TypeAndValue tav = fv->value->tav; TypeAndValue tav = fv->value->tav;
LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value; LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
i64 offset = matrix_row_major_index_to_offset(type, index); i64 offset = matrix_row_major_index_to_offset(type, index);
+3 -2
View File
@@ -1406,15 +1406,16 @@ i64 matrix_indices_to_offset(Type *t, i64 row_index, i64 column_index) {
GB_ASSERT(0 <= row_index && row_index < t->Matrix.row_count); GB_ASSERT(0 <= row_index && row_index < t->Matrix.row_count);
GB_ASSERT(0 <= column_index && column_index < t->Matrix.column_count); GB_ASSERT(0 <= column_index && column_index < t->Matrix.column_count);
i64 stride_elems = matrix_type_stride_in_elems(t); i64 stride_elems = matrix_type_stride_in_elems(t);
return stride_elems*column_index + row_index; // NOTE(bill): Column-major layout internally
return row_index + stride_elems*column_index;
} }
i64 matrix_row_major_index_to_offset(Type *t, i64 index) { i64 matrix_row_major_index_to_offset(Type *t, i64 index) {
t = base_type(t); t = base_type(t);
GB_ASSERT(t->kind == Type_Matrix); GB_ASSERT(t->kind == Type_Matrix);
i64 column_index = index%t->Matrix.column_count;
i64 row_index = index/t->Matrix.column_count; i64 row_index = index/t->Matrix.column_count;
i64 column_index = index%t->Matrix.column_count;
return matrix_indices_to_offset(t, row_index, column_index); return matrix_indices_to_offset(t, row_index, column_index);
} }
i64 matrix_column_major_index_to_offset(Type *t, i64 index) { i64 matrix_column_major_index_to_offset(Type *t, i64 index) {