libmwemu 0.24.3

x86 32/64bits and system internals emulator, for securely emulating malware and other stuff.
Documentation
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! Tests for the FPREM1 instruction.
//!
//! FPREM1 - Partial Remainder (IEEE 754 compliant)
//!
//! Computes the IEEE remainder obtained from dividing ST(0) by ST(1) and stores result in ST(0).
//! Remainder = ST(0) - (Q * ST(1)) where Q is rounded quotient toward nearest integer.
//! May require multiple executions if C2 flag is set (partial remainder).
//! Stores three least significant bits of quotient in C3, C1, C0.
//!
//! Opcode: D9 F5
//!
//! Flags affected:
//! - C0: Set to bit 2 (Q2) of quotient
//! - C1: Set to bit 0 (Q0) of quotient (or 0 if stack underflow)
//! - C2: Set to 0 if reduction complete, 1 if incomplete
//! - C3: Set to bit 1 (Q1) of quotient
//!
//! Reference: /Users/int/dev/rax/docs/fprem1.txt

use crate::*;
const DATA_ADDR: u64 = 0x7000;

// Helper function to write f64 to memory
fn write_f64(mem: u64, addr: u64, val: f64) {
    let mut emu = emu64();
    emu.maps.write_bytes_slice(addr, &val.to_le_bytes());
}

// Helper function to read f64 from memory
fn read_f64(mem: u64, addr: u64) -> f64 {
    let emu = emu64();
    let mut buf = [0u8; 8];
    emu.maps.read_bytes_buff(&mut buf, addr);
    f64::from_le_bytes(buf)
}

// ============================================================================
// FPREM1 - Basic IEEE Remainder Operations
// ============================================================================

#[test]
fn test_fprem1_basic_positive() {
    let mut emu = emu64(); // 7.0 % 3.0 (IEEE) - rounds to nearest
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008] ; divisor (3.0)
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000] ; dividend (7.0)
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000] ; result
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 7.0);
    emu.maps.write_f64(0x2008, 3.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, 1.0, "7.0 % 3.0 (IEEE) should be 1.0");
}

#[test]
fn test_fprem1_exact_division() {
    let mut emu = emu64(); // 9.0 % 3.0 = 0.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008] ; divisor
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000] ; dividend
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 9.0);
    emu.maps.write_f64(0x2008, 3.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, 0.0, "9.0 % 3.0 should be 0.0");
}

#[test]
fn test_fprem1_small_dividend() {
    let mut emu = emu64(); // 2.0 % 5.0 = 2.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008] ; divisor
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000] ; dividend
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 2.0);
    emu.maps.write_f64(0x2008, 5.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, 2.0, "2.0 % 5.0 should be 2.0");
}

#[test]
fn test_fprem1_fractional() {
    let mut emu = emu64(); // 5.5 % 2.0 = -0.5 (rounds to nearest, Q=3)
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 5.5);
    emu.maps.write_f64(0x2008, 2.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // IEEE remainder: 5.5 = 3*2.0 - 0.5, so remainder is -0.5
    assert_eq!(result, -0.5, "5.5 % 2.0 (IEEE) should be -0.5");
}

// ============================================================================
// FPREM1 - IEEE Rounding vs FPREM
// ============================================================================

#[test]
fn test_fprem1_ieee_rounding_case1() {
    let mut emu = emu64(); // 5.0 % 3.0: Q rounds to 2 (nearest), remainder = 5.0 - 2*3.0 = -1.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 5.0);
    emu.maps.write_f64(0x2008, 3.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // IEEE: 5.0/3.0 = 1.666..., rounds to 2, so 5.0 - 2*3.0 = -1.0
    assert_eq!(result, -1.0, "5.0 % 3.0 (IEEE) should be -1.0");
}

#[test]
fn test_fprem1_ieee_rounding_case2() {
    let mut emu = emu64(); // 7.5 % 4.0: Q = 1.875 rounds to 2, remainder = 7.5 - 2*4.0 = -0.5
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 7.5);
    emu.maps.write_f64(0x2008, 4.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, -0.5, "7.5 % 4.0 (IEEE) should be -0.5");
}

#[test]
fn test_fprem1_magnitude_less_than_half() {
    let mut emu = emu64(); // IEEE remainder magnitude should be <= |modulus|/2
                           // 10.0 % 7.0: Q = 1.428... rounds to 1, remainder = 10.0 - 1*7.0 = 3.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 10.0);
    emu.maps.write_f64(0x2008, 7.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, 3.0, "10.0 % 7.0 (IEEE) should be 3.0");
    assert!(
        result.abs() <= 7.0 / 2.0,
        "Magnitude should be <= |modulus|/2"
    );
}

// ============================================================================
// FPREM1 - Negative Dividends
// ============================================================================

