Skip to content

Commit

Permalink
Merge pull request #7 from xyjixyjixyji/master
Browse files Browse the repository at this point in the history
[feat] some floating point operations and some dword operations with test cases
  • Loading branch information
sisshiki1969 authored Oct 30, 2024
2 parents a96f8ae + 9915d9e commit 3d9b583
Show file tree
Hide file tree
Showing 12 changed files with 596 additions and 31 deletions.
4 changes: 4 additions & 0 deletions monoasm/src/jit_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,10 @@ impl JitMemory {
}
}

pub fn enc_rex_digit(&mut self, op: &[u8], rm: Rm, digit: u8, imm: Imm) {
self.encode(op, Rex::REX, ModRM::Digit(digit), rm, imm);
}

/// Encoding: /n
/// REX.W Op /n
pub fn enc_rexw_digit(&mut self, op: &[u8], rm: Rm, digit: u8, imm: Imm) {
Expand Down
26 changes: 26 additions & 0 deletions monoasm/tests/andpd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern crate monoasm;
extern crate monoasm_macro;

use monoasm::*;
use monoasm_macro::monoasm;

#[test]
fn andpd() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let pi =-3.141592653589793;
let mask = 0x7fffffffffffffffu64; // mask for the sign bit
monoasm!(&mut jit,
begin:
movq rax, (mask);
movq xmm1, rax;

andpd xmm0, xmm1;
ret;
);
jit.finalize();

let f = jit.get_label_addr::<f64, f64>(begin);
let ret = f(pi);
assert_eq!(ret, pi.abs());
}
24 changes: 24 additions & 0 deletions monoasm/tests/cdq.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
extern crate monoasm;
extern crate monoasm_macro;

use monoasm::*;
use monoasm_macro::monoasm;
pub type ReturnFunc = extern "C" fn() -> u64;

#[test]
fn cdq() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
monoasm!(&mut jit,
begin:
movq rax, rdi;
cdq;
movl rax, rdx;
ret;
);
jit.finalize();

let f = jit.get_label_addr::<u64, u64>(begin);
let ret = f(0x80000000); // rax contains 0x00000000FFFFFFFF
assert_eq!(ret, 0x00000000FFFFFFFF);
}
59 changes: 59 additions & 0 deletions monoasm/tests/counts_dword.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
extern crate monoasm;
extern crate monoasm_macro;

use monoasm::*;
use monoasm_macro::monoasm;

#[test]
fn lzcntl() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let val = 0x000FF0F0;
monoasm!(&mut jit,
begin:
movl rax, rdi;
lzcntl rax, rax;
ret;
);
jit.finalize();

let f = jit.get_label_addr::<u32, u32>(begin);
let ret = f(val);
assert_eq!(ret, 12);
}

#[test]
fn tzcntl() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let val = 0x000F0F40; // 0000 0000 0000 1111 0000 1111 0100 0000
monoasm!(&mut jit,
begin:
movl rax, rdi;
tzcntl rax, rax;
ret;
);
jit.finalize();

let f = jit.get_label_addr::<u32, u32>(begin);
let ret = f(val);
assert_eq!(ret, 6);
}

#[test]
fn popcntl() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let val = 0xDEADBEEFu32; // 1101 1110 1010 1101 1011 1110 1110 1111
monoasm!(&mut jit,
begin:
movq rax, rdi;
popcntl rax, rax;
ret;
);
jit.finalize();

let f = jit.get_label_addr::<u32, u32>(begin);
let ret = f(val);
assert_eq!(ret, 24);
}
24 changes: 24 additions & 0 deletions monoasm/tests/cvttsd2si.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
extern crate monoasm;
extern crate monoasm_macro;

use monoasm::*;
use monoasm_macro::monoasm;
pub type ReturnFunc = extern "C" fn() -> u64;

#[test]
fn cvttsd2si() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let pi = 3.141592653589793f64;
monoasm!(&mut jit,
begin:
cvttsd2siq rax, xmm0;
ret;
);
jit.finalize();

let f = jit.get_label_addr::<f64, i64>(begin);
let ret = f(pi);
assert_eq!(ret, 3);
}

48 changes: 48 additions & 0 deletions monoasm/tests/divl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
extern crate monoasm;
extern crate monoasm_macro;

use monoasm::*;
use monoasm_macro::monoasm;

#[test]
fn divl() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
monoasm!(&mut jit,
begin:
xorl rdx, rdx;
xorl rax, rax;
movq rax, rdi;
movq r12, rsi;
cdq;
divl r12; // eax = 7, edx = 0
ret;
);
jit.finalize();

let f = jit.get_label_addr2::<i32, i32, i32>(begin);
let ret = f(7777777, 1111111); // rax contains (7)
assert_eq!(ret, 7);
}

#[test]
fn divl_rem() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
monoasm!(&mut jit,
begin:
xorl rdx, rdx;
xorl rax, rax;
movq rax, (7);
movq r12, (3);
cdq;
divl r12; // eax = 2, edx = 1
movl rax, rdx;
ret;
);
jit.finalize();

