|
| 1 | +// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Measures SIMD values held in instance fields. |
| 6 | +// |
| 7 | +// Each type has a field variant and a local variant over identical work. |
| 8 | + |
| 9 | +import 'dart:typed_data'; |
| 10 | + |
| 11 | +import 'package:benchmark_harness/benchmark_harness.dart'; |
| 12 | + |
| 13 | +const int iterations = 100000; |
| 14 | + |
| 15 | +class F32x4Holder { |
| 16 | + Float32x4 v = Float32x4.zero(); |
| 17 | +} |
| 18 | + |
| 19 | +class I32x4Holder { |
| 20 | + Int32x4 v = Int32x4(0, 0, 0, 0); |
| 21 | +} |
| 22 | + |
| 23 | +class F64x2Holder { |
| 24 | + Float64x2 v = Float64x2.zero(); |
| 25 | +} |
| 26 | + |
| 27 | +abstract class FieldBench extends BenchmarkBase { |
| 28 | + FieldBench(String name) : super('SimdFields.$name'); |
| 29 | +} |
| 30 | + |
| 31 | +class F32x4Field extends FieldBench { |
| 32 | + F32x4Field() : super('f32x4Field'); |
| 33 | + final h = F32x4Holder(); |
| 34 | + final d = Float32x4(1.0, 1.0, 1.0, 1.0); |
| 35 | + @override |
| 36 | + void run() { |
| 37 | + final holder = h; |
| 38 | + final delta = d; |
| 39 | + for (int i = 0; i < iterations; i++) { |
| 40 | + holder.v = holder.v + delta; |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +class F32x4Local extends FieldBench { |
| 46 | + F32x4Local() : super('f32x4Local'); |
| 47 | + final d = Float32x4(1.0, 1.0, 1.0, 1.0); |
| 48 | + @override |
| 49 | + void run() { |
| 50 | + final delta = d; |
| 51 | + var v = Float32x4.zero(); |
| 52 | + for (int i = 0; i < iterations; i++) { |
| 53 | + v = v + delta; |
| 54 | + } |
| 55 | + if (v.x < 0.0) throw StateError('unreachable'); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +class I32x4Field extends FieldBench { |
| 60 | + I32x4Field() : super('i32x4Field'); |
| 61 | + final h = I32x4Holder(); |
| 62 | + final d = Int32x4(1, 1, 1, 1); |
| 63 | + @override |
| 64 | + void run() { |
| 65 | + final holder = h; |
| 66 | + final delta = d; |
| 67 | + for (int i = 0; i < iterations; i++) { |
| 68 | + holder.v = holder.v + delta; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class I32x4Local extends FieldBench { |
| 74 | + I32x4Local() : super('i32x4Local'); |
| 75 | + final d = Int32x4(1, 1, 1, 1); |
| 76 | + @override |
| 77 | + void run() { |
| 78 | + final delta = d; |
| 79 | + var v = Int32x4(0, 0, 0, 0); |
| 80 | + for (int i = 0; i < iterations; i++) { |
| 81 | + v = v + delta; |
| 82 | + } |
| 83 | + if (v.x == 0x7fffffff) throw StateError('unreachable'); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +class F64x2Field extends FieldBench { |
| 88 | + F64x2Field() : super('f64x2Field'); |
| 89 | + final h = F64x2Holder(); |
| 90 | + final d = Float64x2(1.0, 1.0); |
| 91 | + @override |
| 92 | + void run() { |
| 93 | + final holder = h; |
| 94 | + final delta = d; |
| 95 | + for (int i = 0; i < iterations; i++) { |
| 96 | + holder.v = holder.v + delta; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +class F64x2Local extends FieldBench { |
| 102 | + F64x2Local() : super('f64x2Local'); |
| 103 | + final d = Float64x2(1.0, 1.0); |
| 104 | + @override |
| 105 | + void run() { |
| 106 | + final delta = d; |
| 107 | + var v = Float64x2.zero(); |
| 108 | + for (int i = 0; i < iterations; i++) { |
| 109 | + v = v + delta; |
| 110 | + } |
| 111 | + if (v.x < 0.0) throw StateError('unreachable'); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +void main() { |
| 116 | + const benchmarks = [ |
| 117 | + F32x4Field.new, |
| 118 | + F32x4Local.new, |
| 119 | + I32x4Field.new, |
| 120 | + I32x4Local.new, |
| 121 | + F64x2Field.new, |
| 122 | + F64x2Local.new, |
| 123 | + ]; |
| 124 | + |
| 125 | + for (final bm in benchmarks) { |
| 126 | + bm() |
| 127 | + ..setup() |
| 128 | + ..run() |
| 129 | + ..run(); |
| 130 | + } |
| 131 | + |
| 132 | + for (final bm in benchmarks) { |
| 133 | + bm().report(); |
| 134 | + } |
| 135 | +} |
0 commit comments