test_floatingpoint.rs 26.6 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
// Copyright 2017 The Australian National University
qinsoon's avatar
qinsoon committed
2
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
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
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
7
//     http://www.apache.org/licenses/LICENSE-2.0
qinsoon's avatar
qinsoon committed
8
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
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.

qinsoon's avatar
qinsoon committed
15 16 17 18 19 20 21 22 23
extern crate mu;
extern crate log;
extern crate libloading;

use self::mu::ast::types::*;
use self::mu::ast::ir::*;
use self::mu::ast::inst::*;
use self::mu::ast::op::*;
use self::mu::vm::*;
24
use self::mu::linkutils;
25
use mu::utils::LinkedHashMap;
qinsoon's avatar
qinsoon committed
26 27

#[test]
28
fn test_double_add() {
29
    let lib = linkutils::aot::compile_fnc("double_add", &double_add);
qinsoon's avatar
qinsoon committed
30 31

    unsafe {
qinsoon's avatar
qinsoon committed
32 33
        let double_add: libloading::Symbol<unsafe extern "C" fn(f64, f64) -> f64> =
            lib.get(b"double_add").unwrap();
qinsoon's avatar
qinsoon committed
34

35 36 37
        let double_add_1_1 = double_add(1f64, 1f64);
        println!("double_add(1, 1) = {}", double_add_1_1);
        assert!(double_add_1_1 == 2f64);
qinsoon's avatar
qinsoon committed
38 39 40
    }
}

41
fn double_add() -> VM {
qinsoon's avatar
qinsoon committed
42 43
    let vm = VM::new();

44
    typedef!        ((vm) double = mu_double);
qinsoon's avatar
qinsoon committed
45

46 47 48
    funcsig!        ((vm) double_add_sig = (double, double) -> (double));
    funcdecl!       ((vm) <double_add_sig> double_add);
    funcdef!        ((vm) <double_add_sig> double_add VERSION double_add_v1);
qinsoon's avatar
qinsoon committed
49 50

    // %entry(<@double> %a, <@double> %b):
51 52 53
    block!          ((vm, double_add_v1) blk_entry);
    ssa!            ((vm, double_add_v1) <double> a);
    ssa!            ((vm, double_add_v1) <double> b);
qinsoon's avatar
qinsoon committed
54 55

    // %r = FADD %a %b
56 57
    ssa!            ((vm, double_add_v1) <double> r);
    inst!           ((vm, double_add_v1) blk_entry_fadd:
58 59
        r = BINOP (BinOp::FAdd) a b
    );
qinsoon's avatar
qinsoon committed
60 61

    // RET %r
62
    inst!           ((vm, double_add_v1) blk_entry_ret:
63 64
        RET (r)
    );
qinsoon's avatar
qinsoon committed
65

66
    define_block!   ((vm, double_add_v1) blk_entry(a, b) {
67 68
        blk_entry_fadd,
        blk_entry_ret
qinsoon's avatar
qinsoon committed
69 70
    });

71 72 73 74 75 76 77 78 79
    define_func_ver!((vm) double_add_v1(entry: blk_entry) {
        blk_entry
    });

    vm
}

#[test]
fn test_float_add() {
80
    let lib = linkutils::aot::compile_fnc("float_add", &float_add);
81 82

    unsafe {
qinsoon's avatar
qinsoon committed
83 84
        let float_add: libloading::Symbol<unsafe extern "C" fn(f32, f32) -> f32> =
            lib.get(b"float_add").unwrap();
85

qinsoon's avatar
qinsoon committed
86
        let float_add_1_1 = float_add(1f32, 1f32);
87
        println!("float_add(1, 1) = {}", float_add_1_1);
qinsoon's avatar
qinsoon committed
88
        assert!(float_add_1_1 == 2f32);
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
    }
}

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

    typedef!        ((vm) float = mu_float);

    funcsig!        ((vm) float_add_sig = (float, float) -> (float));
    funcdecl!       ((vm) <float_add_sig> float_add);
    funcdef!        ((vm) <float_add_sig> float_add VERSION float_add_v1);

    // %entry(<@float> %a, <@float> %b):
    block!          ((vm, float_add_v1) blk_entry);
    ssa!            ((vm, float_add_v1) <float> a);
    ssa!            ((vm, float_add_v1) <float> b);

    // %r = FADD %a %b
    ssa!            ((vm, float_add_v1) <float> r);
    inst!           ((vm, float_add_v1) blk_entry_fadd:
        r = BINOP (BinOp::FAdd) a b
    );

    // RET %r
    inst!           ((vm, float_add_v1) blk_entry_ret:
        RET (r)
    );

    define_block!   ((vm, float_add_v1) blk_entry(a, b) {
        blk_entry_fadd,
        blk_entry_ret
    });

    define_func_ver!((vm) float_add_v1(entry: blk_entry) {
123 124
        blk_entry
    });