#[test]
fn test_fprem1_negative_dividend() {
    let mut emu = emu64(); // -7.0 % 3.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, -7.0);
    emu.maps.write_f64(0x2008, 3.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // -7.0/3.0 = -2.333... rounds to -2, so -7.0 - (-2)*3.0 = -1.0
    assert_eq!(result, -1.0, "-7.0 % 3.0 (IEEE) should be -1.0");
}

#[test]
fn test_fprem1_negative_divisor() {
    let mut emu = emu64(); // 7.0 % -3.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 7.0);
    emu.maps.write_f64(0x2008, -3.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // 7.0/-3.0 = -2.333... rounds to -2, so 7.0 - (-2)*(-3.0) = 1.0
    assert_eq!(result, 1.0, "7.0 % -3.0 (IEEE) should be 1.0");
}

#[test]
fn test_fprem1_both_negative() {
    let mut emu = emu64(); // -7.0 % -3.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, -7.0);
    emu.maps.write_f64(0x2008, -3.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // -7.0/-3.0 = 2.333... rounds to 2, so -7.0 - 2*(-3.0) = -1.0
    assert_eq!(result, -1.0, "-7.0 % -3.0 (IEEE) should be -1.0");
}

#[test]
fn test_fprem1_negative_half_case() {
    let mut emu = emu64(); // -5.0 % 3.0: rounds to nearest even
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, -5.0);
    emu.maps.write_f64(0x2008, 3.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // -5.0/3.0 = -1.666... rounds to -2, so -5.0 - (-2)*3.0 = 1.0
    assert_eq!(result, 1.0, "-5.0 % 3.0 (IEEE) should be 1.0");
}

// ============================================================================
// FPREM1 - Special Values: Zero
// ============================================================================

#[test]
fn test_fprem1_zero_dividend() {
    let mut emu = emu64(); // 0.0 % 5.0 = 0.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 0.0);
    emu.maps.write_f64(0x2008, 5.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, 0.0, "0.0 % 5.0 should be 0.0");
    assert!(!result.is_sign_negative(), "Result should be positive zero");
}

#[test]
fn test_fprem1_negative_zero_dividend() {
    let mut emu = emu64(); // -0.0 % 5.0 = -0.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, -0.0);
    emu.maps.write_f64(0x2008, 5.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, 0.0, "-0.0 % 5.0 should be -0.0");
    assert!(result.is_sign_negative(), "Result should be negative zero");
}

// ============================================================================
// FPREM1 - Divisors
// ============================================================================

#[test]
fn test_fprem1_divisor_one() {
    let mut emu = emu64(); // 5.5 % 1.0 = 0.5
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 5.5);
    emu.maps.write_f64(0x2008, 1.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // 5.5/1.0 = 5.5 rounds to 6 (even), so 5.5 - 6*1.0 = -0.5
    assert_eq!(result, -0.5, "5.5 % 1.0 (IEEE) should be -0.5");
}

#[test]
fn test_fprem1_small_divisor() {
    let mut emu = emu64(); // 10.0 % 0.5 = 0.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 10.0);
    emu.maps.write_f64(0x2008, 0.5);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, 0.0, "10.0 % 0.5 should be 0.0");
}

#[test]
fn test_fprem1_large_divisor() {
    let mut emu = emu64(); // 5.0 % 10.0 = 5.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 5.0);
    emu.maps.write_f64(0x2008, 10.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, 5.0, "5.0 % 10.0 should be 5.0");
}

#[test]
fn test_fprem1_fractional_divisor() {
    let mut emu = emu64(); // 7.0 % 1.5
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 7.0);
    emu.maps.write_f64(0x2008, 1.5);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // 7.0/1.5 = 4.666... rounds to 5, so 7.0 - 5*1.5 = -0.5
    assert_eq!(result, -0.5, "7.0 % 1.5 (IEEE) should be -0.5");
}

// ============================================================================
// FPREM1 - Special Values: Infinity
// ============================================================================

#[test]
fn test_fprem1_finite_mod_infinity() {
    let mut emu = emu64(); // 5.0 % infinity = 5.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 5.0);
    emu.maps.write_f64(0x2008, f64::INFINITY);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, 5.0, "Finite % infinity should be the finite value");
}

#[test]
fn test_fprem1_finite_mod_neg_infinity() {
    let mut emu = emu64(); // 5.0 % -infinity = 5.0
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 5.0);
    emu.maps.write_f64(0x2008, f64::NEG_INFINITY);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, 5.0, "Finite % -infinity should be the finite value");
}

// ============================================================================
// FPREM1 - Angle Reduction for Tangent
// ============================================================================

#[test]
fn test_fprem1_pi_over_4_reduction() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    let angle = 10.0;
    let pi_over_4 = std::f64::consts::FRAC_PI_4;
    emu.maps.write_f64(0x2000, angle);
    emu.maps.write_f64(0x2008, pi_over_4);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert!(
        result.abs() <= pi_over_4 / 2.0,
        "Reduced angle magnitude should be <= π/8"
    );
}

