rvsim-core 2.0.0

A cycle-level RISC-V 64-bit system simulator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! Widening and narrowing operations, including widening FMA.

use super::convert::{
    f32_to_i64_frm, f32_to_u64_frm, f64_to_f32_round_to_odd, f64_to_i32_frm, f64_to_u32_frm,
};
use super::{elem_to_f32, elem_to_f64};
use crate::exec::compute::fpu::half::{f16_to_f32, f64_to_f16};
use crate::exec::compute::fpu::host::{clear_host_fp_flags, read_host_fp_flags};
use crate::exec::compute::fpu::nan_handling::{box_f32_canon, canonicalize_f64_bits};
use crate::exec::compute::fpu::rmm::rmm_round_f64_to_f32;
use crate::exec::compute::vector::context::{
    VecExecCtx, VecExecResult, VecOperand, mask_active, read_op1, sign_extend, widen_sew,
};
use crate::exec::compute::vector::regfile::VectorRegFile;
use crate::isa::fp::{FpFlags, RoundingMode};
use crate::isa::op::VectorOp;
use crate::isa::rvv::{ElemIdx, Sew, VRegIdx, Vlmax};

/// Widening FP operations: read at SEW, write at 2*SEW.
#[allow(clippy::too_many_lines)]
pub(super) fn exec_fp_widening(
    op: VectorOp,
    vpr: &mut impl VectorRegFile,
    vd_idx: VRegIdx,
    vs2_idx: VRegIdx,
    operand1: VecOperand,
    ctx: &VecExecCtx,
) -> VecExecResult {
    let Some(wsew) = widen_sew(ctx.sew) else {
        return VecExecResult { vxsat: false, scalar_result: None, fp_flags: FpFlags::NONE };
    };

    // Handle widening FMA BEFORE the main loop — the FMA reads vd as the
    // accumulator, so we must not overwrite it with the arithmetic path first.
    if matches!(op, VectorOp::VFWMacc | VectorOp::VFWNMacc | VectorOp::VFWMSac | VectorOp::VFWNMSac)
    {
        return exec_fp_widening_fma(op, vpr, vd_idx, vs2_idx, operand1, ctx);
    }

    let vlmax = Vlmax::compute(vpr.vlen(), ctx.sew, ctx.vlmul).as_usize();
    let mut flags = FpFlags::NONE;

    // Determine if this is a ".w" variant (vs2 is already wide)
    let vs2_wide = matches!(op, VectorOp::VFWAddW | VectorOp::VFWSubW);

    for i in 0..vlmax {
        if i < ctx.vstart {
            continue;
        }
        if i >= ctx.vl {
            if ctx.vta.is_agnostic() {
                vpr.write_element(vd_idx, ElemIdx::new(i), wsew, wsew.ones());
            }
            continue;
        }
        if !ctx.vm && !mask_active(vpr, i) {
            if ctx.vma.is_agnostic() {
                vpr.write_element(vd_idx, ElemIdx::new(i), wsew, wsew.ones());
            }
            continue;
        }

        // Read source elements
        let vs2_raw = if vs2_wide {
            vpr.read_element(vs2_idx, ElemIdx::new(i), wsew)
        } else {
            vpr.read_element(vs2_idx, ElemIdx::new(i), ctx.sew)
        };
        let op1_raw = read_op1(vpr, &operand1, i, ctx.sew);

        // For widening FP: SEW=32 -> f32 inputs, f64 output
        if ctx.sew == Sew::E32 {
            // Handle integer-producing conversions (bypass the f64 path)
            if matches!(
                op,
                VectorOp::VFWCvtXuF
                    | VectorOp::VFWCvtXF
                    | VectorOp::VFWCvtRtzXuF
                    | VectorOp::VFWCvtRtzXF
                    | VectorOp::VFWCvtFXu
                    | VectorOp::VFWCvtFX
            ) {
                let (bits, f) = match op {
                    VectorOp::VFWCvtXuF => {
                        let a = elem_to_f32(vs2_raw);
                        let (r, fl) = f32_to_u64_frm(a, ctx.frm);
                        (r, fl)
                    }
                    VectorOp::VFWCvtXF => {
                        let a = elem_to_f32(vs2_raw);
                        let (r, fl) = f32_to_i64_frm(a, ctx.frm);
                        (r as u64, fl)
                    }
                    VectorOp::VFWCvtRtzXuF => {
                        let a = elem_to_f32(vs2_raw);
                        let (r, fl) = f32_to_u64_frm(a, RoundingMode::Rtz);
                        (r, fl)
                    }
                    VectorOp::VFWCvtRtzXF => {
                        let a = elem_to_f32(vs2_raw);
                        let (r, fl) = f32_to_i64_frm(a, RoundingMode::Rtz);
                        (r as u64, fl)
                    }
                    VectorOp::VFWCvtFXu => ((vs2_raw as u32 as f64).to_bits(), FpFlags::NONE),
                    VectorOp::VFWCvtFX => {
                        ((sign_extend(vs2_raw, ctx.sew) as i32 as f64).to_bits(), FpFlags::NONE)
                    }
                    _ => (0, FpFlags::NONE),
                };
                flags = flags | f;
                vpr.write_element(vd_idx, ElemIdx::new(i), wsew, bits);
                continue;
            }

            // FP arithmetic widening path
            let vs2_f = if vs2_wide { elem_to_f64(vs2_raw) } else { elem_to_f32(vs2_raw) as f64 };
            let op1_f = elem_to_f32(op1_raw) as f64;

            clear_host_fp_flags();
            let r = std::hint::black_box(match op {
                VectorOp::VFWAdd | VectorOp::VFWAddW => {
                    std::hint::black_box(vs2_f) + std::hint::black_box(op1_f)
                }
                VectorOp::VFWSub | VectorOp::VFWSubW => {
                    std::hint::black_box(vs2_f) - std::hint::black_box(op1_f)
                }
                VectorOp::VFWMul => std::hint::black_box(vs2_f) * std::hint::black_box(op1_f),
                _ => vs2_f,
            });
            let f = read_host_fp_flags();
            flags = flags | f;
            vpr.write_element(vd_idx, ElemIdx::new(i), wsew, canonicalize_f64_bits(r));
        } else if ctx.sew == Sew::E16 && ctx.zvfh {
            // Zvfh widening: SEW=16 (f16) -> wsew=E32 (f32)
            // Handle integer-producing conversions
            if matches!(
                op,
                VectorOp::VFWCvtXuF
                    | VectorOp::VFWCvtXF
                    | VectorOp::VFWCvtRtzXuF
                    | VectorOp::VFWCvtRtzXF
                    | VectorOp::VFWCvtFXu
                    | VectorOp::VFWCvtFX
            ) {
                clear_host_fp_flags();
                let a16 = vs2_raw as u16;
                let a_f = f16_to_f32(a16);
                let bits = match op {
                    VectorOp::VFWCvtXuF | VectorOp::VFWCvtRtzXuF => {
                        if a_f.is_nan() {
                            u32::MAX as u64
                        } else {
                            a_f as u32 as u64
                        }
                    }
                    VectorOp::VFWCvtXF | VectorOp::VFWCvtRtzXF => {
                        if a_f.is_nan() {
                            i32::MAX as u64
                        } else {
                            a_f as i32 as u32 as u64
                        }
                    }
                    VectorOp::VFWCvtFXu => (vs2_raw as u16 as f32).to_bits() as u64,
                    VectorOp::VFWCvtFX => {
                        (sign_extend(vs2_raw, Sew::E16) as i16 as f32).to_bits() as u64
                    }
                    _ => 0,
                };
                let nan_input = matches!(
                    op,
                    VectorOp::VFWCvtXuF
                        | VectorOp::VFWCvtXF
                        | VectorOp::VFWCvtRtzXuF
                        | VectorOp::VFWCvtRtzXF
                ) && a_f.is_nan();
                let f = read_host_fp_flags() | if nan_input { FpFlags::NV } else { FpFlags::NONE };
                flags = flags | f;
                vpr.write_element(vd_idx, ElemIdx::new(i), wsew, bits);
                continue;
            }

            // FP arithmetic widening: f16 inputs → f32 output (via f64 for lossless computation)
            let vs2_f = if vs2_wide {
                elem_to_f32(vs2_raw) as f64
            } else {
                f16_to_f32(vs2_raw as u16) as f64
            };
            let op1_f = f16_to_f32(op1_raw as u16) as f64;

            clear_host_fp_flags();
            let r_f64 = std::hint::black_box(match op {
                VectorOp::VFWAdd | VectorOp::VFWAddW => {
                    std::hint::black_box(vs2_f) + std::hint::black_box(op1_f)
                }
                VectorOp::VFWSub | VectorOp::VFWSubW => {
                    std::hint::black_box(vs2_f) - std::hint::black_box(op1_f)
                }
                VectorOp::VFWMul => std::hint::black_box(vs2_f) * std::hint::black_box(op1_f),
                // VFWCvtFF (f16→f32) and other widening identity paths return
                // the input unchanged (the widen happens via the cast above).
                _ => vs2_f,
            });
            let f = read_host_fp_flags();
            flags = flags | f;
            // RMM has no native host equivalent (set_host_round_mode mapped it
            // to FE_TONEAREST). f16+f16 in f64 is exact, so the only rounding
            // happens in the f64→f32 cast at the end. Use rmm_round_f64_to_f32
            // to fix the half-ULP ties to max-magnitude under RMM.
            let r_f32 = if ctx.frm == RoundingMode::Rmm {
                rmm_round_f64_to_f32(r_f64)
            } else {
                r_f64 as f32
            };
            vpr.write_element(vd_idx, ElemIdx::new(i), wsew, box_f32_canon(r_f32));
        }
    }

    VecExecResult { vxsat: false, scalar_result: None, fp_flags: flags }
}