qinsoon's avatar
qinsoon committed
125

qinsoon's avatar
qinsoon committed
126 127 128 129 130
    vm
}

#[test]
fn test_fp_ogt_branch() {
131
    let lib = linkutils::aot::compile_fnc("fp_ogt_branch", &fp_ogt_branch);
qinsoon's avatar
qinsoon committed
132 133

    unsafe {
qinsoon's avatar
qinsoon committed
134 135
        let fp_ogt: libloading::Symbol<unsafe extern "C" fn(f64, f64) -> u32> =
            lib.get(b"fp_ogt_branch").unwrap();
qinsoon's avatar
qinsoon committed
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

        let res = fp_ogt(-1f64, 0f64);
        println!("fp_ogt(-1, 0) = {}", res);
        assert!(res == 0);

        let res = fp_ogt(0f64, -1f64);
        println!("fp_ogt(0, -1) = {}", res);
        assert!(res == 1);

        let res = fp_ogt(-1f64, -1f64);
        println!("fp_ogt(-1, -1) = {}", res);
        assert!(res == 0);

        let res = fp_ogt(-1f64, -2f64);
        println!("fp_ogt(-1, -2) = {}", res);
        assert!(res == 1);

        let res = fp_ogt(-2f64, -1f64);
        println!("fp_ogt(-2, -1) = {}", res);
        assert!(res == 0);

        let res = fp_ogt(1f64, 2f64);
        println!("fp_ogt(1, 2) = {}", res);
        assert!(res == 0);

        let res = fp_ogt(2f64, 1f64);
        println!("fp_ogt(2, 1) = {}", res);
        assert!(res == 1);
    }
}

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

    typedef!    ((vm) double = mu_double);
    typedef!    ((vm) int32  = mu_int(32));
    typedef!    ((vm) int1   = mu_int(1));

    constdef!   ((vm) <int32> int32_0 = Constant::Int(0));
    constdef!   ((vm) <int32> int32_1 = Constant::Int(1));

    funcsig!    ((vm) sig = (double, double) -> (int32));
    funcdecl!   ((vm) <sig> fp_ogt_branch);
    funcdef!    ((vm) <sig> fp_ogt_branch VERSION fp_ogt_branch_v1);

    // blk entry
    block!      ((vm, fp_ogt_branch_v1) blk_entry);
    ssa!        ((vm, fp_ogt_branch_v1) <double> a);
    ssa!        ((vm, fp_ogt_branch_v1) <double> b);

    ssa!        ((vm, fp_ogt_branch_v1) <int1> cond);
    inst!       ((vm, fp_ogt_branch_v1) blk_entry_cmp:
        cond = CMPOP (CmpOp::FOGT) a b
    );

    block!      ((vm, fp_ogt_branch_v1) blk_ret1);
    consta!     ((vm, fp_ogt_branch_v1) int32_1_local = int32_1);
    block!      ((vm, fp_ogt_branch_v1) blk_ret0);
    consta!     ((vm, fp_ogt_branch_v1) int32_0_local = int32_0);

    inst!       ((vm, fp_ogt_branch_v1) blk_entry_branch:
        BRANCH2 (cond, int32_1_local, int32_0_local)
            IF (OP 0)
            THEN blk_ret1 (vec![1]) WITH 0.6f32,
            ELSE blk_ret0 (vec![2])
    );

    define_block! ((vm, fp_ogt_branch_v1) blk_entry(a, b){
        blk_entry_cmp, blk_entry_branch
    });

    // blk_ret1
    ssa!        ((vm, fp_ogt_branch_v1) <int32> blk_ret1_res);
    inst!       ((vm, fp_ogt_branch_v1) blk_ret1_inst:
        RET (blk_ret1_res)
    );

    define_block! ((vm, fp_ogt_branch_v1) blk_ret1(blk_ret1_res){
        blk_ret1_inst
    });

    // blk_ret0
    ssa!        ((vm, fp_ogt_branch_v1) <int32> blk_ret0_res);
    inst!       ((vm, fp_ogt_branch_v1) blk_ret0_inst:
        RET (blk_ret0_res)
    );

    define_block! ((vm, fp_ogt_branch_v1) blk_ret0(blk_ret0_res){
        blk_ret0_inst
    });

    define_func_ver!((vm) fp_ogt_branch_v1 (entry: blk_entry) {
        blk_entry, blk_ret1, blk_ret0
    });

    vm
}

