test_floatingpoint.rs 21.4 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2 3 4 5 6 7 8 9
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::*;
qinsoon's avatar
qinsoon committed
10
use self::mu::testutil;
11
use mu::utils::LinkedHashMap;
qinsoon's avatar
qinsoon committed
12 13 14 15

use std::sync::RwLock;

#[test]
16 17
fn test_double_add() {
    let lib = testutil::compile_fnc("double_add", &double_add);
qinsoon's avatar
qinsoon committed
18 19

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

22 23 24
        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
25 26 27
    }
}

28
fn double_add() -> VM {
qinsoon's avatar
qinsoon committed
29 30
    let vm = VM::new();

31
    typedef!        ((vm) double = mu_double);
qinsoon's avatar
qinsoon committed
32

33 34 35
    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
36 37

    // %entry(<@double> %a, <@double> %b):
38 39 40
    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
41 42

    // %r = FADD %a %b
43 44
    ssa!            ((vm, double_add_v1) <double> r);
    inst!           ((vm, double_add_v1) blk_entry_fadd:
45 46
        r = BINOP (BinOp::FAdd) a b
    );
qinsoon's avatar
qinsoon committed
47 48

    // RET %r
49
    inst!           ((vm, double_add_v1) blk_entry_ret:
50 51
        RET (r)
    );
qinsoon's avatar
qinsoon committed
52

53
    define_block!   ((vm, double_add_v1) blk_entry(a, b) {
54 55
        blk_entry_fadd,
        blk_entry_ret
qinsoon's avatar
qinsoon committed
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    define_func_ver!((vm) double_add_v1(entry: blk_entry) {
        blk_entry
    });

    vm
}

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

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

        let float_add_1_1 = float_add(1f64, 1f64);
        println!("float_add(1, 1) = {}", float_add_1_1);
        assert!(float_add_1_1 == 2f64);
    }
}

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) {
109 110
        blk_entry
    });
qinsoon's avatar
qinsoon committed
111

qinsoon's avatar
qinsoon committed
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 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 236 237 238 239 240 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
    vm
}

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

    unsafe {
        let fp_ogt : libloading::Symbol<unsafe extern fn(f64, f64) -> u32> = lib.get(b"fp_ogt_branch").unwrap();

        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() {
    let lib = testutil::compile_fnc("sitofp", &sitofp);

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

        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
269 270 271
    vm
}

272
#[test]
273 274
fn test_ui64tofp() {
    let lib = testutil::compile_fnc("ui64tofp", &ui64tofp);
275 276

    unsafe {
277
        let ui64tofp : libloading::Symbol<unsafe extern fn(u64) -> f64> = lib.get(b"ui64tofp").unwrap();
278

279 280
        let res = ui64tofp(0u64);
        println!("ui64tofp(0) = {}", res);
281 282
        assert!(res == 0f64);

283 284
        let res = ui64tofp(1u64);
        println!("ui64tofp(1) = {}", res);
285 286 287 288
        assert!(res == 1f64);
    }
}

289
fn ui64tofp() -> VM {
290 291 292 293 294 295
    let vm = VM::new();

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

    funcsig!    ((vm) sig = (int64) -> (double));
296 297
    funcdecl!   ((vm) <sig> ui64tofp);
    funcdef!    ((vm) <sig> ui64tofp VERSION ui64tofp_v1);
298 299

    // blk entry
300 301
    block!      ((vm, ui64tofp_v1) blk_entry);
    ssa!        ((vm, ui64tofp_v1) <int64> x);
302

303 304
    ssa!        ((vm, ui64tofp_v1) <double> res);
    inst!       ((vm, ui64tofp_v1) blk_entry_conv:
305 306 307
        res = CONVOP (ConvOp::UITOFP) <int64 double> x
    );

308
    inst!       ((vm, ui64tofp_v1) blk_entry_ret:
309 310 311
        RET (res)
    );

312
    define_block!((vm, ui64tofp_v1) blk_entry(x){
313 314 315
        blk_entry_conv, blk_entry_ret
    });

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 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 384 385 386 387 388 389 390 391 392 393 394 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 440 441 442 443 444 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 490 491 492 493 494 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 540 541 542 543 544 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 590 591 592 593 594 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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
    define_func_ver!((vm) ui64tofp_v1 (entry: blk_entry) {blk_entry});

    vm
}

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

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

        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() {
    let lib = testutil::compile_fnc("ui16tofp", &ui16tofp);

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

        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() {
    let lib = testutil::compile_fnc("ui8tofp", &ui8tofp);

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

        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() {
    let lib = testutil::compile_fnc("fptoui64", &fptoui64);

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

        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() {
    let lib = testutil::compile_fnc("fptoui32", &fptoui32);

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

        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() {
    let lib = testutil::compile_fnc("fptoui16", &fptoui16);

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

        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() {
    let lib = testutil::compile_fnc("fptoui8", &fptoui8);

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

        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});
660 661 662 663

    vm
}

qinsoon's avatar
qinsoon committed
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
#[test]
fn test_fp_arraysum() {
    use std::os::raw::c_double;

    let lib = testutil::compile_fnc("fp_arraysum", &fp_arraysum);

    unsafe {
        let fp_arraysum : libloading::Symbol<unsafe extern fn(*const c_double, u64) -> f64> = lib.get(b"fp_arraysum").unwrap();

        let array : [f64; 10] = [0f64, 0.1f64, 0.2f64, 0.3f64, 0.4f64, 0.5f64, 0.6f64, 0.7f64, 0.8f64, 0.9f64];
        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);
688
    typedef!    ((vm) hybrid = mu_hybrid()(double));
qinsoon's avatar
qinsoon committed
689 690 691 692 693 694 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 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
    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)
    );

    define_block!   ((vm, fp_arraysum_v1) blk_entry(blk_entry_arr, blk_entry_sz) {blk_entry_branch});

    // 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
788
    vm
789
}