diff --git a/CMakeLists.txt b/CMakeLists.txt index 3770cb5e29..e241c08e8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1102,6 +1102,8 @@ src/simulate/simulate_fusion_op2_bin_vec.cpp src/simulate/simulate_fusion_op2_scalar_vec.cpp src/simulate/simulate_fusion_at.cpp src/simulate/simulate_fusion_at_array.cpp +src/simulate/simulate_fusion_ptr_at.cpp +src/simulate/simulate_fusion_widen.cpp src/simulate/simulate_fusion_tableindex.cpp src/simulate/simulate_fusion_tablewithhash.cpp src/simulate/simulate_fusion_misc_copy.cpp diff --git a/benchmarks/README.md b/benchmarks/README.md index 098e562ad2..c234765f74 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -131,7 +131,16 @@ Small-table regime micro-benchmarks (N from 1 to 64) - the load profile the larg | File | Description | |---|---| +| `bench_arr_at_i64.das` | Array read and write with an `int`, `int64` and `uint64` index, same loop shape for all three | +| `bench_double_index.das` | A table indexed by an element of another table: fixed arrays and arrays of `int` and `uint8`, local, global and argument; the Doom column loops through global `uint8` pointers; a class-then-transition automaton step | +| `bench_global_at.das` | Global array and global fixed array read and write by index in a context that is not solid (the global is found by name hash) | +| `bench_global_local_op2.das` | A global combined with a local in a binary operation under `options solid_context` | +| `bench_ptr_at.das` | `p[i]` read through a pointer: local, argument, global and field pointer; local, expression, constant and `uint` index; `int`, `uint8` and `float3` elements | +| `bench_ptr_set.das` | `p[i] = value` and `*p = value` through a pointer, `int` and `uint8` elements, with and without a narrowing cast on the value | +| `bench_small_int_at.das` | Array and fixed-array read of `uint8`, `uint16` and `int16` elements cast to `int` or `uint` | +| `bench_table_index_i64.das` | Table read with an `int`, `int64` and `uint64` key, same loop shape for all three | | `bench_v_ldu.das` | Fusion-engine `Op2At` array-indexed read at sizeof(T) in {4,8,12,16} - int, int64, float3, float4. Used to compare DAS_FUSION=0 vs current `DAS_LDU_WORKHORSE` ladder vs `v_zero+memcpy(sizeof(CTYPE))` | +| `bench_widen_read.das` | A small int cast to `int` or `uint` when read from a loop variable, a field of a loop variable, a local struct field and an argument struct field | ## sql/ diff --git a/benchmarks/fusion/bench_double_index.das b/benchmarks/fusion/bench_double_index.das new file mode 100644 index 0000000000..33675cf152 --- /dev/null +++ b/benchmarks/fusion/bench_double_index.das @@ -0,0 +1,158 @@ +options gen2 +options persistent_heap + +require dastest/testing_boost + +let N = 10000 +let OUTER = 10 +let TOTAL = N * OUTER +let STATES = 64 + +var g_lut : int[256] +var g_remap : int[256] +var g_bytes : uint8[256] +var g_colormap : uint8? +var g_translation : uint8? +var g_source : uint8? + +def sum_argument_arrays(lut_array, remap_array : array) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += lut_array[remap_array[i & 255]] + } + } + return sum +} + +def run_automaton(classes : uint8 const?; transitions : int const?; input : uint8 const?) { + var state = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + unsafe { + let c = int(classes[int(input[i])]) + state = transitions[state * STATES + c] + } + } + } + return state +} + +[benchmark] +def double_index_tables(b : B?) { + var lut, remap : int[256] + var bytes : uint8[256] + for (i in range(256)) { + lut[i] = i + remap[i] = 255 - i + bytes[i] = uint8(255 - i) + g_lut[i] = i + g_remap[i] = 255 - i + g_bytes[i] = uint8(255 - i) + } + let lut_array <- [for (i in range(256)); i] + let remap_array <- [for (i in range(256)); 255 - i] + var expected = 0 + for (i in range(N)) { + expected += 255 - (i & 255) + } + expected *= OUTER + b |> run("local_fixed_int_of_int/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += lut[remap[i & 255]] + } + } + b |> equal(sum, expected) + } + b |> run("local_fixed_int_of_uint8/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += lut[int(bytes[i & 255])] + } + } + b |> equal(sum, expected) + } + b |> run("global_fixed_int_of_int/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += g_lut[g_remap[i & 255]] + } + } + b |> equal(sum, expected) + } + b |> run("global_fixed_int_of_uint8/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += g_lut[int(g_bytes[i & 255])] + } + } + b |> equal(sum, expected) + } + b |> run("argument_array_int_of_int/{TOTAL}", TOTAL) { + b |> equal(sum_argument_arrays(lut_array, remap_array), expected) + } +} + +[benchmark] +def double_index_pointers(b : B?) { + var colormap <- [for (i in range(256)); uint8(255 - i)] + var translation <- [for (i in range(256)); uint8(i ^ 1)] + var source <- [for (i in range(128)); uint8(i * 2)] + var screen : array + screen |> resize(N) + g_colormap = unsafe(addr(colormap[0])) + g_translation = unsafe(addr(translation[0])) + g_source = unsafe(addr(source[0])) + let dest = unsafe(addr(screen[0])) + b |> run("doom_column/{TOTAL}", TOTAL) { + for (_j in range(OUTER)) { + var frac = 0 + var dest_addr : uint64 = unsafe(reinterpret(dest)) + for (_i in range(N)) { + unsafe { + *reinterpret(dest_addr) = g_colormap[int(g_source[frac >> 16 & 127])] + } + dest_addr++ + frac += 40000 + } + } + b |> equal(int(screen[0]), 255) + } + b |> run("doom_translated_column/{TOTAL}", TOTAL) { + for (_j in range(OUTER)) { + var frac = 0 + var dest_addr : uint64 = unsafe(reinterpret(dest)) + for (_i in range(N)) { + unsafe { + *reinterpret(dest_addr) = g_colormap[int(g_translation[ + int(g_source[frac >> 16 & 127])])] + } + dest_addr++ + frac += 40000 + } + } + b |> equal(int(screen[0]), 254) + } + g_colormap = null + g_translation = null + g_source = null +} + +[benchmark] +def double_index_automaton(b : B?) { + var classes <- [for (i in range(256)); uint8(i & (STATES - 1))] + var transitions <- [for (i in range(STATES * STATES)); (i * 7 + 3) & (STATES - 1)] + var input <- [for (i in range(N)); uint8((i * 31) & 255)] + let pclasses = unsafe(addr(classes[0])) + let ptransitions = unsafe(addr(transitions[0])) + let pinput = unsafe(addr(input[0])) + let expected = run_automaton(pclasses, ptransitions, pinput) + b |> run("class_then_transition/{TOTAL}", TOTAL) { + b |> equal(run_automaton(pclasses, ptransitions, pinput), expected) + } +} diff --git a/benchmarks/fusion/bench_global_at.das b/benchmarks/fusion/bench_global_at.das new file mode 100644 index 0000000000..caf5a81679 --- /dev/null +++ b/benchmarks/fusion/bench_global_at.das @@ -0,0 +1,60 @@ +options gen2 +options persistent_heap + +require dastest/testing_boost + +let N = 10000 +let OUTER = 10 +let TOTAL = N * OUTER + +var g_values : array +var g_table : int[1024] + +[benchmark] +def global_at(b : B?) { + delete g_values + g_values <- [for (i in range(N)); i & 1023] + for (i in range(1024)) { + g_table[i] = i + } + var expected = 0 + for (v in g_values) { + expected += v + } + expected *= OUTER + b |> run("array_local_idx/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += g_values[i] + } + } + b |> equal(sum, expected) + } + b |> run("array_expr_idx/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += g_values[N - 1 - i] + } + } + b |> equal(sum, expected) + } + b |> run("fixed_expr_idx/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += g_table[i & 1023] + } + } + b |> equal(sum, expected) + } + b |> run("fixed_write_expr_idx/{TOTAL}", TOTAL) { + for (_j in range(OUTER)) { + for (i in range(N)) { + g_table[i & 1023] = i + } + } + b |> equal(g_table[0], 9216) + } +} diff --git a/benchmarks/fusion/bench_global_local_op2.das b/benchmarks/fusion/bench_global_local_op2.das new file mode 100644 index 0000000000..11280852c0 --- /dev/null +++ b/benchmarks/fusion/bench_global_local_op2.das @@ -0,0 +1,41 @@ +options gen2 +options persistent_heap +options solid_context = true + +require dastest/testing_boost + +let N = 10000 +let OUTER = 10 +let TOTAL = N * OUTER + +var g_scale = 3 +var g_bias = 0.5 + +[benchmark] +def global_op_local(b : B?) { + var expected = 0 + for (i in range(N)) { + expected += g_scale * i + } + expected *= OUTER + b |> run("int_mul/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += g_scale * i + } + } + b |> equal(sum, expected) + } + b |> run("float_add/{TOTAL}", TOTAL) { + var sum = 0.0 + for (_j in range(OUTER)) { + var x = 0.0 + for (_i in range(N)) { + x = g_bias + x + } + sum += x + } + b |> equal(sum > 0.0, true) + } +} diff --git a/benchmarks/fusion/bench_ptr_at.das b/benchmarks/fusion/bench_ptr_at.das new file mode 100644 index 0000000000..dfd4ae8b5c --- /dev/null +++ b/benchmarks/fusion/bench_ptr_at.das @@ -0,0 +1,132 @@ +options gen2 +options persistent_heap + +require dastest/testing_boost +require daslib/linq + +let N = 10000 +let OUTER = 10 +let TOTAL = N * OUTER + +struct Holder { + data : int? +} + +var g_data : int? + +def sum_through_argument(p : int?) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += unsafe(p[i]) + } + } + return sum +} + +def make_ints() => <- [for (i in range(N)); i & 1023] + +[benchmark] +def ptr_at_int(b : B?) { + var arr <- make_ints() + let expected = sum(arr) * OUTER + var p = unsafe(addr(arr[0])) + b |> run("local_ptr_local_idx/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += unsafe(p[i]) + } + } + b |> equal(sum, expected) + } + b |> run("local_ptr_expr_idx/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += unsafe(p[N - 1 - i]) + } + } + b |> equal(sum, expected) + } + b |> run("local_ptr_uint_idx/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in urange(uint(N))) { + sum += unsafe(p[i]) + } + } + b |> equal(sum, expected) + } + b |> run("local_ptr_const_idx/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (_i in range(N)) { + sum += unsafe(p[1]) + } + } + b |> equal(sum, TOTAL) + } + b |> run("argument_ptr_local_idx/{TOTAL}", TOTAL) { + b |> equal(sum_through_argument(p), expected) + } + g_data = p + b |> run("global_ptr_local_idx/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += unsafe(g_data[i]) + } + } + b |> equal(sum, expected) + } + g_data = null + var holder = new Holder(data = p) + b |> run("field_ptr_local_idx/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += unsafe(holder.data[i]) + } + } + b |> equal(sum, expected) + } + unsafe { + delete holder + } +} + +[benchmark] +def ptr_at_uint8(b : B?) { + var bytes <- [for (i in range(N)); uint8(i & 255)] + var expected = 0 + for (v in bytes) { + expected += int(v) + } + expected *= OUTER + let p = unsafe(addr(bytes[0])) + b |> run("local_ptr_local_idx/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += int(unsafe(p[i])) + } + } + b |> equal(sum, expected) + } +} + +[benchmark] +def ptr_at_float3(b : B?) { + var points <- [for (i in range(N)); float3(float(i & 7), 1.0, 2.0)] + let p = unsafe(addr(points[0])) + b |> run("local_ptr_local_idx/{TOTAL}", TOTAL) { + var sum = float3(0.0) + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += unsafe(p[i]) + } + } + b |> equal(sum.y, float(TOTAL)) + } +} diff --git a/benchmarks/fusion/bench_ptr_set.das b/benchmarks/fusion/bench_ptr_set.das new file mode 100644 index 0000000000..b073b95aee --- /dev/null +++ b/benchmarks/fusion/bench_ptr_set.das @@ -0,0 +1,76 @@ +options gen2 +options persistent_heap + +require dastest/testing_boost + +let N = 10000 +let OUTER = 10 +let TOTAL = N * OUTER + +def fill_through_argument(var p : int?) { + for (_j in range(OUTER)) { + for (i in range(N)) { + unsafe(p[i]) = i + 1 + } + } +} + +[benchmark] +def ptr_set_int(b : B?) { + var arr : array + arr |> resize(N) + var p = unsafe(addr(arr[0])) + b |> run("local_ptr_local_idx/{TOTAL}", TOTAL) { + for (_j in range(OUTER)) { + for (i in range(N)) { + unsafe(p[i]) = i + 1 + } + } + b |> equal(arr[N - 1], N) + } + b |> run("local_ptr_expr_idx/{TOTAL}", TOTAL) { + for (_j in range(OUTER)) { + for (i in range(N)) { + unsafe(p[N - 1 - i]) = i + 2 + } + } + b |> equal(arr[0], N + 1) + } + b |> run("argument_ptr_local_idx/{TOTAL}", TOTAL) { + fill_through_argument(p) + b |> equal(arr[N - 1], N) + } + b |> run("deref_local_ptr/{TOTAL}", TOTAL) { + for (_j in range(OUTER)) { + for (i in range(N)) { + *p = i + 3 + } + } + b |> equal(arr[0], N + 2) + } +} + +[benchmark] +def ptr_set_uint8(b : B?) { + var bytes : array + bytes |> resize(N) + var p = unsafe(addr(bytes[0])) + b |> run("local_ptr_local_idx_narrowed/{TOTAL}", TOTAL) { + for (_j in range(OUTER)) { + for (i in range(N)) { + unsafe(p[i]) = uint8(i + 1) + } + } + b |> equal(int(bytes[254]), 255) + } + b |> run("deref_walking_ptr_narrowed/{TOTAL}", TOTAL) { + for (_j in range(OUTER)) { + var dest_addr : uint64 = unsafe(reinterpret(p)) + for (i in range(N)) { + *unsafe(reinterpret(dest_addr)) = uint8(i + 2) + dest_addr++ + } + } + b |> equal(int(bytes[253]), 255) + } +} diff --git a/benchmarks/fusion/bench_small_int_at.das b/benchmarks/fusion/bench_small_int_at.das new file mode 100644 index 0000000000..0bbebdcf3e --- /dev/null +++ b/benchmarks/fusion/bench_small_int_at.das @@ -0,0 +1,83 @@ +options gen2 +options persistent_heap + +require dastest/testing_boost + +let N = 10000 +let OUTER = 10 +let TOTAL = N * OUTER + +def sum_bytes_argument(bytes : array) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += int(bytes[i]) + } + } + return sum +} + +[benchmark] +def array_at_small_int(b : B?) { + var bytes <- [for (i in range(N)); uint8(i & 255)] + var words <- [for (i in range(N)); uint16(i)] + var bytes_expected = 0 + for (v in bytes) { + bytes_expected += int(v) + } + bytes_expected *= OUTER + var words_expected = 0u + for (v in words) { + words_expected += uint(v) + } + words_expected *= uint(OUTER) + b |> run("uint8_local_array/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += int(bytes[i]) + } + } + b |> equal(sum, bytes_expected) + } + b |> run("uint8_argument_array/{TOTAL}", TOTAL) { + b |> equal(sum_bytes_argument(bytes), bytes_expected) + } + b |> run("uint16_local_array_to_uint/{TOTAL}", TOTAL) { + var sum = 0u + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += uint(words[i]) + } + } + b |> equal(sum, words_expected) + } +} + +[benchmark] +def fixed_at_small_int(b : B?) { + var bytes : uint8[256] + var shorts : int16[256] + for (i in range(256)) { + bytes[i] = uint8(i) + shorts[i] = int16(i - 128) + } + b |> run("uint8_local_fixed/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += int(bytes[i & 255]) + } + } + b |> equal(sum > 0, true) + } + b |> run("int16_local_fixed/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (i in range(N)) { + sum += int(shorts[i & 255]) + } + } + b |> equal(sum < 0, true) + } +} diff --git a/benchmarks/fusion/bench_widen_read.das b/benchmarks/fusion/bench_widen_read.das new file mode 100644 index 0000000000..ed4a1c57fe --- /dev/null +++ b/benchmarks/fusion/bench_widen_read.das @@ -0,0 +1,88 @@ +options gen2 +options persistent_heap + +require dastest/testing_boost + +let N = 10000 +let OUTER = 10 +let TOTAL = N * OUTER + +struct Texel { + pad : int + color : uint8 + height : uint16 +} + +def sum_color_argument(t : Texel) { + var sum = 0 + for (_j in range(OUTER)) { + for (_i in range(N)) { + sum += int(t.color) + } + } + return sum +} + +[benchmark] +def widen_read(b : B?) { + var bytes <- [for (i in range(N)); uint8(i & 255)] + var texels <- [for (i in range(N)); Texel(color = uint8(i & 255), height = uint16(i))] + var lut : int[256] + for (i in range(256)) { + lut[i] = i + } + var expected = 0 + for (v in bytes) { + expected += int(v) + } + expected *= OUTER + b |> run("loop_variable_to_int/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (c in bytes) { + sum += int(c) + } + } + b |> equal(sum, expected) + } + b |> run("loop_variable_as_index/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (c in bytes) { + sum += lut[int(c)] + } + } + b |> equal(sum, expected) + } + b |> run("loop_struct_field_to_int/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (t in texels) { + sum += int(t.color) + } + } + b |> equal(sum, expected) + } + b |> run("loop_struct_field_to_uint/{TOTAL}", TOTAL) { + var sum = 0u + for (_j in range(OUTER)) { + for (t in texels) { + sum += uint(t.height) + } + } + b |> equal(sum > 0u, true) + } + let texel = texels[3] + b |> run("local_struct_field_to_int/{TOTAL}", TOTAL) { + var sum = 0 + for (_j in range(OUTER)) { + for (_i in range(N)) { + sum += int(texel.color) + } + } + b |> equal(sum, 3 * TOTAL) + } + b |> run("argument_struct_field_to_int/{TOTAL}", TOTAL) { + b |> equal(sum_color_argument(texel), 3 * TOTAL) + } +} diff --git a/include/daScript/simulate/simulate_nodes.h b/include/daScript/simulate/simulate_nodes.h index 6a879aa783..f22d04041b 100644 --- a/include/daScript/simulate/simulate_nodes.h +++ b/include/daScript/simulate/simulate_nodes.h @@ -23,6 +23,7 @@ namespace das { , sThisBlockArgumentRef , sGlobal , sShared + , sGlobalMnh }; struct DAS_API SimSource { @@ -89,6 +90,11 @@ namespace das { mangledNameHash = mnh; offset = ofs; } + __forceinline void setGlobalMnh(uint32_t ofs, uint64_t mnh) { + type = SimSourceType::sGlobalMnh; + mangledNameHash = mnh; + offset = ofs; + } __forceinline void setBlockCMResOfs(uint32_t asp, uint32_t ofs) { type = SimSourceType::sBlockCMResOff; argStackTop = asp; @@ -1123,6 +1129,12 @@ namespace das { uint32_t stride, offset; }; + template struct PtrAtR2VName; + template <> struct PtrAtR2VName { constexpr static const char * name() { return "PtrAtR2V"; } }; + template <> struct PtrAtR2VName { constexpr static const char * name() { return "PtrAtR2V_U32"; } }; + template <> struct PtrAtR2VName { constexpr static const char * name() { return "PtrAtR2V_I64"; } }; + template <> struct PtrAtR2VName { constexpr static const char * name() { return "PtrAtR2V_U64"; } }; + template struct SimNode_PtrAtR2V : SimNode_PtrAt { SimNode_PtrAtR2V ( const LineInfo & at, SimNode * rv, SimNode * idx, uint32_t strd, uint32_t o ) @@ -2704,7 +2716,7 @@ SIM_NODE_AT_VECTOR(Float, float) DAS_PTR_NODE; SimNode_GetGlobalMnh ( const LineInfo & at, uint32_t o, uint64_t mnh ) : SimNode_SourceBase(at) { - subexpr.setGlobal(o,mnh); + subexpr.setGlobalMnh(o,mnh); } virtual SimNode * visit ( SimVisitor & vis ) override; __forceinline char * compute (Context & context) { diff --git a/include/daScript/simulate/simulate_visit.h b/include/daScript/simulate/simulate_visit.h index a0e7661c6a..7988be2a26 100644 --- a/include/daScript/simulate/simulate_visit.h +++ b/include/daScript/simulate/simulate_visit.h @@ -142,7 +142,7 @@ namespace das { template SimNode * SimNode_PtrAtR2V::visit ( SimVisitor & vis ) { V_BEGIN(); - V_OP_TT(PtrAtR2V); + vis.op(PtrAtR2VName::name(), sizeof(TT), typeName::name()); V_SUB_THIS(value); V_SUB_THIS(index); V_ARG_THIS(stride); diff --git a/src/simulate/ARCHITECTURE.md b/src/simulate/ARCHITECTURE.md new file mode 100644 index 0000000000..874f0041e8 --- /dev/null +++ b/src/simulate/ARCHITECTURE.md @@ -0,0 +1,66 @@ +# src/simulate Architecture + +`src/simulate/` holds the interpreter's runtime sources. The node declarations sit in +`include/daScript/simulate/`, whose own architecture document covers them. This document +covers the fusion engine, the `simulate_fusion*` files. + +A fused node is one interpreter node that replaces a node and the simple children it reads +through: a local, an argument, a global, a constant. `fusionContext` (`simulate_fusion.cpp`) +walks every function a round at a time, children before their parent, until a round fuses +nothing. A node fused in one round is seen by its parent in the next round under its fused +name: the op name followed by the source name of each operand, `PtrAtR2VGlobAny` for one. + +The semantic hash of a function is taken from its fused tree. A new fused node therefore +changes the hash of every function that gets it, and AOT output is tied to the fusion level +that generated it. + +## Small-int reads answer widened {#fused-small-int-reads} + +A small int is `int8`, `uint8`, `int16` or `uint16`. A read of one from memory hands it to +`eval` widened to 32 bits: zero-extended when unsigned, sign-extended when signed. A small-int +argument slot is not such a read: a fused call fills it zero-extended whatever its sign. + +A non-fused read of a small int answers `eval` only. Its `evalInt` reads four bytes from a +slot one or two bytes wide, so nothing calls it. A fused read of a small int answers `eval`, +`evalInt` and `evalUInt` with the same widened value (`DAS_FUSION_WIDE_NODE`, +`simulate_fusion.h`). The fused reads are the small-int stamps of the array, fixed-array and +pointer index families, and the widened reads (`simulate_fusion_widen.cpp`). + +A cast from a small int to `int` or `uint` changes no bit of that widened value. So the widen +fusion point replaces the cast over a read of a local, of a local reference or of an argument +reference with one widened read, and removes the cast over a fused small-int index read: the +read below it answers `evalInt` and `evalUInt` directly. It tells a fused index read from a +non-fused one by its name, an op name from `g_fusedSmallIntReads` followed by two source +names. The same cast over any other node stays: a by-value small-int argument, a field +reached through a pointer, a read with a 64-bit index. + +The small-int stamps exist for a 32-bit index only. An array, fixed-array or pointer read of +a small int with a 64-bit index stays on the non-fused node. + +## The pointer index family {#pointer-index-family} + +`simulate_fusion_ptr_at.cpp` fuses `p[i]` on a pointer: the read by value, the read by +reference, and the store. One class template computes the address from two policy types, where +the pointer comes from and where the index comes from. A table of instantiations stamps the +element types. The family has no bounds check and no null check, like the nodes it replaces; +the index is `int` or `uint`, and a 64-bit index stays on the non-fused node. + +A store is `p[i] = value`, for the same pointer and index sources as the read. `*p = value` +is the store without an index; it fuses for a pointer held in a local or an argument, and +keeps the null check of the `Ptr2Ref` node it replaces. A store evaluates the value first and +the address second, the order of the `Set` node it replaces. A small-int store takes its value +through `eval` and keeps the low bits, so a narrowing cast from `int` or `uint` right under it +is removed. + +The family is compiled at `DAS_FUSION` 2 and above. The small-int stamps, the widened reads +and the cast removal are compiled at `DAS_FUSION` 1 and above. + +## Globals read by name hash {#global-by-hash-source} + +A context that is not solid reads a global through its mangled name hash: the node holds the +hash and looks the offset up on every access. `SimSourceType::sGlobalMnh` is that source, named +`GlobMnh` in a fused node's name. The array and fixed-array index families take it as a base: +their two files define `FUSION_OP2_SET_GLOBAL_MNH` before they include the set permutation +list, which adds the lines for it. The pointer index family takes it as a base too. The `Set` +family and the table index family include the same list without the define, and the binary +operation and call families have a list of their own; none of them takes a global by hash. diff --git a/src/simulate/simulate_fusion.cpp b/src/simulate/simulate_fusion.cpp index a9ac14596d..395a10b12d 100644 --- a/src/simulate/simulate_fusion.cpp +++ b/src/simulate/simulate_fusion.cpp @@ -33,6 +33,34 @@ namespace das { return (it->second.name == name) && (it->second.typeName==typeName); } + static const char * const g_unknownSimSourceName = "???"; + + static const char * simSourceNameAt ( int index ) { + const char * name = getSimSourceName(SimSourceType(index)); + return strcmp(name, g_unknownSimSourceName)==0 ? nullptr : name; + } + + static bool isSimSourceName ( const char * text ) { + for ( int index=0; const char * name = simSourceNameAt(index); ++index ) { + if ( strcmp(text, name)==0 ) return true; + } + return false; + } + + bool FusionPoint::isFusedOp2 ( const SimNodeInfoLookup & info, SimNode * node, const char * op, const char * typeName ) { + auto it = info.find(node); + if ( it==info.end() || it->second.typeName!=typeName ) return false; + size_t opLength = strlen(op); + const char * name = it->second.name.c_str(); + if ( strncmp(name, op, opLength)!=0 ) return false; + const char * sources = name + opLength; + for ( int index=0; const char * left = simSourceNameAt(index); ++index ) { + size_t leftLength = strlen(left); + if ( strncmp(sources, left, leftLength)==0 && isSimSourceName(sources + leftLength) ) return true; + } + return false; + } + SimNode * SimNode_Op1Fusion::visit(SimVisitor & vis) { V_BEGIN(); string name = op; @@ -61,6 +89,23 @@ namespace das { V_END(); } + SimNode * SimNode_Op2Index::visit(SimVisitor & vis) { + V_BEGIN(); + string name = op; + name += getSimSourceName(l.type); + name += getSimSourceName(r.type); + if ( baseType!=Type::none ) { + vis.op(name.c_str(), getTypeBaseSize(baseType), das_to_string(baseType)); + } else { + vis.op(name.c_str()); + } + l.visit(vis); + r.visit(vis); + V_ARG(stride); + V_ARG(offset); + V_END(); + } + SimNode * FusionPointOp1::fuseOp1(const SimNodeInfoLookup & info, SimNode * node, SimNode * node_x, Context * context) const { SimNode_Op1Fusion * result = (SimNode_Op1Fusion *) match(info,node,node_x,context); if (result) { @@ -120,8 +165,9 @@ namespace das { case SimSourceType::sThisBlockArgumentRef: return "Tbargr"; case SimSourceType::sGlobal: return "Glob"; case SimSourceType::sShared: return "Shared"; + case SimSourceType::sGlobalMnh: return "GlobMnh"; }; - return "???"; + return g_unknownSimSourceName; } #if DAS_FUSION @@ -147,6 +193,10 @@ namespace das { extern const FusionTable fusionTable_tablewithhash; extern const FusionTable fusionTable_call1; extern const FusionTable fusionTable_call2; + extern const FusionTable fusionTable_widen; +#if DAS_FUSION>=2 + extern const FusionTable fusionTable_ptr_at; +#endif #endif #if DAS_FUSION @@ -173,6 +223,10 @@ namespace das { &fusionTable_tablewithhash, &fusionTable_call1, &fusionTable_call2, + &fusionTable_widen, +#if DAS_FUSION>=2 + &fusionTable_ptr_at, +#endif }; #endif diff --git a/src/simulate/simulate_fusion.h b/src/simulate/simulate_fusion.h index f9a3bac836..f7ef5931d6 100644 --- a/src/simulate/simulate_fusion.h +++ b/src/simulate/simulate_fusion.h @@ -21,6 +21,24 @@ else { vec4f __r = v_zero(); memcpy(&__r, (src_ptr), sizeof(CTYPE)); (dst) = __r; } \ } while (0) +// src/simulate/ARCHITECTURE.md#fused-small-int-reads +#define DAS_FUSION_WIDE_NODE \ + DAS_EVAL_ABI virtual vec4f eval ( das::Context & context ) override { \ + return das::cast::from(compute(context)); \ + } \ + virtual int32_t evalInt ( das::Context & context ) override { \ + return compute(context); \ + } \ + virtual uint32_t evalUInt ( das::Context & context ) override { \ + return uint32_t(compute(context)); \ + } + +#define DAS_FUSION_SMALL_INTS(STAMP,ARG) \ + STAMP(ARG,int8_t) \ + STAMP(ARG,uint8_t) \ + STAMP(ARG,int16_t) \ + STAMP(ARG,uint16_t) + namespace das { typedef char * StringPtr; @@ -54,6 +72,7 @@ namespace das { static bool is ( const SimNodeInfoLookup & info, SimNode * node, const char * name ); static bool is2 ( const SimNodeInfoLookup & info, SimNode * lnode, SimNode * rnode, const char * lname, const char * rname ); static bool is ( const SimNodeInfoLookup & info, SimNode * node, const char * name, const char * typeName ); + static bool isFusedOp2 ( const SimNodeInfoLookup & info, SimNode * node, const char * op, const char * typeName ); protected: ~FusionPoint() = default; }; @@ -86,6 +105,12 @@ namespace das { SimSource l, r; }; + struct SimNode_Op2Index : SimNode_Op2Fusion { + SimNode_Op2Index(const char * msg = nullptr) : SimNode_Op2Fusion(msg) {} + virtual SimNode * visit(SimVisitor & vis) override; + uint32_t stride, offset; + }; + struct FusionPointOp1 : FusionPoint { virtual SimNode * match(const SimNodeInfoLookup &, SimNode *, SimNode *, Context *) const = 0; virtual void set(SimNode_Op1Fusion * result, SimNode * node) const = 0; diff --git a/src/simulate/simulate_fusion_at.cpp b/src/simulate/simulate_fusion_at.cpp index acd0a31310..f8ee8ac69f 100644 --- a/src/simulate/simulate_fusion_at.cpp +++ b/src/simulate/simulate_fusion_at.cpp @@ -11,6 +11,8 @@ #include "daScript/simulate/sim_policy.h" #include "daScript/simulate/simulate_visit_op.h" +#define FUSION_OP2_SET_GLOBAL_MNH + namespace das { struct SimNode_Op2At : SimNode_Op2Fusion { @@ -51,7 +53,7 @@ namespace das { DAS_AT_CHECK(rr, range); \ return *((CTYPE *)(pl + rr*stride + offset)); \ } \ - DAS_NODE(TYPE,CTYPE); \ + DAS_AT_NODE_TAIL(TYPE,CTYPE); \ }; #define IMPLEMENT_OP2_SET_NODE(INLINE,OPNAME,TYPE,CTYPE,COMPUTEL,COMPUTER) \ @@ -63,7 +65,7 @@ namespace das { DAS_AT_CHECK(rr, range); \ return *((CTYPE *)(pl + rr*stride + offset)); \ } \ - DAS_NODE(TYPE,CTYPE); \ + DAS_AT_NODE_TAIL(TYPE,CTYPE); \ }; #define IMPLEMENT_OP2_SET_SETUP_NODE(result,node) \ @@ -80,12 +82,23 @@ namespace das { #include "simulate_fusion_op2_set_impl.h" #include "simulate_fusion_op2_set_perm.h" +#define DAS_AT_NODE_TAIL(TYPE,CTYPE) DAS_NODE(TYPE,CTYPE) #define DAS_AT_CHECK DAS_AT_CHECK_ON IMPLEMENT_SETOP_SCALAR(AtR2V); #undef DAS_AT_CHECK #define DAS_AT_CHECK DAS_AT_CHECK_OFF IMPLEMENT_SETOP_SCALAR(AtR2VU); #undef DAS_AT_CHECK +#undef DAS_AT_NODE_TAIL + +#define DAS_AT_NODE_TAIL(TYPE,CTYPE) DAS_FUSION_WIDE_NODE +#define DAS_AT_CHECK DAS_AT_CHECK_ON + IMPLEMENT_SETOP_SMALL_INT(AtR2V); +#undef DAS_AT_CHECK +#define DAS_AT_CHECK DAS_AT_CHECK_OFF + IMPLEMENT_SETOP_SMALL_INT(AtR2VU); +#undef DAS_AT_CHECK +#undef DAS_AT_NODE_TAIL /* AtR2V VECTOR */ @@ -434,6 +447,7 @@ namespace das { static constexpr FusionEntry fusionEntries_at[] = { REGISTER_SETOP_SCALAR(AtR2V) + REGISTER_SETOP_SMALL_INT(AtR2V) REGISTER_SETOP_NUMERIC_VEC(AtR2V) FUSION_ENTRY("At","",FusionPoint_Set_At_StringPtr) FUSION_ENTRY("At","",FusionPoint_Set_At_VoidPtr) @@ -446,6 +460,7 @@ namespace das { FUSION_ENTRY("At_U64","",FusionPoint_Set_At_U64_StringPtr) FUSION_ENTRY("At_U64","",FusionPoint_Set_At_U64_VoidPtr) REGISTER_SETOP_SCALAR(AtR2VU) + REGISTER_SETOP_SMALL_INT(AtR2VU) REGISTER_SETOP_NUMERIC_VEC(AtR2VU) FUSION_ENTRY("AtU","",FusionPoint_Set_AtU_StringPtr) FUSION_ENTRY("AtU","",FusionPoint_Set_AtU_VoidPtr) @@ -454,5 +469,7 @@ namespace das { FUSION_TABLE(fusionTable_at, fusionEntries_at) } +#undef FUSION_OP2_SET_GLOBAL_MNH + #endif diff --git a/src/simulate/simulate_fusion_at_array.cpp b/src/simulate/simulate_fusion_at_array.cpp index e8b4d59c56..6b90fead74 100644 --- a/src/simulate/simulate_fusion_at_array.cpp +++ b/src/simulate/simulate_fusion_at_array.cpp @@ -12,27 +12,11 @@ #include "daScript/simulate/sim_policy.h" #include "daScript/simulate/simulate_visit_op.h" +#define FUSION_OP2_SET_GLOBAL_MNH + namespace das { - struct SimNode_Op2ArrayAt : SimNode_Op2Fusion { - virtual SimNode * visit(SimVisitor & vis) override { - V_BEGIN(); - string name = op; - name += getSimSourceName(l.type); - name += getSimSourceName(r.type); - if (baseType != Type::none) { - vis.op(name.c_str(), getTypeBaseSize(baseType), das_to_string(baseType)); - } else { - vis.op(name.c_str()); - } - l.visit(vis); - r.visit(vis); - V_ARG(stride); - V_ARG(offset); - V_END(); - } - uint32_t stride, offset; - }; + using SimNode_Op2ArrayAt = SimNode_Op2Index; // the 32-bit array bounds check, toggled per node family: checked op-name (ArrayAt/ArrayAtR2V) // instantiates with it ON, the unchecked op-name (ArrayAtU/ArrayAtR2VU, emitted only where the @@ -57,7 +41,7 @@ namespace das { DAS_ARRAYAT_CHECK(rr); \ return *((CTYPE *)(pl->data + uint64_t(rr)*uint64_t(stride) + offset)); \ } \ - DAS_NODE(TYPE,CTYPE); \ + DAS_ARRAYAT_NODE_TAIL(TYPE,CTYPE); \ }; #define IMPLEMENT_OP2_SET_NODE(INLINE,OPNAME,TYPE,CTYPE,COMPUTEL,COMPUTER) \ @@ -69,7 +53,7 @@ namespace das { DAS_ARRAYAT_CHECK(rr); \ return *((CTYPE *)(pl->data + uint64_t(rr)*uint64_t(stride) + offset)); \ } \ - DAS_NODE(TYPE,CTYPE); \ + DAS_ARRAYAT_NODE_TAIL(TYPE,CTYPE); \ }; #define IMPLEMENT_OP2_SET_SETUP_NODE(result,node) \ @@ -84,12 +68,23 @@ namespace das { #include "simulate_fusion_op2_set_impl.h" #include "simulate_fusion_op2_set_perm.h" +#define DAS_ARRAYAT_NODE_TAIL(TYPE,CTYPE) DAS_NODE(TYPE,CTYPE) #define DAS_ARRAYAT_CHECK DAS_ARRAYAT_CHECK_ON IMPLEMENT_SETOP_SCALAR(ArrayAtR2V); #undef DAS_ARRAYAT_CHECK #define DAS_ARRAYAT_CHECK DAS_ARRAYAT_CHECK_OFF IMPLEMENT_SETOP_SCALAR(ArrayAtR2VU); #undef DAS_ARRAYAT_CHECK +#undef DAS_ARRAYAT_NODE_TAIL + +#define DAS_ARRAYAT_NODE_TAIL(TYPE,CTYPE) DAS_FUSION_WIDE_NODE +#define DAS_ARRAYAT_CHECK DAS_ARRAYAT_CHECK_ON + IMPLEMENT_SETOP_SMALL_INT(ArrayAtR2V); +#undef DAS_ARRAYAT_CHECK +#define DAS_ARRAYAT_CHECK DAS_ARRAYAT_CHECK_OFF + IMPLEMENT_SETOP_SMALL_INT(ArrayAtR2VU); +#undef DAS_ARRAYAT_CHECK +#undef DAS_ARRAYAT_NODE_TAIL /* ArrayAtR2V VECTOR */ @@ -421,9 +416,11 @@ namespace das { static constexpr FusionEntry fusionEntries_at_array[] = { REGISTER_SETOP_SCALAR(ArrayAtR2V) + REGISTER_SETOP_SMALL_INT(ArrayAtR2V) REGISTER_SETOP_NUMERIC_VEC(ArrayAtR2V) FUSION_ENTRY("ArrayAt","",FusionPoint_Set_ArrayAt_StringPtr) REGISTER_SETOP_SCALAR(ArrayAtR2VU) + REGISTER_SETOP_SMALL_INT(ArrayAtR2VU) REGISTER_SETOP_NUMERIC_VEC(ArrayAtR2VU) FUSION_ENTRY("ArrayAtU","",FusionPoint_Set_ArrayAtU_StringPtr) REGISTER_SETOP_SCALAR(ArrayAtR2V_I64) @@ -437,5 +434,7 @@ namespace das { FUSION_TABLE(fusionTable_at_array, fusionEntries_at_array) } +#undef FUSION_OP2_SET_GLOBAL_MNH + #endif diff --git a/src/simulate/simulate_fusion_op2_impl.h b/src/simulate/simulate_fusion_op2_impl.h index 9a3be2e7c9..7e348a1f5a 100644 --- a/src/simulate/simulate_fusion_op2_impl.h +++ b/src/simulate/simulate_fusion_op2_impl.h @@ -55,6 +55,7 @@ virtual SimNode * match(const SimNodeInfoLookup & info, SimNode *, SimNode * node_l, SimNode * node_r, Context * context, bool & anyLeft, bool & anyRight) const override { \ auto & ccode = *(context->code); \ /* match op2 */ if ( !node_l || !node_r ) { return nullptr; } \ + MATCH_OP2(OPNAME,"GetGlobalR2V","GetLocalR2V",Global,Local) \ MATCH_OP2(OPNAME,"GetThisBlockArgumentR2V","GetThisBlockArgument",ThisBlockArgumentRef,ThisBlockArgument) \ MATCH_OP2(OPNAME,"GetThisBlockArgument","GetThisBlockArgument",ThisBlockArgument,ThisBlockArgument) \ MATCH_OP2(OPNAME,"GetThisBlockArgument","GetArgument",ThisBlockArgument,Argument) \ diff --git a/src/simulate/simulate_fusion_op2_set_impl.h b/src/simulate/simulate_fusion_op2_set_impl.h index 4079f66479..6084f2b6c6 100644 --- a/src/simulate/simulate_fusion_op2_set_impl.h +++ b/src/simulate/simulate_fusion_op2_set_impl.h @@ -9,6 +9,28 @@ return ccode.makeNode(); \ } +#undef IMPLEMENT_OP2_SET_NODES_MNH +#undef MATCH_OP2_SET_MNH +// src/simulate/ARCHITECTURE.md#global-by-hash-source +#ifdef FUSION_OP2_SET_GLOBAL_MNH +#if DAS_FUSION>=2 +#define IMPLEMENT_OP2_SET_NODES_MNH(INLINE,OPNAME,TYPE,CTYPE) \ + IMPLEMENT_OP2_SET_NODE(INLINE,OPNAME,TYPE,CTYPE,GlobalMnh,Local); \ + IMPLEMENT_OP2_SET_NODE_ANY(INLINE,OPNAME,TYPE,CTYPE,GlobalMnh); +#define MATCH_OP2_SET_MNH(OPNAME) \ + MATCH_OP2_SET(OPNAME,"GetGlobalMnh","GetLocalR2V",GlobalMnh,Local) \ + MATCH_OP2_SET_ANY(OPNAME,"GetGlobalMnh",GlobalMnh) +#else +#define IMPLEMENT_OP2_SET_NODES_MNH(INLINE,OPNAME,TYPE,CTYPE) \ + IMPLEMENT_OP2_SET_NODE_ANY(INLINE,OPNAME,TYPE,CTYPE,GlobalMnh); +#define MATCH_OP2_SET_MNH(OPNAME) \ + MATCH_OP2_SET_ANY(OPNAME,"GetGlobalMnh",GlobalMnh) +#endif +#else +#define IMPLEMENT_OP2_SET_NODES_MNH(INLINE,OPNAME,TYPE,CTYPE) +#define MATCH_OP2_SET_MNH(OPNAME) +#endif + #if DAS_FUSION>=2 // a SetOPNAME b @@ -37,6 +59,7 @@ IMPLEMENT_OP2_SET_NODE_ANY(INLINE,OPNAME,TYPE,CTYPE,ArgumentRef); \ IMPLEMENT_OP2_SET_NODE_ANY(INLINE,OPNAME,TYPE,CTYPE,ArgumentRefOff); \ IMPLEMENT_OP2_SET_NODE_ANY(INLINE,OPNAME,TYPE,CTYPE,ThisBlockArgumentRef); \ + IMPLEMENT_OP2_SET_NODES_MNH(INLINE,OPNAME,TYPE,CTYPE) \ virtual SimNode * match(const SimNodeInfoLookup & info, SimNode *, SimNode * node_l, SimNode * node_r, Context * context, bool & anyLeft, bool & anyRight) const override { \ auto & ccode = *(context->code); \ /* match op2 */ if ( !node_l || !node_r ) { return nullptr; } \ @@ -63,6 +86,7 @@ MATCH_OP2_SET_ANY(OPNAME,"GetArgument",ArgumentRef) \ MATCH_OP2_SET_ANY(OPNAME,"GetArgumentRefOff",ArgumentRefOff) \ MATCH_OP2_SET_ANY(OPNAME,"GetThisBlockArgument",ThisBlockArgumentRef) \ + MATCH_OP2_SET_MNH(OPNAME) \ return nullptr; \ } \ virtual void set(SimNode_Op2Fusion * result, SimNode * node) const override { \ @@ -86,6 +110,7 @@ IMPLEMENT_OP2_SET_NODE_ANY(INLINE,OPNAME,TYPE,CTYPE,ArgumentRef); \ IMPLEMENT_OP2_SET_NODE_ANY(INLINE,OPNAME,TYPE,CTYPE,ArgumentRefOff); \ IMPLEMENT_OP2_SET_NODE_ANY(INLINE,OPNAME,TYPE,CTYPE,ThisBlockArgumentRef); \ + IMPLEMENT_OP2_SET_NODES_MNH(INLINE,OPNAME,TYPE,CTYPE) \ virtual SimNode * match(const SimNodeInfoLookup & info, SimNode *, SimNode * node_l, SimNode * node_r, Context * context, bool & anyLeft, bool & anyRight) const override { \ auto & ccode = *(context->code); \ /* match op2 */ if ( !node_l || !node_r ) { return nullptr; } \ @@ -96,6 +121,7 @@ MATCH_OP2_SET_ANY(OPNAME,"GetArgument",ArgumentRef) \ MATCH_OP2_SET_ANY(OPNAME,"GetArgumentRefOff",ArgumentRefOff) \ MATCH_OP2_SET_ANY(OPNAME,"GetThisBlockArgument",ThisBlockArgumentRef) \ + MATCH_OP2_SET_MNH(OPNAME) \ return nullptr; \ } \ virtual void set(SimNode_Op2Fusion * result, SimNode * node) const override { \ diff --git a/src/simulate/simulate_fusion_op2_set_perm.h b/src/simulate/simulate_fusion_op2_set_perm.h index cc7a264c27..eb2d3cdd7c 100644 --- a/src/simulate/simulate_fusion_op2_set_perm.h +++ b/src/simulate/simulate_fusion_op2_set_perm.h @@ -33,6 +33,18 @@ IMPLEMENT_ANY_SETOP(__forceinline,OPNAME,Bool,bool,bool); \ IMPLEMENT_SETOP_NUMERIC(OPNAME); +#define IMPLEMENT_SETOP_SMALL(OPNAME,CTYPE) \ + IMPLEMENT_ANY_SETOP(__forceinline,OPNAME,Int,CTYPE,CTYPE); + +#define IMPLEMENT_SETOP_SMALL_INT(OPNAME) \ + DAS_FUSION_SMALL_INTS(IMPLEMENT_SETOP_SMALL,OPNAME) + +#define REGISTER_SETOP_SMALL(OPNAME,CTYPE) \ + REGISTER_SETOP(OPNAME,Int,CTYPE) + +#define REGISTER_SETOP_SMALL_INT(OPNAME) \ + DAS_FUSION_SMALL_INTS(REGISTER_SETOP_SMALL,OPNAME) + #define IMPLEMENT_SETOP_WORKHORSE(OPNAME) \ IMPLEMENT_ANY_SETOP(__forceinline,OPNAME,Ptr,StringPtr,StringPtr); \ IMPLEMENT_ANY_SETOP(__forceinline,OPNAME,Ptr,VoidPtr,StringPtr); \ diff --git a/src/simulate/simulate_fusion_ptr_at.cpp b/src/simulate/simulate_fusion_ptr_at.cpp new file mode 100644 index 0000000000..afb49d4f7b --- /dev/null +++ b/src/simulate/simulate_fusion_ptr_at.cpp @@ -0,0 +1,378 @@ +#include "daScript/misc/platform.h" + +#ifdef _MSC_VER +#pragma warning(disable:4505) +#endif + +#include "simulate_fusion.h" + +#if DAS_FUSION>=2 + +#include "daScript/simulate/sim_policy.h" +#include "daScript/simulate/simulate_visit_op.h" + +namespace das { + + // src/simulate/ARCHITECTURE.md#pointer-index-family + struct SimNode_PtrAtFusion : SimNode_Op2Index { + SimNode_PtrAtFusion() : SimNode_Op2Index("") {} + }; + + struct SimNode_PtrSetFusion : SimNode_PtrAtFusion { + virtual SimNode * visit ( SimVisitor & vis ) override { + V_BEGIN(); + string name = op; + name += getSimSourceName(l.type); + if ( isIndexed ) name += getSimSourceName(r.type); + vis.op(name.c_str(), getTypeBaseSize(baseType), das_to_string(baseType)); + l.visit(vis); + if ( isIndexed ) { + r.visit(vis); + V_ARG(stride); + V_ARG(offset); + } + V_SUB(value); + V_END(); + } + SimNode * value = nullptr; + bool isIndexed = false; + }; + +#define IMPLEMENT_PTR_BASE(BASE,COMPUTE) \ + struct PtrBase##BASE { \ + static constexpr bool fusesAnyIndex = true; \ + static __forceinline char * get ( const SimSource & base, Context & context ) { \ + return *(char **) base.compute##COMPUTE(context); \ + } \ + }; + + IMPLEMENT_PTR_BASE(Loc,Local) + IMPLEMENT_PTR_BASE(Arg,Argument) + IMPLEMENT_PTR_BASE(Glob,Global) + IMPLEMENT_PTR_BASE(GlobMnh,GlobalMnh) + + struct PtrBaseAny { + static constexpr bool fusesAnyIndex = false; + static __forceinline char * get ( const SimSource & base, Context & context ) { + return base.computeAnyPtr(context); + } + }; + +#define IMPLEMENT_PTR_INDEX(INDEX,COMPUTE) \ + template \ + struct PtrIndex##INDEX { \ + static __forceinline IDXT get ( const SimSource & index, Context & context ) { \ + return *(IDXT *) index.compute##COMPUTE(context); \ + } \ + }; + + IMPLEMENT_PTR_INDEX(Loc,Local) + IMPLEMENT_PTR_INDEX(Arg,Argument) + IMPLEMENT_PTR_INDEX(Const,Const) + + template + struct PtrIndexAny { + static __forceinline IDXT get ( const SimSource & index, Context & context ) { + return evalNode::eval(context, index.subexpr); + } + }; + + template + struct SimNode_PtrAtAddr : NODE { + __forceinline char * address ( Context & context ) { + char * pValue = BASE::get(this->l, context); + auto idx = INDEX::get(this->r, context); + typedef decltype(idx) IDXT; + return pValue + (idx*IDXT(this->stride) + IDXT(this->offset)); + } + }; + + template + struct SimNode_PtrDerefAddr : NODE { + __forceinline char * address ( Context & context ) { + char * ptr = BASE::get(this->l, context); + if ( !ptr ) { + context.throw_error_at(this->debugInfo,"dereferencing null pointer%s", this->errorMessage); + } + return ptr; + } + }; + +#define PTR_AT_WORKHORSES(STAMP) \ + STAMP(Bool,bool) \ + STAMP(Int,int32_t) \ + STAMP(UInt,uint32_t) \ + STAMP(Int64,int64_t) \ + STAMP(UInt64,uint64_t) \ + STAMP(Float,float) \ + STAMP(Double,double) + +#define PTR_AT_VECTORS(STAMP) \ + STAMP(int2) \ + STAMP(uint2) \ + STAMP(int3) \ + STAMP(uint3) \ + STAMP(int4) \ + STAMP(uint4) \ + STAMP(float2) \ + STAMP(float3) \ + STAMP(float4) + + struct PtrAtRef {}; + + template + struct SimNode_PtrAtLoad; + +#define IMPLEMENT_PTR_AT_LOAD(CTYPE,TAIL) \ + template \ + struct SimNode_PtrAtLoad : ADDR { \ + __forceinline CTYPE compute ( Context & context ) { \ + DAS_PROFILE_NODE \ + return *((CTYPE *) this->address(context)); \ + } \ + TAIL; \ + }; + +#define IMPLEMENT_PTR_AT_LOAD_WORKHORSE(TYPE,CTYPE) \ + IMPLEMENT_PTR_AT_LOAD(CTYPE,DAS_NODE(TYPE,CTYPE)) + +#define IMPLEMENT_PTR_AT_LOAD_SMALL_INT(UNUSED,CTYPE) \ + IMPLEMENT_PTR_AT_LOAD(CTYPE,DAS_FUSION_WIDE_NODE) + +#define IMPLEMENT_PTR_AT_LOAD_VECTOR(CTYPE) \ + template \ + struct SimNode_PtrAtLoad : ADDR { \ + DAS_EVAL_ABI virtual vec4f eval ( Context & context ) override { \ + DAS_PROFILE_NODE \ + vec4f __r; \ + DAS_LDU_WORKHORSE(__r, this->address(context), CTYPE); \ + return __r; \ + } \ + }; + + PTR_AT_WORKHORSES(IMPLEMENT_PTR_AT_LOAD_WORKHORSE) + DAS_FUSION_SMALL_INTS(IMPLEMENT_PTR_AT_LOAD_SMALL_INT,) + PTR_AT_VECTORS(IMPLEMENT_PTR_AT_LOAD_VECTOR) + + template + struct SimNode_PtrAtLoad : ADDR { + __forceinline char * compute ( Context & context ) { + DAS_PROFILE_NODE + return this->address(context); + } + DAS_PTR_NODE; + }; + + template + struct SimNode_PtrSet : ADDR { + DAS_EVAL_ABI virtual vec4f eval ( Context & context ) override { + DAS_PROFILE_NODE + auto R = EvalTT::eval(context, this->value); + CTYPE * pl = (CTYPE *) this->address(context); + *pl = R; + return v_zero(); + } + }; + + template + struct PtrAtLoadOf { + template using node = SimNode_PtrAtLoad; + }; + + template + struct PtrSetOf { + template using node = SimNode_PtrSet; + }; + + template struct PtrAtOps; + + template <> struct PtrAtOps { + static constexpr const char * at = "PtrAt"; + static constexpr const char * set = "PtrAtSet"; + }; + + template <> struct PtrAtOps { + static constexpr const char * at = "PtrAt_U32"; + static constexpr const char * set = "PtrAtSet_U32"; + }; + + template + struct PtrAtLoadType { + static const char * op() { return PtrAtR2VName::name(); } + static Type type() { return Type(ToBasicType::type); } + }; + + template + struct PtrAtLoadType { + static const char * op() { return PtrAtOps::at; } + static Type type() { return Type::none; } + }; + + static SimSource ptrBaseSource ( const SimNodeInfoLookup & info, SimNode * node ) { + SimSource source; + if ( FusionPoint::is(info,node,"GetLocalR2V") || FusionPoint::is(info,node,"GetArgument") + || FusionPoint::is(info,node,"GetGlobalR2V") || FusionPoint::is(info,node,"GetGlobalMnhR2V") ) { + source = static_cast(node)->subexpr; + } else { + source.setSimNode(node); + } + return source; + } + + static SimSourceType ptrIndexSourceType ( const SimNodeInfoLookup & info, SimNode * node ) { + if ( FusionPoint::is(info,node,"GetLocalR2V") || FusionPoint::is(info,node,"GetArgument") + || FusionPoint::is(info,node,"ConstValue") ) { + return static_cast(node)->subexpr.type; + } + return SimSourceType::sSimNode; + } + + template + NODE * makePtrAtNode ( Context * context ) { + return context->code->makeNode>>(); + } + + template + NODE * makePtrAtForIndex ( Context * context, SimSourceType index ) { + switch ( index ) { + case SimSourceType::sSimNode: + if constexpr ( BASE::fusesAnyIndex ) { + return makePtrAtNode>(context); + } else { + return nullptr; + } + case SimSourceType::sLocal: return makePtrAtNode>(context); + case SimSourceType::sArgument: return makePtrAtNode>(context); + case SimSourceType::sConstValue: return makePtrAtNode>(context); + default: return nullptr; + } + } + + template + NODE * makePtrAtForBase ( Context * context, SimSourceType base, SimSourceType index ) { + switch ( base ) { + case SimSourceType::sLocal: return makePtrAtForIndex(context, index); + case SimSourceType::sArgument: return makePtrAtForIndex(context, index); + case SimSourceType::sGlobal: return makePtrAtForIndex(context, index); + case SimSourceType::sGlobalMnh: return makePtrAtForIndex(context, index); + case SimSourceType::sSimNode: return makePtrAtForIndex(context, index); + default: return nullptr; + } + } + + template + NODE * makePtrDerefNode ( Context * context, SimSourceType base ) { + auto & code = *(context->code); + switch ( base ) { + case SimSourceType::sLocal: + return code.makeNode>>(); + case SimSourceType::sArgument: + return code.makeNode>>(); + default: + return nullptr; + } + } + + template + struct FusionPoint_PtrAt final : FusionPointOp2 { + virtual SimNode * match ( const SimNodeInfoLookup & info, SimNode *, SimNode * node_l, SimNode * node_r, Context * context, bool & anyLeft, bool & anyRight ) const override { + SimSourceType base = ptrBaseSource(info, node_l).type; + SimSourceType index = ptrIndexSourceType(info, node_r); + anyLeft = base==SimSourceType::sSimNode; + anyRight = index==SimSourceType::sSimNode; + return makePtrAtForBase,SimNode_PtrAtFusion,IDXT>(context, base, index); + } + virtual void set ( SimNode_Op2Fusion * result, SimNode * node ) const override { + auto at = static_cast *>(node); + auto fused = static_cast(result); + fused->set(PtrAtLoadType::op(), PtrAtLoadType::type(), node->debugInfo); + fused->stride = at->stride; + fused->offset = at->offset; + } + virtual SimNode * fuse ( const SimNodeInfoLookup & info, SimNode * node, Context * context ) const override { + auto at = static_cast *>(node); + return fuseOp2(info, node, at->value, at->index, context); + } + }; + + template struct IsSmallInt { enum { value = false }; }; + +#define IMPLEMENT_IS_SMALL_INT(UNUSED,CTYPE) \ + template <> struct IsSmallInt { enum { value = true }; }; + + DAS_FUSION_SMALL_INTS(IMPLEMENT_IS_SMALL_INT,) + + template + struct FusionPoint_PtrSet final : FusionPoint { + template + static SimNode_PtrSetFusion * indexed ( SimNode * node, SimNode * left, Context * context ) { + auto at = static_cast(left); + auto result = makePtrAtForBase,SimNode_PtrSetFusion,IDXT>(context, at->l.type, at->r.type); + if ( !result ) return nullptr; + result->set(PtrAtOps::set, Type(ToBasicType::type), node->debugInfo); + result->l = at->l; + result->r = at->r; + result->stride = at->stride; + result->offset = at->offset; + result->isIndexed = true; + return result; + } + static SimNode_PtrSetFusion * dereferenced ( const SimNodeInfoLookup & info, SimNode * left, Context * context ) { + auto ref = static_cast(left); + SimSource base = ptrBaseSource(info, ref->subexpr); + auto result = makePtrDerefNode,SimNode_PtrSetFusion>(context, base.type); + if ( !result ) return nullptr; + result->set("PtrSet", Type(ToBasicType::type), ref->debugInfo); + result->l = base; + result->errorMessage = ref->errorMessage; + return result; + } + static SimNode * storedValue ( const SimNodeInfoLookup & info, SimNode * value ) { + if ( IsSmallInt::value ) { + string narrow = string("Cast_to_") + typeName::name(); + if ( is(info,value,narrow.c_str(),"int") || is(info,value,narrow.c_str(),"uint") ) { + return static_cast(value)->arguments[0]; + } + } + return value; + } + virtual SimNode * fuse ( const SimNodeInfoLookup & info, SimNode * node, Context * context ) const override { + auto set = static_cast *>(node); + SimNode_PtrSetFusion * result = nullptr; + if ( is(info,set->l,"Ptr2Ref") ) { + result = dereferenced(info, set->l, context); + } else if ( isFusedOp2(info,set->l,PtrAtOps::at,"") ) { + result = indexed(node, set->l, context); + } else if ( isFusedOp2(info,set->l,PtrAtOps::at,"") ) { + result = indexed(node, set->l, context); + } + if ( !result ) return node; + result->value = storedValue(info, set->r); + return result; + } + }; + +#define REGISTER_PTR_AT(CTYPE) \ + { PtrAtR2VName::name(), typeName::name(), &fusionPoint> }, \ + { PtrAtR2VName::name(), typeName::name(), &fusionPoint> }, + +#define REGISTER_PTR_AT_TYPED(UNUSED,CTYPE) \ + REGISTER_PTR_AT(CTYPE) + +#define REGISTER_PTR_SET(UNUSED,CTYPE) \ + { "Set", typeName::name(), &fusionPoint> }, + + static constexpr FusionEntry fusionEntries_ptr_at[] = { + PTR_AT_WORKHORSES(REGISTER_PTR_AT_TYPED) + DAS_FUSION_SMALL_INTS(REGISTER_PTR_AT_TYPED,) + PTR_AT_VECTORS(REGISTER_PTR_AT) + { PtrAtOps::at, typeName::name(), &fusionPoint> }, + { PtrAtOps::at, typeName::name(), &fusionPoint> }, + PTR_AT_WORKHORSES(REGISTER_PTR_SET) + DAS_FUSION_SMALL_INTS(REGISTER_PTR_SET,) + }; + + FUSION_TABLE(fusionTable_ptr_at, fusionEntries_ptr_at) +} + +#endif diff --git a/src/simulate/simulate_fusion_widen.cpp b/src/simulate/simulate_fusion_widen.cpp new file mode 100644 index 0000000000..6f357d48ee --- /dev/null +++ b/src/simulate/simulate_fusion_widen.cpp @@ -0,0 +1,96 @@ +#include "daScript/misc/platform.h" + +#ifdef _MSC_VER +#pragma warning(disable:4505) +#endif + +#include "simulate_fusion.h" + +#if DAS_FUSION + +#include "daScript/simulate/sim_policy.h" +#include "daScript/simulate/simulate_visit_op.h" + +namespace das { + + struct SimNode_WideRead : SimNode_Op1Fusion { + SimNode_WideRead() : SimNode_Op1Fusion("") {} + virtual SimNode * visit ( SimVisitor & vis ) override { + V_BEGIN(); + vis.op(op, getTypeBaseSize(baseType), das_to_string(baseType)); + subexpr.visit(vis); + V_END(); + } + }; + +#define IMPLEMENT_WIDE_READ_SOURCE(SOURCE) \ + struct WideRead##SOURCE { \ + static constexpr const char * source = "Get" #SOURCE "R2V"; \ + static constexpr const char * op = "Get" #SOURCE "R2VToInt"; \ + static __forceinline char * get ( const SimSource & subexpr, Context & context ) { \ + return subexpr.compute##SOURCE(context); \ + } \ + }; + + IMPLEMENT_WIDE_READ_SOURCE(Local) + IMPLEMENT_WIDE_READ_SOURCE(LocalRefOff) + IMPLEMENT_WIDE_READ_SOURCE(ArgumentRefOff) + + template + struct SimNode_WideReadT : SimNode_WideRead { + __forceinline CTYPE compute ( Context & context ) { + DAS_PROFILE_NODE + return *((CTYPE *) SOURCE::get(subexpr, context)); + } + DAS_FUSION_WIDE_NODE; + }; + + // src/simulate/ARCHITECTURE.md#fused-small-int-reads + static const char * const g_fusedSmallIntReads[] = { + "AtR2V", "AtR2VU", "ArrayAtR2V", "ArrayAtR2VU", + PtrAtR2VName::name(), PtrAtR2VName::name() + }; + + template + struct FusionPoint_Widen final : FusionPointOp1 { + template + static SimNode * wideRead ( Context * context ) { + auto result = context->code->makeNode>(); + result->op = SOURCE::op; + return result; + } + virtual SimNode * match ( const SimNodeInfoLookup & info, SimNode *, SimNode * arg, Context * context ) const override { + const char * from = typeName::name(); + if ( is(info,arg,WideReadLocal::source,from) ) { + return wideRead(context); + } else if ( is(info,arg,WideReadLocalRefOff::source,from) ) { + return wideRead(context); + } else if ( is(info,arg,WideReadArgumentRefOff::source,from) ) { + return wideRead(context); + } + return nullptr; + } + virtual void set ( SimNode_Op1Fusion * result, SimNode * node ) const override { + result->set(result->op, Type(ToBasicType::type), node->debugInfo); + } + virtual SimNode * fuse ( const SimNodeInfoLookup & info, SimNode * node, Context * context ) const override { + auto arg = static_cast(node)->arguments[0]; + for ( auto op : g_fusedSmallIntReads ) { + if ( isFusedOp2(info,arg,op,typeName::name()) ) return arg; + } + return fuseOp1(info, node, arg, context); + } + }; + +#define REGISTER_WIDEN(CAST,CTYPE) \ + FUSION_ENTRY(CAST,typeName::name(),FusionPoint_Widen) + + static constexpr FusionEntry fusionEntries_widen[] = { + DAS_FUSION_SMALL_INTS(REGISTER_WIDEN,"Cast_to_int") + DAS_FUSION_SMALL_INTS(REGISTER_WIDEN,"Cast_to_uint") + }; + + FUSION_TABLE(fusionTable_widen, fusionEntries_widen) +} + +#endif diff --git a/src/simulate/simulate_visit.cpp b/src/simulate/simulate_visit.cpp index 0a0f0f4de7..dc835a12fb 100644 --- a/src/simulate/simulate_visit.cpp +++ b/src/simulate/simulate_visit.cpp @@ -26,6 +26,7 @@ namespace das { V_ARG(offset); break; case SimSourceType::sGlobal: + case SimSourceType::sGlobalMnh: V_ARG(mangledNameHash); break; case SimSourceType::sShared: diff --git a/tests/fixed_array/test_indexing.das b/tests/fixed_array/test_indexing.das index 3064e62a26..c2bd58ba6c 100644 --- a/tests/fixed_array/test_indexing.das +++ b/tests/fixed_array/test_indexing.das @@ -103,3 +103,103 @@ def test_subarray(t : T?) { t |> equal(sum, 3) } } + +var g_small_int_table = fixed_array(3u8, 2u8, 1u8, 0u8) +var g_double_index_lut = fixed_array(10, 20, 30, 40) + +def small_int_fixed_sum_argument(bytes : uint8[4]) { + var sum = 0 + for (i in range(4)) { + sum += int(bytes[i]) + } + return sum +} + +[test(description = "a fixed array of small ints read by index widens uint8 and uint16 with zeros and int8 and int16 with the sign, for a local, an argument and a global array, and an index out of range raises")] +def test_small_int_fixed_index_read(t : T?) { + let bytes = fixed_array(200u8, 7u8, 255u8, 1u8) + let chars = fixed_array(int8(-5), int8(100), int8(-128), int8(1)) + let words = fixed_array(uint16(60000), uint16(9), uint16(65535), uint16(1)) + let shorts = fixed_array(int16(-300), int16(300), int16(-32768), int16(1)) + var int_sum = 0 + var uint_sum = 0u + for (i in range(4)) { + int_sum += int(bytes[i]) + int(chars[i]) + int(words[i]) + int(shorts[i]) + uint_sum += uint(bytes[3 - i]) + uint(words[3 - i]) + } + t |> equal(int_sum, 93209) + t |> equal(uint_sum, 126008u) + var index = 0 + t |> equal(uint(chars[index]), 0xfffffffb) + t |> equal(uint(shorts[index]), 0xfffffed4) + t |> equal(bytes[index], 200u8) + t |> equal(int(bytes[1]), 7) + t |> equal(small_int_fixed_sum_argument(bytes), 463) + int_sum = 0 + for (i in range(4)) { + int_sum += int(g_small_int_table[i]) + int(g_small_int_table[3 - i]) + } + t |> equal(int_sum, 12) + index = 4 + var raised = false + try { + int_sum = int(bytes[index]) + } recover { + raised = true + } + t |> success(raised, "an index past the end of a uint8 fixed array raises") + index = -1 + raised = false + try { + int_sum = int(chars[index]) + } recover { + raised = true + } + t |> success(raised, "a negative index into an int8 fixed array raises") +} + +[test(description = "a table indexed by an element of another table gives the same element whether the inner table holds ints or small ints, for local and global tables")] +def test_double_index_fixed(t : T?) { + let lut = fixed_array(10, 20, 30, 40) + let remap = fixed_array(3, 2, 1, 0) + var bytes = fixed_array(3u8, 2u8, 1u8, 0u8) + var through_ints = 0 + var through_bytes = 0 + var through_globals = 0 + for (i in range(4)) { + through_ints += lut[remap[i]] * (i + 1) + through_bytes += lut[int(bytes[i])] * (i + 1) + through_globals += g_double_index_lut[int(g_small_int_table[i])] * (i + 1) + } + t |> equal(through_ints, 200) + t |> equal(through_bytes, 200) + t |> equal(through_globals, 200) + var folded = 0 + for (c in bytes) { + folded += lut[remap[int(c)]] + } + t |> equal(folded, 100) +} + +[test(description = "a fixed array of small ints read through an expression index keeps the sign and still raises out of range")] +def test_small_int_fixed_expression_index(t : T?) { + let chars = fixed_array(int8(-5), int8(100), int8(-128)) + let shorts = fixed_array(int16(-300), int16(300), int16(-32768)) + var int_sum = 0 + var uint_sum = 0u + for (i in range(3)) { + int_sum += int(chars[2 - i]) * (i + 1) + int(shorts[2 - i]) + uint_sum += uint(chars[2 - i]) + } + t |> equal(int_sum, -32711) + t |> equal(uint_sum, 0xffffffdf) + var raised = false + for (i in range(3, 4)) { + try { + int_sum = int(chars[i * 2 - 3]) + } recover { + raised = true + } + } + t |> success(raised, "an expression index past the end of an int8 fixed array raises") +} diff --git a/tests/language/arrays.das b/tests/language/arrays.das index 11ea904201..6036622184 100644 --- a/tests/language/arrays.das +++ b/tests/language/arrays.das @@ -1249,3 +1249,74 @@ def test_scoped_array_of_arrays_unlocked_after_element_read(t : T?) { t |> equal(picked[0], "x") t |> equal(picked[1], "9") } + +var g_small_int_bytes : array + +def small_int_sum_argument(bytes : array; count : int) { + var sum = 0 + for (i in range(count)) { + sum += int(bytes[i]) + } + return sum +} + +[test(description = "an array of small ints read by index widens uint8 and uint16 with zeros and int8 and int16 with the sign, for a local, an argument and a global array, and an index past the end raises")] +def test_small_int_array_index_read(t : T?) { + var bytes <- [200u8, 7u8, 255u8] + let chars <- [int8(-5), int8(100), int8(-128)] + let words <- [uint16(60000), uint16(9), uint16(65535)] + let shorts <- [int16(-300), int16(300), int16(-32768)] + var int_sum = 0 + var uint_sum = 0u + for (i in range(3)) { + int_sum += int(bytes[i]) + int(chars[i]) + int(words[i]) + int(shorts[i]) + uint_sum += uint(bytes[2 - i]) + uint(words[2 - i]) + } + t |> equal(int_sum, 93205) + t |> equal(uint_sum, 126006u) + var index = 0 + t |> equal(uint(chars[index]), 0xfffffffb) + t |> equal(uint(shorts[index]), 0xfffffed4) + t |> equal(bytes[index], 200u8) + t |> equal(shorts[index], int16(-300)) + t |> equal(int(bytes[1]), 7) + t |> equal(small_int_sum_argument(bytes, 3), 462) + g_small_int_bytes := bytes + int_sum = 0 + for (i in range(3)) { + int_sum += int(g_small_int_bytes[i]) + int(g_small_int_bytes[2 - i]) + } + t |> equal(int_sum, 924) + delete g_small_int_bytes + index = 3 + var raised = false + try { + int_sum = int(bytes[index]) + } recover { + raised = true + } + t |> success(raised, "an index past the end of a uint8 array raises") +} + +[test(description = "an array of small ints read through an expression index keeps the sign and still raises past the end")] +def test_small_int_array_expression_index(t : T?) { + let chars <- [int8(-5), int8(100), int8(-128)] + let shorts <- [int16(-300), int16(300), int16(-32768)] + var int_sum = 0 + var uint_sum = 0u + for (i in range(3)) { + int_sum += int(chars[2 - i]) * (i + 1) + int(shorts[2 - i]) + uint_sum += uint(chars[2 - i]) + } + t |> equal(int_sum, -32711) + t |> equal(uint_sum, 0xffffffdf) + var raised = false + for (i in range(3, 4)) { + try { + int_sum = int(chars[i * 2 - 3]) + } recover { + raised = true + } + } + t |> success(raised, "an expression index past the end of an int8 array raises") +} diff --git a/tests/language/bound_check_elision_results.das b/tests/language/bound_check_elision_results.das index 60546494ac..05dbfbfc1d 100644 --- a/tests/language/bound_check_elision_results.das +++ b/tests/language/bound_check_elision_results.das @@ -94,3 +94,28 @@ def test_unproven_dim_and_vector_index_checked(t : T?) { } t |> success(caught4) } + +[export] def elided_small_int_sum : int { + var bytes : array + bytes |> resize(16) + for (i in range(length(bytes))) { + bytes[i] = uint8(i * 3) + } + var chars : int8[8] + for (i in range(8)) { + chars[i] = int8(i - 4) + } + var s = 0 + for (i in range(length(bytes))) { // nolint:PERF018 + s += int(bytes[i]) + } + for (i in urange(8u)) { + s += int(chars[i]) + } + return s +} + +[test(description = "a small int element read through an index proven in range has the right value and sign")] +def test_elided_small_int_reads(t : T?) { + t |> equal(elided_small_int_sum(), 356) +} diff --git a/tests/language/globals.das b/tests/language/globals.das index d604eba984..e426772cdb 100644 --- a/tests/language/globals.das +++ b/tests/language/globals.das @@ -139,3 +139,50 @@ def test_global_array_of_property_literals(t : T?) { t |> equal(g_prop_bits[1].low, 2u) t |> equal(g_prop_bits[2].low, 5u) } + +var g_indexed_values : array +var g_indexed_table : int[8] +var g_indexed_points : float3[4] +var g_indexed_cursor : int? + +[test(description = "a global array and a global fixed array read and written by index give the element for a local, an expression and a constant index, and a global pointer indexes the same elements")] +def test_global_indexing(t : T?) { + delete g_indexed_values + g_indexed_values <- [for (i in range(8)); i * 5] + var forward = 0 + var backward = 0 + for (i in range(8)) { + g_indexed_table[i] = i * 7 + g_indexed_table[7 - i]++ + } + for (i in range(8)) { + forward += g_indexed_values[i] + g_indexed_table[i] + backward += (g_indexed_values[7 - i] + g_indexed_table[7 - i]) * (i + 1) + } + t |> equal(forward, 340) + t |> equal(backward, 1034) + t |> equal(g_indexed_values[3], 15) + t |> equal(g_indexed_table[3], 22) + for (i in range(4)) { + g_indexed_points[i] = float3(float(i), 1.0, 2.0) + } + var total = float3(0.0) + for (i in range(4)) { + total += g_indexed_points[i] + } + t |> equal(total, float3(6.0, 4.0, 8.0)) + for (i in range(8)) { + g_indexed_values[i] = g_indexed_table[7 - i] + i + } + t |> equal(g_indexed_values[0], 49) + g_indexed_cursor = unsafe(addr(g_indexed_table[0])) + var through_pointer = 0 + for (i in range(8)) { + through_pointer += unsafe(g_indexed_cursor[i]) + } + t |> equal(through_pointer, 200) + unsafe(g_indexed_cursor[2]) = through_pointer + 1 + t |> equal(g_indexed_table[2], 201) + g_indexed_cursor = null + delete g_indexed_values +} diff --git a/tests/language/pointers.das b/tests/language/pointers.das index 2d06bcc2b9..683ac179e9 100644 --- a/tests/language/pointers.das +++ b/tests/language/pointers.das @@ -1044,3 +1044,444 @@ def test_typed_pointer_binds_to_void_ref(t : T?) { t |> equal(*q, 7) t |> success(is_set_through_const_void_ref(q)) } + +struct IndexedHolder { + data : int? +} + +var g_indexed_ints : int? + + +def indexed_sum_argument(p : int?; count : int) { + var sum = 0 + for (i in range(count)) { + sum += unsafe(p[i]) + } + return sum +} + +def indexed_pick_argument(p : int?; index : int) : int => unsafe(p[index]) + +def indexed_third_argument(p : int?) : int => unsafe(p[2]) + +def indexed_from_end_argument(p : int?; index : int) : int => unsafe(p[15 - index]) + +def indexed_pick_local(source : int?; index : int) { + let p = source + return unsafe(p[index]) +} + +def indexed_pick_global(index : int) : int => unsafe(g_indexed_ints[index]) + +def indexed_pick_field(holder : IndexedHolder?; index : int) : int => unsafe(holder.data[index]) + +def indexed_bump(var value : int&) { + value += 1000 +} + +[test(description = "an indexed read through a local pointer gives the element for a local, an expression, a constant, an argument and a negative index")] +def test_indexed_read_local_pointer(t : T?) { + var arr <- [for (i in range(16)); i * 3] + let p = unsafe(addr(arr[0])) + var forward = 0 + var backward = 0 + for (i in range(16)) { + forward += unsafe(p[i]) + backward += unsafe(p[15 - i]) * (i + 1) + } + t |> equal(forward, 360) + t |> equal(backward, 2040) + t |> equal(unsafe(p[5]), 15) + t |> equal(indexed_pick_local(p, 7), 21) + let middle = unsafe(addr(arr[8])) + let back = -3 + t |> equal(unsafe(middle[-3]), 15) + t |> equal(unsafe(middle[back]), 15) +} + +[test(description = "an indexed read through a pointer argument gives the element for a local, an expression, a constant and an argument index")] +def test_indexed_read_argument_pointer(t : T?) { + var arr <- [for (i in range(16)); i * 3] + let p = unsafe(addr(arr[0])) + t |> equal(indexed_sum_argument(p, 16), 360) + t |> equal(indexed_from_end_argument(p, 1), 42) + t |> equal(indexed_third_argument(p), 6) + t |> equal(indexed_pick_argument(p, 9), 27) +} + +[test(description = "an indexed read through a global pointer gives the element for a local, an expression, a constant and an argument index")] +def test_indexed_read_global_pointer(t : T?) { + var arr <- [for (i in range(16)); i * 3] + g_indexed_ints = unsafe(addr(arr[0])) + var forward = 0 + var backward = 0 + for (i in range(16)) { + forward += unsafe(g_indexed_ints[i]) + backward += unsafe(g_indexed_ints[15 - i]) * (i + 1) + } + t |> equal(forward, 360) + t |> equal(backward, 2040) + t |> equal(unsafe(g_indexed_ints[4]), 12) + t |> equal(indexed_pick_global(11), 33) + g_indexed_ints = null +} + +[test(description = "an indexed read through a pointer taken from a field gives the element for a local, a constant and an argument index")] +def test_indexed_read_field_pointer(t : T?) { + var arr <- [for (i in range(16)); i * 3] + var holder = new IndexedHolder(data = unsafe(addr(arr[0]))) + var forward = 0 + for (i in range(16)) { + forward += unsafe(holder.data[i]) + } + t |> equal(forward, 360) + t |> equal(unsafe(holder.data[6]), 18) + t |> equal(indexed_pick_field(holder, 10), 30) + unsafe { + delete holder + } +} + +[test(description = "an indexed read through a pointer with a uint index gives the element for a local, a constant and an expression index")] +def test_indexed_read_uint_index(t : T?) { + var arr <- [for (i in range(16)); i * 3] + let p = unsafe(addr(arr[0])) + var forward = 0 + var backward = 0 + for (i in urange(16u)) { + forward += unsafe(p[i]) + backward += unsafe(p[15u - i]) + } + t |> equal(forward, 360) + t |> equal(backward, 360) + t |> equal(unsafe(p[5u]), 15) +} + +[test(description = "an indexed read through a pointer widens uint8 and uint16 elements with zeros and int8 and int16 elements with the sign, into both int and uint")] +def test_indexed_read_small_int_elements(t : T?) { + var bytes <- [200u8, 7u8] + var chars <- [int8(-5), int8(100)] + var words <- [uint16(60000), uint16(9)] + var shorts <- [int16(-300), int16(300)] + let pb = unsafe(addr(bytes[0])) + let pc = unsafe(addr(chars[0])) + let pw = unsafe(addr(words[0])) + let ps = unsafe(addr(shorts[0])) + var index = 0 + t |> equal(int(unsafe(pb[index])), 200) + t |> equal(uint(unsafe(pb[index])), 200u) + t |> equal(int(unsafe(pc[index])), -5) + t |> equal(uint(unsafe(pc[index])), 0xfffffffb) + t |> equal(int(unsafe(pw[index])), 60000) + t |> equal(uint(unsafe(pw[index])), 60000u) + t |> equal(int(unsafe(ps[index])), -300) + t |> equal(uint(unsafe(ps[index])), 0xfffffed4) + index = 1 + t |> equal(unsafe(pb[index]), 7u8) + t |> equal(unsafe(pc[index]), int8(100)) + t |> equal(unsafe(pw[index]), uint16(9)) + t |> equal(unsafe(ps[index]), int16(300)) +} + +[test(description = "an indexed read through a pointer gives bool, int64, uint64, uint, float, double and vector elements unchanged")] +def test_indexed_read_element_types(t : T?) { + var flags <- [false, true] + var longs <- [1l, -9000000000l] + var ulongs <- [1ul, 18000000000000000000ul] + var uints <- [1u, 4000000000u] + var floats <- [0.5, 1.5] + var doubles <- [0.25lf, 2.25lf] + var points <- [float3(1.0, 2.0, 3.0), float3(4.0, 5.0, 6.0)] + var pairs <- [int2(1, 2), int2(3, 4)] + var quads <- [uint4(1u, 2u, 3u, 4u), uint4(5u, 6u, 7u, 8u)] + let index = 1 + let pflags = unsafe(addr(flags[0])) + let plongs = unsafe(addr(longs[0])) + let pulongs = unsafe(addr(ulongs[0])) + let puints = unsafe(addr(uints[0])) + let pfloats = unsafe(addr(floats[0])) + let pdoubles = unsafe(addr(doubles[0])) + t |> equal(unsafe(pflags[index]), true) + t |> equal(unsafe(plongs[index]), -9000000000l) + t |> equal(unsafe(pulongs[index]), 18000000000000000000ul) + t |> equal(unsafe(puints[index]), 4000000000u) + t |> equal(unsafe(pfloats[index]), 1.5) + t |> equal(unsafe(pdoubles[index]), 2.25lf) + let ppoints = unsafe(addr(points[0])) + let ppairs = unsafe(addr(pairs[0])) + let pquads = unsafe(addr(quads[0])) + t |> equal(unsafe(ppoints[index]), float3(4.0, 5.0, 6.0)) + t |> equal(unsafe(ppairs[index]), int2(3, 4)) + t |> equal(unsafe(pquads[index]), uint4(5u, 6u, 7u, 8u)) +} + +[test(description = "an indexed pointer element passed by reference is the element itself")] +def test_indexed_reference(t : T?) { + var arr <- [for (i in range(4)); i] + var p = unsafe(addr(arr[0])) + let index = 2 + unsafe { + indexed_bump(p[index]) + indexed_bump(p[3]) + } + t |> equal(arr[2], 1002) + t |> equal(arr[3], 1003) + t |> equal(arr[1], 1) +} + +def indexed_fill_argument(var p : int?; count : int) { + for (i in range(count)) { + unsafe(p[i]) = i + 100 + } +} + +def indexed_store_argument(var p : int?; index, value : int) { + unsafe(p[index]) = value + 1 +} + +def deref_store_argument(var p : int?; value : int) { + *p = value + 1 +} + +[test(description = "an indexed store through a pointer writes the element for a local, an argument, a global and a field pointer, with a local, an expression, a constant and an argument index")] +def test_indexed_store_sources(t : T?) { + var arr : array + arr |> resize(8) + var p = unsafe(addr(arr[0])) + for (i in range(8)) { + unsafe(p[i]) = i + 1 + } + t |> equal(arr[7], 8) + for (i in range(8)) { + unsafe(p[7 - i]) = i + 10 + } + t |> equal(arr[0], 17) + let seed = 40 + unsafe(p[3]) = seed + 2 + t |> equal(arr[3], 42) + indexed_fill_argument(p, 8) + t |> equal(arr[5], 105) + indexed_store_argument(p, 6, 76) + t |> equal(arr[6], 77) + g_indexed_ints = p + for (i in range(8)) { + unsafe(g_indexed_ints[i]) = i + 200 + } + g_indexed_ints = null + t |> equal(arr[4], 204) + var holder = new IndexedHolder(data = p) + for (i in range(8)) { + unsafe(holder.data[i]) = i + 300 + } + t |> equal(arr[2], 302) + unsafe { + delete holder + } + for (i in urange(8u)) { + unsafe(p[i]) = int(i) + 400 + } + t |> equal(arr[1], 401) +} + +[test(description = "a store through a dereferenced local or argument pointer writes the pointee, and a null pointer raises instead of writing")] +def test_deref_store(t : T?) { + var arr <- [1, 2] + var p = unsafe(addr(arr[0])) + let seed = 50 + *p = seed + 5 + t |> equal(arr[0], 55) + deref_store_argument(unsafe(addr(arr[1])), 60) + t |> equal(arr[1], 61) + var missing : int? + var raised = false + try { + *missing = seed + 1 + } recover { + raised = true + } + t |> success(raised, "a store through a null local pointer raises") + raised = false + try { + deref_store_argument(missing, 1) + } recover { + raised = true + } + t |> success(raised, "a store through a null pointer argument raises") +} + +[test(description = "a store of an int narrowed to a small int through a pointer keeps only the low bits, indexed and dereferenced")] +def test_pointer_store_narrows(t : T?) { + var bytes : array + var chars : array + var words : array + var shorts : array + bytes |> resize(4) + chars |> resize(4) + words |> resize(4) + shorts |> resize(4) + var pb = unsafe(addr(bytes[0])) + var pc = unsafe(addr(chars[0])) + var pw = unsafe(addr(words[0])) + var ps = unsafe(addr(shorts[0])) + let big = 70000 + let wide = 4000000000u + for (i in range(4)) { + unsafe { + pb[i] = uint8(big + i) + pc[i] = int8(200 + i) + pw[i] = uint16(big + i) + ps[i] = int16(wide) + } + } + t |> equal(bytes[1], 113u8) + t |> equal(chars[1], int8(-55)) + t |> equal(words[1], uint16(4465)) + t |> equal(shorts[1], int16(10240)) + *pb = uint8(big) + *pw = uint16(wide) + t |> equal(bytes[0], 112u8) + t |> equal(words[0], uint16(10240)) + t |> equal(bytes[2], 114u8) +} + +[test(description = "an indexed store through a pointer writes bool, int64, uint64, uint, float and double values unchanged")] +def test_indexed_store_element_types(t : T?) { + var flags <- [false, false] + var longs <- [0l, 0l] + var ulongs <- [0ul, 0ul] + var uints <- [0u, 0u] + var floats <- [0.0, 0.0] + var doubles <- [0.0lf, 0.0lf] + var pflags = unsafe(addr(flags[0])) + var plongs = unsafe(addr(longs[0])) + var pulongs = unsafe(addr(ulongs[0])) + var puints = unsafe(addr(uints[0])) + var pfloats = unsafe(addr(floats[0])) + var pdoubles = unsafe(addr(doubles[0])) + let index = 1 + let seed = 3 + unsafe { + pflags[index] = seed > 2 + plongs[index] = int64(seed) - 9000000000l + pulongs[index] = uint64(seed) + 18000000000000000000ul + puints[index] = uint(seed) + 4000000000u + pfloats[index] = float(seed) * 0.5 + pdoubles[index] = double(seed) * 0.25lf + } + t |> equal(flags[1], true) + t |> equal(longs[1], -8999999997l) + t |> equal(ulongs[1], 18000000000000000003ul) + t |> equal(uints[1], 4000000003u) + t |> equal(floats[1], 1.5) + t |> equal(doubles[1], 0.75lf) + t |> equal(flags[0], false) + t |> equal(longs[0], 0l) +} + +[test(description = "an indexed read through a pointer gives uint2, int3, uint3, int4, float2 and float4 elements unchanged")] +def test_indexed_read_vector_elements(t : T?) { + var upairs <- [uint2(1u, 2u), uint2(3u, 4u)] + var triples <- [int3(1, 2, 3), int3(-4, 5, -6)] + var utriples <- [uint3(1u, 2u, 3u), uint3(4u, 5u, 6u)] + var quads <- [int4(1, 2, 3, 4), int4(-5, 6, -7, 8)] + var fpairs <- [float2(1.0, 2.0), float2(3.5, 4.5)] + var fquads <- [float4(1.0, 2.0, 3.0, 4.0), float4(5.5, 6.5, 7.5, 8.5)] + let pupairs = unsafe(addr(upairs[0])) + let ptriples = unsafe(addr(triples[0])) + let putriples = unsafe(addr(utriples[0])) + let pquads = unsafe(addr(quads[0])) + let pfpairs = unsafe(addr(fpairs[0])) + let pfquads = unsafe(addr(fquads[0])) + for (index in range(1, 2)) { + t |> equal(unsafe(pupairs[index]), uint2(3u, 4u)) + t |> equal(unsafe(ptriples[index]), int3(-4, 5, -6)) + t |> equal(unsafe(putriples[index]), uint3(4u, 5u, 6u)) + t |> equal(unsafe(pquads[index]), int4(-5, 6, -7, 8)) + t |> equal(unsafe(pfpairs[index]), float2(3.5, 4.5)) + t |> equal(unsafe(pfquads[index]), float4(5.5, 6.5, 7.5, 8.5)) + } +} + +[test(description = "an indexed read through a pointer with a uint index gives small int, int64, float, double, bool and vector elements, forward and backward")] +def test_indexed_read_uint_index_element_types(t : T?) { + var bytes <- [200u8, 7u8, 255u8, 1u8] + var chars <- [int8(-5), int8(100), int8(-128), int8(1)] + var words <- [uint16(60000), uint16(9), uint16(65535), uint16(1)] + var shorts <- [int16(-300), int16(300), int16(-32768), int16(1)] + var longs <- [1l, -9000000000l, 3l, 4l] + var floats <- [0.5, 1.5, 2.5, 3.5] + var doubles <- [0.25lf, 2.25lf, 4.25lf, 6.25lf] + var flags <- [false, true, true, false] + var points <- [float3(1.0, 2.0, 3.0), float3(4.0, 5.0, 6.0), float3(7.0, 8.0, 9.0), + float3(10.0, 11.0, 12.0)] + let pbytes = unsafe(addr(bytes[0])) + let pchars = unsafe(addr(chars[0])) + let pwords = unsafe(addr(words[0])) + let pshorts = unsafe(addr(shorts[0])) + let plongs = unsafe(addr(longs[0])) + let pfloats = unsafe(addr(floats[0])) + let pdoubles = unsafe(addr(doubles[0])) + let pflags = unsafe(addr(flags[0])) + let ppoints = unsafe(addr(points[0])) + var small_sum = 0 + var weighted = 0 + var long_sum = 0l + var float_sum = 0.0 + var double_sum = 0.0lf + var set_flags = 0 + var point_sum = float3(0.0) + for (i in urange(4u)) { + small_sum += int(unsafe(pbytes[i])) + int(unsafe(pchars[i])) + small_sum += int(unsafe(pwords[i])) + int(unsafe(pshorts[i])) + weighted += (int(unsafe(pbytes[3u - i])) + int(unsafe(pchars[3u - i]))) * int(i + 1u) + long_sum += unsafe(plongs[i]) + float_sum += unsafe(pfloats[3u - i]) + double_sum += unsafe(pdoubles[i]) + if (unsafe(pflags[i])) { + set_flags += int(i) + 1 + } + point_sum += unsafe(ppoints[i]) + } + t |> equal(small_sum, 93209) + t |> equal(weighted, 1357) + t |> equal(long_sum, -8999999992l) + t |> equal(float_sum, 8.0) + t |> equal(double_sum, 13.0lf) + t |> equal(set_flags, 5) + t |> equal(point_sum, float3(22.0, 26.0, 30.0)) +} + +[test(description = "a small int read through a pointer with a 64-bit index widens under an int and a uint cast")] +def test_indexed_read_small_int_wide_index(t : T?) { + var chars <- [int8(-5), int8(100), int8(-128), int8(1), int8(-1), int8(-1), int8(-1), int8(-1)] + var words <- [uint16(60000), uint16(9), uint16(65535), uint16(1), uint16(65535), uint16(65535)] + let pchars = unsafe(addr(chars[0])) + let pwords = unsafe(addr(words[0])) + var int_sum = 0 + var uint_sum = 0u + for (i in range64(4l)) { + int_sum += int(unsafe(pchars[i])) + uint_sum += uint(unsafe(pwords[i])) + } + t |> equal(int_sum, -32) + t |> equal(uint_sum, 125545u) +} + +[test(description = "a pointer store keeps a widening cast and a cast from float on its value; only a narrowing cast from int or uint folds into a small-int store")] +def test_pointer_store_keeps_other_casts(t : T?) { + var longs <- [0l, 0l, 0l] + var doubles <- [0.0lf, 0.0lf, 0.0lf] + var bytes : array + bytes |> resize(3) + var plongs = unsafe(addr(longs[0])) + var pdoubles = unsafe(addr(doubles[0])) + var pbytes = unsafe(addr(bytes[0])) + for (i in range(3)) { + unsafe(plongs[i]) = int64(i - 5) + unsafe(pdoubles[i]) = double(i + 2) + unsafe(pbytes[i]) = uint8(float(i) + 200.5) + } + t |> equal(longs[1], -4l) + t |> equal(doubles[2], 4.0lf) + t |> equal(bytes[1], 201u8) +} diff --git a/tests/language/uncategorized.das b/tests/language/uncategorized.das index 798da526ed..5db261d130 100644 --- a/tests/language/uncategorized.das +++ b/tests/language/uncategorized.das @@ -171,3 +171,83 @@ def joinSeq_helper(l : array; r : array) { a |> push_from(l, r) return <- a } + +struct WidenedTexel { + pad : int + color : uint8 + depth : int8 + height : uint16 + slope : int16 +} + +def widened_color_of(texel : WidenedTexel) : int => int(texel.color) + +def widened_depth_of(texel : WidenedTexel) : uint => uint(texel.depth) + +[test(description = "a small int read from a loop variable, a field of a loop variable, a reference local, a local and an argument field widens the same way under an int and a uint cast")] +def test_widened_small_int_reads(t : T?) { + var bytes <- [250u8, 1u8, 2u8] + var chars <- [int8(-2), int8(1), int8(2)] + var int_sum = 0 + var uint_sum = 0u + for (c in bytes) { + int_sum += int(c) + uint_sum += uint(c) + } + t |> equal(int_sum, 253) + t |> equal(uint_sum, 253u) + int_sum = 0 + uint_sum = 0u + for (c in chars) { + int_sum += int(c) + uint_sum += uint(c) + } + t |> equal(int_sum, 1) + t |> equal(uint_sum, 1u) + var texels <- [WidenedTexel(color = 201u8, depth = int8(-3), height = uint16(65001), + slope = int16(-400))] + for (texel in texels) { + t |> equal(int(texel.color), 201) + t |> equal(uint(texel.depth), 0xfffffffd) + t |> equal(int(texel.height), 65001) + t |> equal(int(texel.slope), -400) + t |> equal(uint(texel.slope), 0xfffffe70) + } + unsafe { + let texel & = texels[0] + t |> equal(int(texel.depth), -3) + t |> equal(uint(texel.height), 65001u) + } + let local_texel = WidenedTexel(color = 202u8, depth = int8(-4)) + t |> equal(int(local_texel.color), 202) + t |> equal(int(local_texel.depth), -4) + t |> equal(widened_color_of(local_texel), 202) + t |> equal(widened_depth_of(local_texel), 0xfffffffc) + var small = 255u8 + small = uint8(int(small) - 1) + t |> equal(int(small), 254) +} + +struct WidenedHolder { + bytes : array +} + +[test(description = "a small int read that stays on a plain node, an array reached through a pointer field, still widens under an int and a uint cast")] +def test_widened_small_int_plain_read(t : T?) { + var holder = new WidenedHolder() + holder.bytes |> resize(4) + for (i in range(4)) { + holder.bytes[i] = uint8(250 + i) + } + var int_sum = 0 + var uint_sum = 0u + for (i in range(4)) { + int_sum += int(holder.bytes[i]) + uint_sum += uint(holder.bytes[3 - i]) + } + t |> equal(int_sum, 1006) + t |> equal(uint_sum, 1006u) + unsafe { + delete holder + } +} diff --git a/tests/language/uncategorized_solid_context.das b/tests/language/uncategorized_solid_context.das index 99344abc6e..173ed53e21 100644 --- a/tests/language/uncategorized_solid_context.das +++ b/tests/language/uncategorized_solid_context.das @@ -28,3 +28,84 @@ def test_solid_context_function_address(t : T?) { t |> equal(invoke(reinterpret(declared), 20, 22), 42, "a handle casts back to a callable") } } + +var g_solid_scale = 3 +var g_solid_bias = 0.5 + +[test(description = "under solid_context a global combined with a local in a binary operation reads the global's value, for int and float")] +def test_solid_context_global_op_local(t : T?) { + var sum = 0 + var total = 0.0 + for (i in range(5)) { + sum += g_solid_scale * i + let step = float(i) + total += g_solid_bias + step + } + t |> equal(sum, 30) + t |> equal(total, 12.5) + g_solid_scale = 4 + let seven = 7 + t |> equal(g_solid_scale - seven, -3) + t |> success(g_solid_scale < seven) + g_solid_scale = 3 +} + +var g_solid_label = "four" + +[never_inline] +def solid_scaled(scale, value : int) : int => scale * value + 1 + +[never_inline] +def solid_biased(bias, value : float) : float => bias + value + +[never_inline] +def solid_labeled(text : string; count : int) : int => length(text) + count + +[test(description = "under solid_context a call that takes a global and then a local passes the global's value, for int, float and string")] +def test_solid_context_call_global_local(t : T?) { + var sum = 0 + var total = 0.0 + var lengths = 0 + for (i in range(4)) { + sum += solid_scaled(g_solid_scale, i) + let step = float(i) + total += solid_biased(g_solid_bias, step) + lengths += solid_labeled(g_solid_label, i) + } + t |> equal(sum, 22) + t |> equal(total, 8.0) + t |> equal(lengths, 22) +} + +var g_solid_table : int[8] +var g_solid_bytes = fixed_array(3u8, 2u8, 1u8, 0u8) +var g_solid_cursor : int? +var g_solid_colormap : uint8? + +[test(description = "under solid_context a global fixed array and a global pointer read and written by index give the element, and small int elements widen")] +def test_solid_context_global_indexing(t : T?) { + for (i in range(8)) { + g_solid_table[i] = i * 7 + } + var through_bytes = 0 + for (i in range(4)) { + through_bytes += g_solid_table[int(g_solid_bytes[i])] * (i + 1) + } + t |> equal(through_bytes, 70) + g_solid_cursor = unsafe(addr(g_solid_table[0])) + var through_pointer = 0 + for (i in range(8)) { + through_pointer += unsafe(g_solid_cursor[i]) + unsafe(g_solid_cursor[7 - i]) + } + t |> equal(through_pointer, 392) + unsafe(g_solid_cursor[2]) = through_pointer + 1 + t |> equal(g_solid_table[2], 393) + g_solid_colormap = unsafe(addr(g_solid_bytes[0])) + var mapped = 0 + for (i in range(4)) { + mapped += int(unsafe(g_solid_colormap[int(unsafe(g_solid_colormap[i]))])) * (i + 1) + } + t |> equal(mapped, 20) + g_solid_cursor = null + g_solid_colormap = null +}