#[test]
fn test_sitofp() {
236
    let lib = linkutils::aot::compile_fnc("sitofp", &sitofp);
qinsoon's avatar
qinsoon committed
237 238

    unsafe {
qinsoon's avatar
qinsoon committed
239 240
        let sitofp: libloading::Symbol<unsafe extern "C" fn(i64) -> f64> =
            lib.get(b"sitofp").unwrap();
qinsoon's avatar
qinsoon committed
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

        let res = sitofp(-1i64);
        println!("sitofp(-1) = {}", res);
        assert!(res == -1f64);

        let res = sitofp(0i64);
        println!("sitofp(0) = {}", res);
        assert!(res == 0f64);

        let res = sitofp(1i64);
        println!("sitofp(1) = {}", res);
        assert!(res == 1f64);
    }
}

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

    typedef!    ((vm) int64 = mu_int(64));
    typedef!    ((vm) double = mu_double);

    funcsig!    ((vm) sig = (int64) -> (double));
    funcdecl!   ((vm) <sig> sitofp);
    funcdef!    ((vm) <sig> sitofp VERSION sitofp_v1);

    // blk entry
    block!      ((vm, sitofp_v1) blk_entry);
    ssa!        ((vm, sitofp_v1) <int64> x);

    ssa!        ((vm, sitofp_v1) <double> res);
    inst!       ((vm, sitofp_v1) blk_entry_conv:
        res = CONVOP (ConvOp::SITOFP) <int64 double> x
    );

    inst!       ((vm, sitofp_v1) blk_entry_ret:
        RET (res)
    );

    define_block!((vm, sitofp_v1) blk_entry(x){
        blk_entry_conv, blk_entry_ret
    });

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

qinsoon's avatar
qinsoon committed
285 286 287
    vm
}

288
#[test]
289
fn test_ui64tofp() {
290
    let lib = linkutils::aot::compile_fnc("ui64tofp", &ui64tofp);
291 292

    unsafe {
qinsoon's avatar
qinsoon committed
293 294
        let ui64tofp: libloading::Symbol<unsafe extern "C" fn(u64) -> f64> =
            lib.get(b"ui64tofp").unwrap();
295

296 297
        let res = ui64tofp(0u64);
        println!("ui64tofp(0) = {}", res);
298 299
        assert!(res == 0f64);

300 301
        let res = ui64tofp(1u64);
        println!("ui64tofp(1) = {}", res);
302 303 304 305
        assert!(res == 1f64);
    }
}

306
fn ui64tofp() -> VM {
307 308 309 310 311 312
    let vm = VM::new();

    typedef!    ((vm) int64 = mu_int(64));
    typedef!    ((vm) double = mu_double);

    funcsig!    ((vm) sig = (int64) -> (double));
313 314
    funcdecl!   ((vm) <sig> ui64tofp);
    funcdef!    ((vm) <sig> ui64tofp VERSION ui64tofp_v1);
315 316

    // blk entry
317 318
    block!      ((vm, ui64tofp_v1) blk_entry);
    ssa!        ((vm, ui64tofp_v1) <int64> x);
319

320 321
    ssa!        ((vm, ui64tofp_v1) <double> res);
    inst!       ((vm, ui64tofp_v1) blk_entry_conv:
322 323 324
        res = CONVOP (ConvOp::UITOFP) <int64 double> x
    );

325
    inst!       ((vm, ui64tofp_v1) blk_entry_ret:
326 327 328
        RET (res)
    );

329
    define_block!((vm, ui64tofp_v1) blk_entry(x){
330 331 332
        blk_entry_conv, blk_entry_ret
    });

333
    define_func_ver!((vm) ui64tofp_v1 (entry: blk_entry) {blk_entry});
qinsoon's avatar
qinsoon committed
334 335 336 337 338 339

    vm
}

