-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from xyjixyjixyji/master
[feat] some floating point operations and some dword operations with test cases
- Loading branch information
Showing
12 changed files
with
596 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.