/// Widening FMA operations.
pub(super) fn exec_fp_widening_fma(
    op: VectorOp,
    vpr: &mut impl VectorRegFile,
    vd_idx: VRegIdx,
    vs2_idx: VRegIdx,
    operand1: VecOperand,
    ctx: &VecExecCtx,
) -> VecExecResult {
    let Some(wsew) = widen_sew(ctx.sew) else {
        return VecExecResult { vxsat: false, scalar_result: None, fp_flags: FpFlags::NONE };
    };
    let vlmax = Vlmax::compute(vpr.vlen(), ctx.sew, ctx.vlmul).as_usize();
    let mut flags = FpFlags::NONE;

    for i in 0..vlmax {
        if i < ctx.vstart {
            continue;
        }
        if i >= ctx.vl {
            if ctx.vta.is_agnostic() {
                vpr.write_element(vd_idx, ElemIdx::new(i), wsew, wsew.ones());
            }
            continue;
        }
        if !ctx.vm && !mask_active(vpr, i) {
            if ctx.vma.is_agnostic() {
                vpr.write_element(vd_idx, ElemIdx::new(i), wsew, wsew.ones());
            }
            continue;
        }

        let vs2_raw = vpr.read_element(vs2_idx, ElemIdx::new(i), ctx.sew);
        let op1_raw = read_op1(vpr, &operand1, i, ctx.sew);
        let vd_raw = vpr.read_element(vd_idx, ElemIdx::new(i), wsew);

        if ctx.sew == Sew::E32 {
            let vs2_f = elem_to_f32(vs2_raw) as f64;
            let op1_f = elem_to_f32(op1_raw) as f64;
            let vd_f = elem_to_f64(vd_raw);

            clear_host_fp_flags();
            let r = std::hint::black_box(match op {
                VectorOp::VFWMacc => std::hint::black_box(op1_f)
                    .mul_add(std::hint::black_box(vs2_f), std::hint::black_box(vd_f)),
                VectorOp::VFWNMacc => (-std::hint::black_box(op1_f))
                    .mul_add(std::hint::black_box(vs2_f), -std::hint::black_box(vd_f)),
                VectorOp::VFWMSac => std::hint::black_box(op1_f)
                    .mul_add(std::hint::black_box(vs2_f), -std::hint::black_box(vd_f)),
                VectorOp::VFWNMSac => (-std::hint::black_box(op1_f))
                    .mul_add(std::hint::black_box(vs2_f), std::hint::black_box(vd_f)),
                _ => vd_f,
            });
            let f = read_host_fp_flags();
            flags = flags | f;
            vpr.write_element(vd_idx, ElemIdx::new(i), wsew, canonicalize_f64_bits(r));
        } else if ctx.sew == Sew::E16 && ctx.zvfh {
            // Zvfh widening FMA: f16 inputs → f32 accumulator
            let vs2_f = f16_to_f32(vs2_raw as u16) as f64;
            let op1_f = f16_to_f32(op1_raw as u16) as f64;
            let vd_f = elem_to_f32(vd_raw) as f64;

            clear_host_fp_flags();
            let r = std::hint::black_box(match op {
                VectorOp::VFWMacc => std::hint::black_box(op1_f)
                    .mul_add(std::hint::black_box(vs2_f), std::hint::black_box(vd_f)),
                VectorOp::VFWNMacc => (-std::hint::black_box(op1_f))
                    .mul_add(std::hint::black_box(vs2_f), -std::hint::black_box(vd_f)),
                VectorOp::VFWMSac => std::hint::black_box(op1_f)
                    .mul_add(std::hint::black_box(vs2_f), -std::hint::black_box(vd_f)),
                VectorOp::VFWNMSac => (-std::hint::black_box(op1_f))
                    .mul_add(std::hint::black_box(vs2_f), std::hint::black_box(vd_f)),
                _ => vd_f,
            });
            let f = read_host_fp_flags();
            flags = flags | f;
            vpr.write_element(vd_idx, ElemIdx::new(i), wsew, box_f32_canon(r as f32));
        }
    }

    VecExecResult { vxsat: false, scalar_result: None, fp_flags: flags }
}