#[test]
fn test_ui64tofp_float() {
340
    let lib = linkutils::aot::compile_fnc("ui64tofp_float", &ui64tofp_float);
qinsoon's avatar
qinsoon committed
341 342

    unsafe {
qinsoon's avatar
qinsoon committed
343 344
        let ui64tofp_float: libloading::Symbol<unsafe extern "C" fn(u64) -> f32> =
            lib.get(b"ui64tofp_float").unwrap();
qinsoon's avatar
qinsoon committed
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

        let res = ui64tofp_float(0u64);
        println!("ui64tofp_float(0) = {}", res);
        assert!(res == 0f32);

        let res = ui64tofp_float(1u64);
        println!("ui64tofp_float(1) = {}", res);
        assert!(res == 1f32);
    }
}

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

    typedef!    ((vm) int64 = mu_int(64));
    typedef!    ((vm) float = mu_float);

    funcsig!    ((vm) sig = (int64) -> (float));
    funcdecl!   ((vm) <sig> ui64tofp_float);
    funcdef!    ((vm) <sig> ui64tofp_float VERSION ui64tofp_float_v1);

    // blk entry
    block!      ((vm, ui64tofp_float_v1) blk_entry);
    ssa!        ((vm, ui64tofp_float_v1) <int64> x);

    ssa!        ((vm, ui64tofp_float_v1) <float> res);
    inst!       ((vm, ui64tofp_float_v1) blk_entry_conv:
        res = CONVOP (ConvOp::UITOFP) <int64 float> x
    );

    inst!       ((vm, ui64tofp_float_v1) blk_entry_ret:
        RET (res)
    );

    define_block!((vm, ui64tofp_float_v1) blk_entry(x){
        blk_entry_conv, blk_entry_ret
    });

    define_func_ver!((vm) ui64tofp_float_v1 (entry: blk_entry) {blk_entry});
384 385 386 387 388 389

    vm
}

#[test]
fn test_ui32tofp() {
390
    let lib = linkutils::aot::compile_fnc("ui32tofp", &ui32tofp);
391 392

    unsafe {
qinsoon's avatar
qinsoon committed
393 394
        let ui32tofp: libloading::Symbol<unsafe extern "C" fn(u32) -> f64> =
            lib.get(b"ui32tofp").unwrap();
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

        let res = ui32tofp(0u32);
        println!("ui32tofp(0) = {}", res);
        assert!(res == 0f64);

        let res = ui32tofp(1u32);
        println!("ui32tofp(1) = {}", res);
        assert!(res == 1f64);
    }
}

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

    typedef!    ((vm) int32 = mu_int(32));
    typedef!    ((vm) double = mu_double);

    funcsig!    ((vm) sig = (int32) -> (double));
    funcdecl!   ((vm) <sig> ui32tofp);
    funcdef!    ((vm) <sig> ui32tofp VERSION ui32tofp_v1);

    // blk entry
    block!      ((vm, ui32tofp_v1) blk_entry);
    ssa!        ((vm, ui32tofp_v1) <int32> x);

    ssa!        ((vm, ui32tofp_v1) <double> res);
    inst!       ((vm, ui32tofp_v1) blk_entry_conv:
        res = CONVOP (ConvOp::UITOFP) <int32 double> x
    );

    inst!       ((vm, ui32tofp_v1) blk_entry_ret:
        RET (res)
    );

    define_block!((vm, ui32tofp_v1) blk_entry(x){
        blk_entry_conv, blk_entry_ret
    });

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

    vm
}

#[test]
fn test_ui16tofp() {
440
    let lib = linkutils::aot::compile_fnc("ui16tofp", &ui16tofp);
441 442

    unsafe {
qinsoon's avatar
qinsoon committed
443 444
        let ui16tofp: libloading::Symbol<unsafe extern "C" fn(u16) -> f64> =
            lib.get(b"ui16tofp").unwrap();
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

        let res = ui16tofp(0u16);
        println!("ui16tofp(0) = {}", res);
        assert!(res == 0f64);

        let res = ui16tofp(1u16);
        println!("ui16tofp(1) = {}", res);
        assert!(res == 1f64);
    }
}

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

    typedef!    ((vm) int16 = mu_int(16));
    typedef!    ((vm) double = mu_double);

    funcsig!    ((vm) sig = (int16) -> (double));
    funcdecl!   ((vm) <sig> ui16tofp);
    funcdef!    ((vm) <sig> ui16tofp VERSION ui16tofp_v1);

    // blk entry
    block!      ((vm, ui16tofp_v1) blk_entry);
    ssa!        ((vm, ui16tofp_v1) <int16> x);

    ssa!        ((vm, ui16tofp_v1) <double> res);
    inst!       ((vm, ui16tofp_v1) blk_entry_conv:
        res = CONVOP (ConvOp::UITOFP) <int16 double> x
    );

    inst!       ((vm, ui16tofp_v1) blk_entry_ret:
        RET (res)
    );

    define_block!((vm, ui16tofp_v1) blk_entry(x){
        blk_entry_conv, blk_entry_ret
    });

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

    vm
}

