test_convop.rs 5.58 KB
Newer Older
1
// Copyright 2017 The Australian National University
qinsoon's avatar
qinsoon committed
2
//
3 4 5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
qinsoon's avatar
qinsoon committed
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
qinsoon's avatar
qinsoon committed
8
//
9 10 11 12 13 14
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
extern crate libloading;
16 17
extern crate log;
extern crate mu;
18 19

use self::mu::ast::inst::*;
20
use self::mu::ast::ir::*;
21
use self::mu::ast::op::*;
22
use self::mu::ast::types::*;
23 24 25
use self::mu::vm::*;
use mu::utils::LinkedHashMap;

26
use self::mu::compiler::*;
27 28
use self::mu::linkutils::aot;
use std::sync::Arc;
29

30
use std::f32;
31

32 33 34
#[test]
fn test_truncate_then_call() {
    build_and_run_test!(truncate_then_call AND dummy_call, truncate_then_call_test1);
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
}

fn truncate_then_call() -> VM {
    let vm = VM::new_with_opts("init_mu --disable-inline");

    typedef! ((vm) u64 = mu_int(64));
    typedef! ((vm) u32 = mu_int(32));

    funcsig! ((vm) dummy_call_sig = (u32) -> (u32));
    funcdecl!((vm) <dummy_call_sig> dummy_call);
    {
        // --- dummy call ---
        funcdef! ((vm) <dummy_call_sig> dummy_call VERSION dummy_call_v1);

        // entry
        block! ((vm, dummy_call_v1) blk_entry);
        ssa!   ((vm, dummy_call_v1) <u32> x);

        inst!  ((vm, dummy_call_v1) ret:
            RET (x)
        );

        define_block!((vm, dummy_call_v1) blk_entry(x) {
            ret
        });

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

    {
        // --- truncate_then_call ---
        typedef! ((vm) funcref_to_dummy = mu_funcref(dummy_call_sig));
        constdef!((vm) <funcref_to_dummy> funcref_dummy = Constant::FuncRef(dummy_call));

        funcsig! ((vm) sig = (u64) -> (u32));
        funcdecl!((vm) <sig> truncate_then_call);
        funcdef! ((vm) <sig> truncate_then_call VERSION truncate_then_call_v1);

        // entry
        block!((vm, truncate_then_call_v1) blk_entry);
        ssa!  ((vm, truncate_then_call_v1) <u64> arg);

        // %arg_u32 = TRUNC <u64 u32> arg
        ssa! ((vm, truncate_then_call_v1) <u32> arg_u32);
        inst!((vm, truncate_then_call_v1) blk_entry_truncate:
            arg_u32 = CONVOP (ConvOp::TRUNC) <u64 u32> arg
        );

        // %ret = CALL dummy_call (arg_u32)
        ssa!    ((vm, truncate_then_call_v1) <u32> res);
        consta! ((vm, truncate_then_call_v1) funcref_dummy_local = funcref_dummy);
        inst!   ((vm, truncate_then_call_v1) blk_entry_call:
            res = EXPRCALL (CallConvention::Mu, is_abort: false) funcref_dummy_local (arg_u32)
        );

        inst!((vm, truncate_then_call_v1) blk_entry_ret:
93
            RET (res)
94 95 96 97 98 99 100 101 102 103 104
        );

        define_block!((vm, truncate_then_call_v1) blk_entry(arg) {
            blk_entry_truncate,
            blk_entry_call,
            blk_entry_ret
        });

        define_func_ver!((vm) truncate_then_call_v1 (entry: blk_entry) {
            blk_entry
        });
105 106 107 108 109 110 111 112

        emit_test! ((vm)
            truncate_then_call, truncate_then_call_test1, truncate_then_call_test1_v1,
            Int RET Int,
            EQ,
            sig,
            u64(1u64) RET u32(1u64),
        );
113 114 115
    }

    vm
qinsoon's avatar
qinsoon committed
116
}
qinsoon's avatar
qinsoon committed
117 118 119

#[test]
fn test_bitcast_f32_to_u32() {
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    //    let lib = linkutils::aot::compile_fnc("bitcast_f32_to_u32", &bitcast_f32_to_u32);
    //
    //    unsafe {
    //        use std::f32;
    //
    //        let bitcast_f32_to_u32: libloading::Symbol<unsafe extern "C" fn(f32) -> u32> =
    //            lib.get(b"bitcast_f32_to_u32").unwrap();
    //
    //        let res = bitcast_f32_to_u32(f32::MAX);
    //        println!("bitcast_f32_to_u32(f32::MAX) = {}", res);
    //        assert!(res == 2139095039u32);
    //
    //        let res = bitcast_f32_to_u32(3.1415926f32);
    //        println!("bitcast_f32_to_u32(PI) = {}", res);
    //        assert!(res == 1078530010u32);
    //    }
    build_and_run_test!(bitcast_f32_to_u32, bitcast_f32_to_u32_test1);
    build_and_run_test!(bitcast_f32_to_u32, bitcast_f32_to_u32_test2);
qinsoon's avatar
qinsoon committed
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
}

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

    typedef!    ((vm) float = mu_float);
    typedef!    ((vm) u32 = mu_int(32));

    funcsig!    ((vm) sig = (float) -> (u32));
    funcdecl!   ((vm) <sig> bitcast_f32_to_u32);
    funcdef!    ((vm) <sig> bitcast_f32_to_u32 VERSION bitcast_f32_to_u32_v1);

    // blk entry
    block!      ((vm, bitcast_f32_to_u32_v1) blk_entry);
    ssa!        ((vm, bitcast_f32_to_u32_v1) <float> f);
    ssa!        ((vm, bitcast_f32_to_u32_v1) <u32> i);

    inst!       ((vm, bitcast_f32_to_u32_v1) blk_entry_bitcast:
        i = CONVOP (ConvOp::BITCAST) <float u32> f
    );

    inst!       ((vm, bitcast_f32_to_u32_v1) blk_entry_ret:
        RET (i)
    );

    define_block!((vm, bitcast_f32_to_u32_v1) blk_entry(f) {
        blk_entry_bitcast, blk_entry_ret
    });

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

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    emit_test! ((vm)
        bitcast_f32_to_u32, bitcast_f32_to_u32_test1, bitcast_f32_to_u32_test1_v1,
        Float RET Int,
        EQ,
        sig,
        float(f32::MAX) RET u32(2139095039u32 as u64),
    );
    emit_test! ((vm)
        bitcast_f32_to_u32, bitcast_f32_to_u32_test2, bitcast_f32_to_u32_test2_v1,
        Float RET Int,
        EQ,
        sig,
        float(3.1415926f32) RET u32(1078530010u32 as u64),
    );

qinsoon's avatar
qinsoon committed
186
    vm
187
}