/// Narrowing FP operations: read at 2*SEW (or SEW for destination), write at SEW.
pub(super) fn exec_fp_narrowing(
    op: VectorOp,
    vpr: &mut impl VectorRegFile,
    vd_idx: VRegIdx,
    vs2_idx: VRegIdx,
    _operand1: VecOperand,
    ctx: &VecExecCtx,
) -> VecExecResult {
    // For narrowing, the destination SEW is ctx.sew, source is 2*ctx.sew
    let Some(src_sew) = widen_sew(ctx.sew) else {
        return VecExecResult { vxsat: false, scalar_result: None, fp_flags: FpFlags::NONE };
    };
    let vlmax = Vlmax::compute(vpr.vlen(), ctx.sew, ctx.vlmul).as_usize();
    let mut flags = FpFlags::NONE;

    for i in 0..vlmax {
        if i < ctx.vstart {
            continue;
        }
        if i >= ctx.vl {
            if ctx.vta.is_agnostic() {
                vpr.write_element(vd_idx, ElemIdx::new(i), ctx.sew, ctx.sew.ones());
            }
            continue;
        }
        if !ctx.vm && !mask_active(vpr, i) {
            if ctx.vma.is_agnostic() {
                vpr.write_element(vd_idx, ElemIdx::new(i), ctx.sew, ctx.sew.ones());
            }
            continue;
        }

        let vs2_raw = vpr.read_element(vs2_idx, ElemIdx::new(i), src_sew);

        // Narrowing: src_sew=E64 -> dst_sew=E32
        let (result, f) = if ctx.sew == Sew::E32 {
            let a64 = elem_to_f64(vs2_raw);
            match op {
                VectorOp::VFNCvtFF => {
                    clear_host_fp_flags();
                    let r = std::hint::black_box(std::hint::black_box(a64) as f32);
                    (box_f32_canon(r) & 0xFFFF_FFFF, read_host_fp_flags())
                }
                VectorOp::VFNCvtRodFF => {
                    // Round-to-odd: truncate to f32 precision and jam LSB to 1
                    // if any bit was lost. Round-to-odd does not raise the
                    // inexact flag (per Zvfh).
                    let r = f64_to_f32_round_to_odd(a64);
                    (box_f32_canon(r) & 0xFFFF_FFFF, FpFlags::NONE)
                }
                VectorOp::VFNCvtXuF => {
                    let (r, f) = f64_to_u32_frm(a64, ctx.frm);
                    (r as u64, f)
                }
                VectorOp::VFNCvtXF => {
                    let (r, f) = f64_to_i32_frm(a64, ctx.frm);
                    (r as u32 as u64, f)
                }
                VectorOp::VFNCvtRtzXuF => {
                    let (r, f) = f64_to_u32_frm(a64, RoundingMode::Rtz);
                    (r as u64, f)
                }
                VectorOp::VFNCvtRtzXF => {
                    let (r, f) = f64_to_i32_frm(a64, RoundingMode::Rtz);
                    (r as u32 as u64, f)
                }
                VectorOp::VFNCvtFXu => {
                    // Convert full 2*SEW unsigned integer to SEW float.
                    // Must not truncate to u32 first — the source is a 64-bit integer.
                    clear_host_fp_flags();
                    let r = std::hint::black_box(vs2_raw as f32);
                    (r.to_bits() as u64, read_host_fp_flags())
                }
                VectorOp::VFNCvtFX => {
                    // Convert full 2*SEW signed integer to SEW float.
                    // Must not truncate to i32 first — the source is a 64-bit integer.
                    clear_host_fp_flags();
                    let r = std::hint::black_box(sign_extend(vs2_raw, src_sew) as f32);
                    (r.to_bits() as u64, read_host_fp_flags())
                }
                _ => (0, FpFlags::NONE),
            }
        } else if ctx.sew == Sew::E16 && ctx.zvfh {
            // Zvfh narrowing: src_sew=E32 (f32) -> dst_sew=E16 (f16)
            let a32 = elem_to_f32(vs2_raw);
            match op {
                VectorOp::VFNCvtFF => {
                    // f32 -> f16 with rounding mode
                    let (bits, f) = f64_to_f16(a32 as f64, ctx.frm);
                    (bits as u64, f)
                }
                VectorOp::VFNCvtRodFF => {
                    // Round-to-odd: round toward zero into f16, then jam the
                    // mantissa LSB to 1 if rounding was inexact (so the
                    // result is always odd when narrowing loses precision).
                    // Round-to-odd suppresses NX (per Zvfh).
                    let (bits, f) = f64_to_f16(a32 as f64, RoundingMode::Rtz);
                    let inexact = f.bits() & FpFlags::NX.bits() != 0;
                    let finite = !a32.is_nan() && !a32.is_infinite();
                    let jammed = if inexact && finite { bits | 1 } else { bits };
                    let flags_no_nx = FpFlags::from_bits(f.bits() & !FpFlags::NX.bits());
                    (jammed as u64, flags_no_nx)
                }
                VectorOp::VFNCvtXuF => {
                    clear_host_fp_flags();
                    let r = if a32.is_nan() { u16::MAX as u64 } else { a32 as u16 as u64 };
                    let f = read_host_fp_flags()
                        | if a32.is_nan() { FpFlags::NV } else { FpFlags::NONE };
                    (r, f)
                }
                VectorOp::VFNCvtXF => {
                    clear_host_fp_flags();
                    let r = if a32.is_nan() { i16::MAX as u64 } else { a32 as i16 as u16 as u64 };
                    let f = read_host_fp_flags()
                        | if a32.is_nan() { FpFlags::NV } else { FpFlags::NONE };
                    (r, f)
                }
                VectorOp::VFNCvtRtzXuF => {
                    clear_host_fp_flags();
                    let r = if a32.is_nan() {
                        u16::MAX as u64
                    } else {
                        std::hint::black_box(a32) as u16 as u64
                    };
                    let f = read_host_fp_flags()
                        | if a32.is_nan() { FpFlags::NV } else { FpFlags::NONE };
                    (r, f)
                }
                VectorOp::VFNCvtRtzXF => {
                    clear_host_fp_flags();
                    let r = if a32.is_nan() {
                        i16::MAX as u64
                    } else {
                        std::hint::black_box(a32) as i16 as u16 as u64
                    };
                    let f = read_host_fp_flags()
                        | if a32.is_nan() { FpFlags::NV } else { FpFlags::NONE };
                    (r, f)
                }
                VectorOp::VFNCvtFXu => {
                    // 2*SEW unsigned int (u32) → f16
                    let (bits, f) = f64_to_f16(vs2_raw as u32 as f64, ctx.frm);
                    (bits as u64, f)
                }
                VectorOp::VFNCvtFX => {
                    // 2*SEW signed int (i32) → f16
                    let (bits, f) =
                        f64_to_f16(sign_extend(vs2_raw, src_sew) as i32 as f64, ctx.frm);
                    (bits as u64, f)
                }
                _ => (0, FpFlags::NONE),
            }
        } else {
            (0u64, FpFlags::NONE)
        };

        flags = flags | f;
        vpr.write_element(vd_idx, ElemIdx::new(i), ctx.sew, result);
    }

    VecExecResult { vxsat: false, scalar_result: None, fp_flags: flags }
}