#[test]
fn test_ui8tofp() {
490
    let lib = linkutils::aot::compile_fnc("ui8tofp", &ui8tofp);
491 492

    unsafe {
qinsoon's avatar
qinsoon committed
493 494
        let ui8tofp: libloading::Symbol<unsafe extern "C" fn(u8) -> f64> =
            lib.get(b"ui8tofp").unwrap();
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

        let res = ui8tofp(0u8);
        println!("ui8tofp(0) = {}", res);
        assert!(res == 0f64);

        let res = ui8tofp(1u8);
        println!("ui8tofp(1) = {}", res);
        assert!(res == 1f64);
    }
}

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

    typedef!    ((vm) int8 = mu_int(8));
    typedef!    ((vm) double = mu_double);

    funcsig!    ((vm) sig = (int8) -> (double));
    funcdecl!   ((vm) <sig> ui8tofp);
    funcdef!    ((vm) <sig> ui8tofp VERSION ui8tofp_v1);

    // blk entry
    block!      ((vm, ui8tofp_v1) blk_entry);
    ssa!        ((vm, ui8tofp_v1) <int8> x);

    ssa!        ((vm, ui8tofp_v1) <double> res);
    inst!       ((vm, ui8tofp_v1) blk_entry_conv:
        res = CONVOP (ConvOp::UITOFP) <int8 double> x
    );

    inst!       ((vm, ui8tofp_v1) blk_entry_ret:
        RET (res)
    );

    define_block!((vm, ui8tofp_v1) blk_entry(x){
        blk_entry_conv, blk_entry_ret
    });

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

    vm
}

#[test]
fn test_fptoui64() {
540
    let lib = linkutils::aot::compile_fnc("fptoui64", &fptoui64);
541 542

    unsafe {
qinsoon's avatar
qinsoon committed
543 544
        let fptoui64: libloading::Symbol<unsafe extern "C" fn(f64) -> u64> =
            lib.get(b"fptoui64").unwrap();
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

        let res = fptoui64(0f64);
        println!("fptoui64(0) = {}", res);
        assert!(res == 0u64);

        let res = fptoui64(1f64);
        println!("fptoui64(1) = {}", res);
        assert!(res == 1u64);
    }
}

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

    typedef!    ((vm) int64 = mu_int(64));
    typedef!    ((vm) double = mu_double);

    funcsig!    ((vm) sig = (double) -> (int64));
    funcdecl!   ((vm) <sig> fptoui64);
    funcdef!    ((vm) <sig> fptoui64 VERSION fptoui64_v1);

    // blk entry
    block!      ((vm, fptoui64_v1) blk_entry);
    ssa!        ((vm, fptoui64_v1) <double> x);

    ssa!        ((vm, fptoui64_v1) <int64> res);
    inst!       ((vm, fptoui64_v1) blk_entry_conv:
        res = CONVOP (ConvOp::FPTOUI) <double int64> x
    );

    inst!       ((vm, fptoui64_v1) blk_entry_ret:
        RET (res)
    );

    define_block!((vm, fptoui64_v1) blk_entry(x){
        blk_entry_conv, blk_entry_ret
    });

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

    vm
}

#[test]
fn test_fptoui32() {
590
    let lib = linkutils::aot::compile_fnc("fptoui32", &fptoui32);
591 592

    unsafe {
qinsoon's avatar
qinsoon committed
593 594
        let fptoui32: libloading::Symbol<unsafe extern "C" fn(f64) -> u32> =
            lib.get(b"fptoui32").unwrap();
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

        let res = fptoui32(0f64);
        println!("fptoui32(0) = {}", res);
        assert!(res == 0u32);

        let res = fptoui32(1f64);
        println!("fptoui32(1) = {}", res);
        assert!(res == 1u32);
    }
}

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

    typedef!    ((vm) int32 = mu_int(32));
    typedef!    ((vm) double = mu_double);

    funcsig!    ((vm) sig = (double) -> (int32));
    funcdecl!   ((vm) <sig> fptoui32);
    funcdef!    ((vm) <sig> fptoui32 VERSION fptoui32_v1);

    // blk entry
    block!      ((vm, fptoui32_v1) blk_entry);
    ssa!        ((vm, fptoui32_v1) <double> x);

    ssa!        ((vm, fptoui32_v1) <int32> res);
    inst!       ((vm, fptoui32_v1) blk_entry_conv:
        res = CONVOP (ConvOp::FPTOUI) <double int32> x
    );

    inst!       ((vm, fptoui32_v1) blk_entry_ret:
        RET (res)
    );

    define_block!((vm, fptoui32_v1) blk_entry(x){
        blk_entry_conv, blk_entry_ret
    });

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

    vm
}