let f = jit.get_label_addr2::<i32, i32, i32>(begin);
let ret = f(7, 3); // rax contains (1)
assert_eq!(ret, 1);
}
49 changes: 49 additions & 0 deletions monoasm/tests/idivl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
extern crate monoasm;
extern crate monoasm_macro;

use monoasm::*;
use monoasm_macro::monoasm;
pub type ReturnFunc = extern "C" fn() -> u64;

#[test]
fn idivl() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
monoasm!(&mut jit,
begin:
xorl rdx, rdx;
xorl rax, rax;
movq rax, rdi;
movq r12, rsi;
cdq;
idivl r12; // eax = -7, edx = 0
ret;
);
jit.finalize();

let f = jit.get_label_addr2::<i32, i32, i32>(begin);
let ret = f(-7777777, 1111111); // rax contains (-7)
assert_eq!(ret, -7);
}

#[test]
fn idivl_rem() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
monoasm!(&mut jit,
begin:
xorl rdx, rdx;
xorl rax, rax;
movq rax, rdi;
movq r12, rsi;
cdq;
idivl r12; // eax = -5, edx = -3
movl rax, rdx;
ret;
);
jit.finalize();

let f = jit.get_label_addr2::<i32, i32, i32>(begin);
let ret = f(-23, 4); // rax contains (-3)
assert_eq!(ret, -3);
}
22 changes: 22 additions & 0 deletions monoasm/tests/roundpd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extern crate monoasm;
extern crate monoasm_macro;

use monoasm::*;
use monoasm_macro::monoasm;

#[test]
fn roundpd() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let pi = 3.141592653589793f64;
monoasm!(&mut jit,
begin:
roundpd xmm0, xmm0, (0x02); // 0x02 -> ceil
ret;
);
jit.finalize();

let f = jit.get_label_addr::<f64, f64>(begin);
let ret = f(pi);
assert_eq!(ret, pi.ceil());
}
119 changes: 119 additions & 0 deletions monoasm/tests/shift_dword.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
extern crate monoasm;
extern crate monoasm_macro;

use monoasm::*;
use monoasm_macro::monoasm;

#[test]
fn shll() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let val = 0x0000000000000040u64; // 0000 ... 0000 0100 0000
monoasm!(&mut jit,
begin:
movq rax, rdi;
movb cl, (0x1F); // 31, oops, its all gone since it is shll
shll rax, cl;
ret;
);
jit.finalize();

let f = jit.get_label_addr::<u64, u64>(begin);
let ret = f(val);
assert_eq!(ret, 0x0000000000000000u64);
}

#[test]
fn shrl() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let val = 0x0000000000000040u64; // 0000 ... 0000 0100 0000
monoasm!(&mut jit,
begin:
movq rax, rdi;
movb cl, (0x01);
shrl rax, cl;
ret;
);
jit.finalize();

let f = jit.get_label_addr::<u64, u64>(begin);
let ret = f(val);
assert_eq!(ret, 0x0000000000000020u64);
}

#[test]
fn sall() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let val = 0x0000000000000001u64; // 0000 ... 0000 0001
monoasm!(&mut jit,
begin:
movq rax, rdi;
movb cl, (0x1F);
sall rax, cl;
ret;
);
jit.finalize();

let f = jit.get_label_addr::<u64, u64>(begin);
let ret = f(val);
assert_eq!(ret, 0x0000000080000000u64);
}

#[test]
fn sarl() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let val = 0x0000000080000000u64; // 0000 ... 0000 0001
monoasm!(&mut jit,
begin:
movq rax, rdi;
movb cl, (0x1F);
sarl rax, cl; // dword operation, so it should carry the sign bit
ret;
);
jit.finalize();

let f = jit.get_label_addr::<u64, u64>(begin);
let ret = f(val);
assert_eq!(ret, 0x00000000FFFFFFFFu64);
}

#[test]
fn roll() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let val = 0x0000000000000001u64; // 0000 ... 0000 0001
monoasm!(&mut jit,
begin:
movq rax, rdi;
movb cl, (0x04);
roll rax, cl;
ret;
);
jit.finalize();

let f = jit.get_label_addr::<u64, u64>(begin);
let ret = f(val);
assert_eq!(ret, 0x0000000000000010u64);
}

#[test]
fn rorl() {
let mut jit: JitMemory = JitMemory::new();
let begin = jit.label();
let val = 0x0000000000000010u64; // 0000 ... 0000 0001
monoasm!(&mut jit,
begin:
movq rax, rdi;
movb cl, (0x04);
rorl rax, cl;
ret;
);
jit.finalize();

let f = jit.get_label_addr::<u64, u64>(begin);
let ret = f(val);
assert_eq!(ret, 0x0000000000000001u64);
}
Loading

0 comments on commit 3d9b583

Please sign in to comment.