GitLab will continue to be upgraded from 11.4.5-ce.0 on November 25th 2019 at 4.00pm (AEDT) to 5.00pm (AEDT) due to Critical Security Patch Availability. During the update, GitLab and Mattermost services will not be available.

test_int128.rs 4.38 KB
Newer Older
qinsoon's avatar
qinsoon committed
1
extern crate libloading;
2
extern crate extprim;
qinsoon's avatar
qinsoon committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

use mu::ast::ir::*;
use mu::ast::inst::*;
use mu::ast::types;
use mu::ast::types::*;
use mu::ast::op::*;
use mu::vm::*;

use std::sync::Arc;
use std::sync::RwLock;
use mu::utils::LinkedHashMap;
use mu::testutil;

#[test]
fn test_add_u128() {
    let lib = testutil::compile_fnc("add_u128", &add_u128);

    unsafe {
21 22
        use std::u64;

qinsoon's avatar
qinsoon committed
23 24 25 26 27
        let add_u128 : libloading::Symbol<unsafe extern fn(u64, u64, u64, u64) -> (u64, u64)> = lib.get(b"add_u128").unwrap();

        let res = add_u128(1, 0, 1, 0);
        println!("add_u128(1, 1) = {:?}", res);
        assert!(res == (2, 0));
28 29 30 31

        let res = add_u128(u64::MAX, 0, 1, 0);
        println!("add_u128(u64::MAX, 1) = {:?}", res);
        assert!(res == (0, 1));
qinsoon's avatar
qinsoon committed
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
    }
}

fn add_u128() -> VM {
    let vm = VM::new();

    typedef!    ((vm) u128 = mu_int(128));

    funcsig!    ((vm) sig = (u128, u128) -> (u128));
    funcdecl!   ((vm) <sig> add_u128);
    funcdef!    ((vm) <sig> add_u128 VERSION add_u128_v1);

    block!      ((vm, add_u128_v1) blk_entry);
    ssa!        ((vm, add_u128_v1) <u128> a);
    ssa!        ((vm, add_u128_v1) <u128> b);

    // sum = Add %a %b
    ssa!        ((vm, add_u128_v1) <u128> sum);
    inst!       ((vm, add_u128_v1) blk_entry_add_u128:
        sum = BINOP (BinOp::Add) a b
    );

    inst!       ((vm, add_u128_v1) blk_entry_ret:
        RET (sum)
    );

    define_block!   ((vm, add_u128_v1) blk_entry(a, b) {
        blk_entry_add_u128, blk_entry_ret
    });

    define_func_ver!((vm) add_u128_v1 (entry: blk_entry) {blk_entry});

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
    vm
}

#[test]
fn test_mul_u128() {
    let lib = testutil::compile_fnc("mul_u128", &mul_u128);

    unsafe {
        use std::u64;

        let mul_u128 : libloading::Symbol<unsafe extern fn(u64, u64, u64, u64) -> (u64, u64)> = lib.get(b"mul_u128").unwrap();

        let res = mul_u128(6, 0, 7, 0);
        println!("mul_u128(6, 7) = {:?}", res);
        assert!(res == (42, 0));

        let res = mul_u128(6, 6, 7, 7);
        println!("mul_u128(??, ??) = {:?}", res);
        assert!(res == (42, 84));
    }
}

fn mul_u128() -> VM {
    let vm = VM::new();

    typedef!    ((vm) u128 = mu_int(128));

    funcsig!    ((vm) sig = (u128, u128) -> (u128));
    funcdecl!   ((vm) <sig> mul_u128);
    funcdef!    ((vm) <sig> mul_u128 VERSION mul_u128_v1);

    block!      ((vm, mul_u128_v1) blk_entry);
    ssa!        ((vm, mul_u128_v1) <u128> a);
    ssa!        ((vm, mul_u128_v1) <u128> b);

    // sum = Add %a %b
    ssa!        ((vm, mul_u128_v1) <u128> sum);
    inst!       ((vm, mul_u128_v1) blk_entry_mul_u128:
        sum = BINOP (BinOp::Mul) a b
    );

    inst!       ((vm, mul_u128_v1) blk_entry_ret:
        RET (sum)
    );

    define_block!   ((vm, mul_u128_v1) blk_entry(a, b) {
        blk_entry_mul_u128, blk_entry_ret
    });

    define_func_ver!((vm) mul_u128_v1 (entry: blk_entry) {blk_entry});

    vm
}

#[test]
fn test_udiv_u128() {
    let lib = testutil::compile_fnc("udiv_u128", &udiv_u128);

    unsafe {
        use self::extprim::u128::u128;

        let udiv_u128 : libloading::Symbol<unsafe extern fn(u64, u64, u64, u64) -> (u64, u64)> = lib.get(b"udiv_u128").unwrap();

        let res = udiv_u128(42, 0, 7, 0);
        println!("udiv_u128(42, 7) = {:?}", res);
        assert!(res == (6, 0));

        let res = udiv_u128(41, 42, 6, 7);
        let a = u128::from_parts(42, 41); // hi, lo
        let b = u128::from_parts(7, 6);
        let expect = a.wrapping_div(b);

        println!("udiv_u128(??, ??) = {:?}", res);
        assert!(expect.low64()  == res.0);
        assert!(expect.high64() == res.1)
    }
}

fn udiv_u128() -> VM {
    let vm = VM::new();

    typedef!    ((vm) u128 = mu_int(128));

    funcsig!    ((vm) sig = (u128, u128) -> (u128));
    funcdecl!   ((vm) <sig> udiv_u128);
    funcdef!    ((vm) <sig> udiv_u128 VERSION udiv_u128_v1);

    block!      ((vm, udiv_u128_v1) blk_entry);
    ssa!        ((vm, udiv_u128_v1) <u128> a);
    ssa!        ((vm, udiv_u128_v1) <u128> b);

    // sum = Add %a %b
    ssa!        ((vm, udiv_u128_v1) <u128> sum);
    inst!       ((vm, udiv_u128_v1) blk_entry_udiv_u128:
        sum = BINOP (BinOp::Udiv) a b
    );

    inst!       ((vm, udiv_u128_v1) blk_entry_ret:
        RET (sum)
    );

    define_block!   ((vm, udiv_u128_v1) blk_entry(a, b) {
        blk_entry_udiv_u128, blk_entry_ret
    });

    define_func_ver!((vm) udiv_u128_v1 (entry: blk_entry) {blk_entry});

qinsoon's avatar
qinsoon committed
171 172
    vm
}