#[test]
fn test_fptoui16() {
640
    let lib = linkutils::aot::compile_fnc("fptoui16", &fptoui16);
641 642

    unsafe {
qinsoon's avatar
qinsoon committed
643 644
        let fptoui16: libloading::Symbol<unsafe extern "C" fn(f64) -> u16> =
            lib.get(b"fptoui16").unwrap();
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

        let res = fptoui16(0f64);
        println!("fptoui16(0) = {}", res);
        assert!(res == 0u16);

        let res = fptoui16(1f64);
        println!("fptoui16(1) = {}", res);
        assert!(res == 1u16);
    }
}

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

    typedef!    ((vm) int16 = mu_int(16));
    typedef!    ((vm) double = mu_double);

    funcsig!    ((vm) sig = (double) -> (int16));
    funcdecl!   ((vm) <sig> fptoui16);
    funcdef!    ((vm) <sig> fptoui16 VERSION fptoui16_v1);

    // blk entry
    block!      ((vm, fptoui16_v1) blk_entry);
    ssa!        ((vm, fptoui16_v1) <double> x);

    ssa!        ((vm, fptoui16_v1) <int16> res);
    inst!       ((vm, fptoui16_v1) blk_entry_conv:
        res = CONVOP (ConvOp::FPTOUI) <double int16> x
    );

    inst!       ((vm, fptoui16_v1) blk_entry_ret:
        RET (res)
    );

    define_block!((vm, fptoui16_v1) blk_entry(x){
        blk_entry_conv, blk_entry_ret
    });

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

    vm
}

#[test]
fn test_fptoui8() {
690
    let lib = linkutils::aot::compile_fnc("fptoui8", &fptoui8);
691 692

    unsafe {
qinsoon's avatar
qinsoon committed
693 694
        let fptoui8: libloading::Symbol<unsafe extern "C" fn(f64) -> u8> =
            lib.get(b"fptoui8").unwrap();
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

        let res = fptoui8(0f64);
        println!("fptoui8(0) = {}", res);
        assert!(res == 0u8);

        let res = fptoui8(1f64);
        println!("fptoui8(1) = {}", res);
        assert!(res == 1u8);
    }
}

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

    typedef!    ((vm) int8 = mu_int(8));
    typedef!    ((vm) double = mu_double);

    funcsig!    ((vm) sig = (double) -> (int8));
    funcdecl!   ((vm) <sig> fptoui8);
    funcdef!    ((vm) <sig> fptoui8 VERSION fptoui8_v1);

    // blk entry
    block!      ((vm, fptoui8_v1) blk_entry);
    ssa!        ((vm, fptoui8_v1) <double> x);

    ssa!        ((vm, fptoui8_v1) <int8> res);
    inst!       ((vm, fptoui8_v1) blk_entry_conv:
        res = CONVOP (ConvOp::FPTOUI) <double int8> x
    );

    inst!       ((vm, fptoui8_v1) blk_entry_ret:
        RET (res)
    );

    define_block!((vm, fptoui8_v1) blk_entry(x){
        blk_entry_conv, blk_entry_ret
    });

    define_func_ver!((vm) fptoui8_v1 (entry: blk_entry) {blk_entry});
734 735 736 737

    vm
}

qinsoon's avatar
qinsoon committed
738 739 740 741
#[test]
fn test_fp_arraysum() {
    use std::os::raw::c_double;

742
    let lib = linkutils::aot::compile_fnc("fp_arraysum", &fp_arraysum);
qinsoon's avatar
qinsoon committed
743 744

    unsafe {
745 746
        let fp_arraysum: libloading::Symbol<unsafe extern "C" fn(*const c_double, u64) -> f64> =
            lib.get(b"fp_arraysum").unwrap();
qinsoon's avatar
qinsoon committed
747 748 749 750 751 752 753 754 755 756 757 758 759

        let array: [f64; 10] = [
            0f64,
            0.1f64,
            0.2f64,
            0.3f64,
            0.4f64,
            0.5f64,
            0.6f64,
            0.7f64,
            0.8f64,
            0.9f64,
        ];
qinsoon's avatar
qinsoon committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773
        let c_array = array.as_ptr() as *const c_double;

        let res = fp_arraysum(c_array, 10);
        println!("fp_arraysum(array, 10) = {}", res);
        assert!(res == 4.5f64);
    }
}

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

    typedef!    ((vm) int64  = mu_int(64));
    typedef!    ((vm) int1   = mu_int(1));
    typedef!    ((vm) double = mu_double);