#[test]
fn test_fprem1_pi_modulo() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, std::f64::consts::PI);
    emu.maps.write_f64(0x2008, 1.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // PI/1.0 = 3.14159... rounds to 3, so PI - 3*1.0 = 0.14159...
    let expected = std::f64::consts::PI - 3.0;
    assert!(
        (result - expected).abs() < 1e-10,
        "PI % 1.0 (IEEE) computation"
    );
}

// ============================================================================
// FPREM1 - Multiple Operations
// ============================================================================

#[test]
fn test_fprem1_sequence() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xDD, 0x04, 0x25, 0x18, 0x20, 0x00, 0x00, // FLD qword [0x2018]
        0xDD, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00, // FLD qword [0x2010]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x08, 0x30, 0x00, 0x00, // FSTP qword [0x3008]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 10.0);
    emu.maps.write_f64(0x2008, 3.0);
    emu.maps.write_f64(0x2010, 15.0);
    emu.maps.write_f64(0x2018, 4.0);

    emu.run(None).unwrap();

    let result1 = emu.maps.read_f64(0x3000).unwrap();
    let result2 = emu.maps.read_f64(0x3008).unwrap();
    // 10.0/3.0 = 3.333... rounds to 3, so 10.0 - 3*3.0 = 1.0
    assert_eq!(result1, 1.0, "10.0 % 3.0 (IEEE) = 1.0");
    // 15.0/4.0 = 3.75 rounds to 4, so 15.0 - 4*4.0 = -1.0
    assert_eq!(result2, -1.0, "15.0 % 4.0 (IEEE) = -1.0");
}

// ============================================================================
// FPREM1 - Edge Cases
// ============================================================================

#[test]
fn test_fprem1_very_large_dividend() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 1000000.0);
    emu.maps.write_f64(0x2008, 7.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // 1000000/7 = 142857.142... rounds to 142857, so 1000000 - 142857*7 = 1
    assert!(result.abs() <= 3.5, "Magnitude should be <= 7/2");
}

#[test]
fn test_fprem1_very_small_values() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 1e-10);
    emu.maps.write_f64(0x2008, 3e-11);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert!(
        result.abs() <= 1.5e-11,
        "Magnitude should be <= |modulus|/2"
    );
}

#[test]
fn test_fprem1_same_values() {
    let mut emu = emu64(); // x % x = 0
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 5.5);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    assert_eq!(result, 0.0, "x % x should be 0");
}

#[test]
fn test_fprem1_preserves_divisor() {
    let mut emu = emu64(); // FPREM1 only modifies ST(0), ST(1) (divisor) remains unchanged
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008] ; divisor
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000] ; dividend
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000] ; remainder
        0xDD, 0x1C, 0x25, 0x08, 0x30, 0x00, 0x00, // FSTP qword [0x3008] ; divisor
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 10.0);
    emu.maps.write_f64(0x2008, 3.0);

    emu.run(None).unwrap();

    let remainder = emu.maps.read_f64(0x3000).unwrap();
    let divisor = emu.maps.read_f64(0x3008).unwrap();
    assert_eq!(remainder, 1.0, "Remainder should be 1.0");
    assert_eq!(divisor, 3.0, "Divisor should remain 3.0");
}

// ============================================================================
// FPREM1 - Various Combinations
// ============================================================================

#[test]
fn test_fprem1_power_of_two_divisor() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 17.0);
    emu.maps.write_f64(0x2008, 8.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // 17.0/8.0 = 2.125 rounds to 2, so 17.0 - 2*8.0 = 1.0
    assert_eq!(result, 1.0, "17.0 % 8.0 (IEEE) = 1.0");
}

#[test]
fn test_fprem1_irrational_dividend() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    let sqrt2 = 2.0f64.sqrt();
    emu.maps.write_f64(0x2000, sqrt2);
    emu.maps.write_f64(0x2008, 1.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // sqrt(2)/1.0 = 1.414... rounds to 1, so sqrt(2) - 1*1.0 = 0.414...
    let expected = sqrt2 - 1.0;
    assert!((result - expected).abs() < 1e-10, "sqrt(2) % 1.0 (IEEE)");
}

#[test]
fn test_fprem1_halfway_cases() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 2.5);
    emu.maps.write_f64(0x2008, 1.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // 2.5/1.0 = 2.5, rounds to 2 (even), so 2.5 - 2*1.0 = 0.5
    assert_eq!(result, 0.5, "2.5 % 1.0 (IEEE) should round to even");
}

#[test]
fn test_fprem1_completion() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword [0x2008]
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword [0x2000]
        0xD9, 0xF5, // FPREM1
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword [0x3000]
        0xF4, // HLT
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x2000, 100.0);
    emu.maps.write_f64(0x2008, 7.0);

    emu.run(None).unwrap();

    let result = emu.maps.read_f64(0x3000).unwrap();
    // 100.0/7.0 = 14.285... rounds to 14, so 100.0 - 14*7.0 = 2.0
    assert_eq!(result, 2.0, "100.0 % 7.0 (IEEE) = 2.0");
}