774
    typedef!    ((vm) hybrid = mu_hybrid()(double));
qinsoon's avatar
qinsoon committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
    typedef!    ((vm) uptr_hybrid = mu_uptr(hybrid));
    typedef!    ((vm) uptr_double = mu_uptr(double));

    constdef!   ((vm) <int64> int64_0   = Constant::Int(0));
    constdef!   ((vm) <int64> int64_1   = Constant::Int(1));
    constdef!   ((vm) <double> double_0 = Constant::Double(0f64));

    funcsig!    ((vm) sig = (uptr_hybrid, int64) -> (double));
    funcdecl!   ((vm) <sig> fp_arraysum);
    funcdef!    ((vm) <sig> fp_arraysum VERSION fp_arraysum_v1);

    // blk entry
    block!      ((vm, fp_arraysum_v1) blk_entry);
    ssa!        ((vm, fp_arraysum_v1) <uptr_hybrid> blk_entry_arr);
    ssa!        ((vm, fp_arraysum_v1) <int64> blk_entry_sz);

    block!      ((vm, fp_arraysum_v1) blk1);
    consta!     ((vm, fp_arraysum_v1) int64_0_local  = int64_0);
    consta!     ((vm, fp_arraysum_v1) int64_1_local  = int64_1);
    consta!     ((vm, fp_arraysum_v1) double_0_local = double_0);
    inst!       ((vm, fp_arraysum_v1) blk_entry_branch:
        BRANCH blk1 (blk_entry_arr, double_0_local, int64_0_local, blk_entry_sz)
    );

qinsoon's avatar
qinsoon committed
799 800 801
    define_block!   ((vm, fp_arraysum_v1) blk_entry(blk_entry_arr, blk_entry_sz) {
        blk_entry_branch
    });
qinsoon's avatar
qinsoon committed
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875

    // blk1
    ssa!        ((vm, fp_arraysum_v1) <uptr_hybrid> blk1_arr);
    ssa!        ((vm, fp_arraysum_v1) <double> blk1_sum);
    ssa!        ((vm, fp_arraysum_v1) <int64> blk1_v1);
    ssa!        ((vm, fp_arraysum_v1) <int64> blk1_v2);

    ssa!        ((vm, fp_arraysum_v1) <int1> blk1_rtn);
    inst!       ((vm, fp_arraysum_v1) blk1_sge:
        blk1_rtn = CMPOP (CmpOp::SGE) blk1_v1 blk1_v2
    );

    block!      ((vm, fp_arraysum_v1) blk2);
    block!      ((vm, fp_arraysum_v1) blk3);
    inst!       ((vm, fp_arraysum_v1) blk1_branch2:
        BRANCH2 (blk1_rtn, blk1_sum, blk1_v2, blk1_v1, blk1_arr)
        IF (OP 0)
        THEN blk3 (vec![1]) WITH 0.2f32,
        ELSE blk2 (vec![2, 3, 4, 1])
    );

    define_block!   ((vm, fp_arraysum_v1) blk1(blk1_arr, blk1_sum, blk1_v1, blk1_v2) {
        blk1_sge, blk1_branch2
    });

    // blk2
    ssa!        ((vm, fp_arraysum_v1) <int64> blk2_v4);
    ssa!        ((vm, fp_arraysum_v1) <int64> blk2_next);
    ssa!        ((vm, fp_arraysum_v1) <uptr_hybrid> blk2_arr);
    ssa!        ((vm, fp_arraysum_v1) <double> blk2_sum);

    ssa!        ((vm, fp_arraysum_v1) <int64> blk2_v5);
    inst!       ((vm, fp_arraysum_v1) blk2_add:
        blk2_v5 = BINOP (BinOp::Add) blk2_next int64_1_local
    );

    ssa!        ((vm, fp_arraysum_v1) <uptr_double> blk2_rtn2);
    inst!       ((vm, fp_arraysum_v1) blk2_getvarpart:
        blk2_rtn2 = GETVARPARTIREF blk2_arr (is_ptr: true)
    );

    ssa!        ((vm, fp_arraysum_v1) <uptr_double> blk2_rtn3);
    inst!       ((vm, fp_arraysum_v1) blk2_shiftiref:
        blk2_rtn3 = SHIFTIREF blk2_rtn2 blk2_next (is_ptr: true)
    );

    ssa!        ((vm, fp_arraysum_v1) <double> blk2_v7);
    inst!       ((vm, fp_arraysum_v1) blk2_load:
        blk2_v7 = LOAD blk2_rtn3 (is_ptr: true, order: MemoryOrder::NotAtomic)
    );

    ssa!        ((vm, fp_arraysum_v1) <double> blk2_sum2);
    inst!       ((vm, fp_arraysum_v1) blk2_fadd:
        blk2_sum2 = BINOP (BinOp::FAdd) blk2_sum blk2_v7
    );

    inst!       ((vm, fp_arraysum_v1) blk2_branch:
        BRANCH blk1 (blk2_arr, blk2_sum2, blk2_v5, blk2_v4)
    );

    define_block!   ((vm, fp_arraysum_v1) blk2(blk2_v4, blk2_next, blk2_arr, blk2_sum) {
        blk2_add, blk2_getvarpart, blk2_shiftiref, blk2_load, blk2_fadd, blk2_branch
    });

    // blk3
    ssa!        ((vm, fp_arraysum_v1) <double> blk3_v8);
    inst!       ((vm, fp_arraysum_v1) blk3_ret:
        RET (blk3_v8)
    );

    define_block!   ((vm, fp_arraysum_v1) blk3(blk3_v8) {blk3_ret});

    define_func_ver!    ((vm) fp_arraysum_v1 (entry: blk_entry) {blk_entry, blk1, blk2, blk3});

qinsoon's avatar
qinsoon committed
876
    vm
877
}
qinsoon's avatar
qinsoon committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987

#[test]
fn test_double_to_float() {
    let lib = linkutils::aot::compile_fnc("double_to_float", &double_to_float);

    unsafe {
        use std::f64;

        let double_to_float: libloading::Symbol<unsafe extern "C" fn(f64) -> f32> =
            lib.get(b"double_to_float").unwrap();

        let res = double_to_float(0f64);
        println!("double_fo_float(0) = {}", res);
        assert!(res == 0f32);

        let res = double_to_float(1f64);
        println!("double_fo_float(1) = {}", res);
        assert!(res == 1f32);

        let res = double_to_float(f64::MAX);
        println!("double_to_float(f64::MAX) = {}", res);
        assert!(res.is_infinite());
    }
}

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

    typedef!    ((vm) double = mu_double);
    typedef!    ((vm) float = mu_float);

    funcsig!    ((vm) sig = (double) -> (float));
    funcdecl!   ((vm) <sig> double_to_float);
    funcdef!    ((vm) <sig> double_to_float VERSION double_to_float_v1);

    // blk entry
    block!      ((vm, double_to_float_v1) blk_entry);
    ssa!        ((vm, double_to_float_v1) <double> d);
    ssa!        ((vm, double_to_float_v1) <float> f);

    inst!       ((vm, double_to_float_v1) blk_entry_fptrunc:
        f = CONVOP (ConvOp::FPTRUNC) <double float> d
    );

    inst!       ((vm, double_to_float_v1) blk_entry_ret:
        RET (f)
    );

    define_block!((vm, double_to_float_v1) blk_entry(d) {
        blk_entry_fptrunc, blk_entry_ret
    });

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

    vm
}

#[test]
fn test_float_to_double() {
    let lib = linkutils::aot::compile_fnc("float_to_double", &float_to_double);

    unsafe {
        let float_to_double: libloading::Symbol<unsafe extern "C" fn(f32) -> f64> =
            lib.get(b"float_to_double").unwrap();

        let res = float_to_double(0f32);
        println!("float_to_double(0) = {}", 0);
        assert!(res == 0f64);

        let res = float_to_double(1f32);
        println!("float_to_double(1) = {}", 0);
        assert!(res == 1f64);
    }
}

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

    typedef!    ((vm) double = mu_double);
    typedef!    ((vm) float = mu_float);

    funcsig!    ((vm) sig = (float) -> (double));
    funcdecl!   ((vm) <sig> float_to_double);
    funcdef!    ((vm) <sig> float_to_double VERSION float_to_double_v1);

    // blk entry
    block!      ((vm, float_to_double_v1) blk_entry);
    ssa!        ((vm, float_to_double_v1) <double> d);
    ssa!        ((vm, float_to_double_v1) <float> f);

    inst!       ((vm, float_to_double_v1) blk_entry_fpext:
        d = CONVOP (ConvOp::FPEXT) <double float> f
    );

    inst!       ((vm, float_to_double_v1) blk_entry_ret:
        RET (d)
    );

    define_block!((vm, float_to_double_v1) blk_entry(f) {
        blk_entry_fpext, blk_entry_ret
    });

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

    vm
}