test_compiler_builtins.cpp 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 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
/**
 *    @file test_compiler_builtins.cpp
 *    @copyright defined in eos/LICENSE.txt
 */

#include <eoslib/eos.hpp>
#include <eoslib/print.hpp>
#include <eoslib/compiler_builtins.h>

#include "test_api.hpp"

using namespace eosio;

void test_compiler_builtins::test_multi3() {
   /*
    * tests for negative values
    */
   __int128 res   = 0;
   __int128 lhs_a = -30;
   __int128 rhs_a = 100;
   __int128 lhs_b = 100;
   __int128 rhs_b = -30;

   __multi3(res, lhs_a, (lhs_a >> 64), rhs_a, (rhs_a >> 64));
   assert(res == -3000, "__multi3 result should be -3000"); 

   __multi3(res, lhs_b, (lhs_b >> 64), rhs_b, (rhs_b >> 64));
   assert(res == -3000, "__multi3 result should be -3000"); 

   __multi3(res, lhs_a, (lhs_a >> 64), rhs_b, (rhs_b >> 64));
   assert(res == 900, "__multi3 result should be 900"); 

   /*
    * test for positive values
    */
   __multi3(res, lhs_b, (lhs_b >> 64), rhs_a, (rhs_a >> 64));
   assert(res == 10000, "__multi3 result should be 10000"); 

   /*
    * test identity
    */
   __multi3(res, 1, 0, rhs_a, rhs_a >> 64);
   assert(res == 100, "__multi3 result should be 100");

   __multi3(res, 1, 0, rhs_b, rhs_b >> 64);
   assert(res == -30, "__multi3 result should be -30");
 

   /*
    * test for large values
    */
   uint128_t large_lhs;
}

void test_compiler_builtins::test_divti3() {
   /*
    * test for negative values
    */
   __int128 res   = 0;
   __int128 lhs_a = -30;
   __int128 rhs_a = 100;
   __int128 lhs_b = 100;
   __int128 rhs_b = -30;

   __divti3(res, lhs_a, (lhs_a >> 64), rhs_a, (rhs_a >> 64));
   assert(res == 0, "__divti3 result should be 0"); 

   __divti3(res, lhs_b, (lhs_b >> 64), rhs_b, (rhs_b >> 64));
   assert(res == -3, "__divti3 result should be -3"); 

   __divti3(res, lhs_a, (lhs_a >> 64), rhs_b, (rhs_b >> 64));
   assert(res == 1, "__divti3 result should be 1"); 

   /*
    * test for positive values
    */
   __int128 lhs_c = 3333;
   __int128 rhs_c = 3333;
   __divti3(res, lhs_b, (lhs_b >> 64), rhs_a, (rhs_a >> 64));
   assert(res == 1, "__divti3 result should be 1"); 

   __divti3(res, lhs_b, (lhs_b >> 64), rhs_c, (rhs_c >> 64));
   assert(res == 0, "__divti3 result should be 0"); 

   __divti3(res, lhs_c, (lhs_c >> 64), rhs_a, (rhs_a >> 64));
   assert(res == 33, "__divti3 result should be 33"); 

   /*
    * test identity
    */
   __divti3(res, lhs_b, (lhs_b >> 64), 1, 0);
   assert(res == 100, "__divti3 result should be 100"); 

   __divti3(res, lhs_a, (lhs_a >> 64), 1, 0);
   assert(res == -30, "__divti3 result should be -30"); 

   uint128_t r2(res);
   prints("res2 ");
   printi128(&r2);
   prints("\n");
}

void test_compiler_builtins::test_divti3_by_0() {
   __int128 res = 0;

   __divti3(res, 100, 0, 0, 0);
   assert(false, "Should have thrown divide by zero");
}

void test_compiler_builtins::test_lshlti3() {
   __int128 res = 0;
   __int128 val = 1;

   __lshlti3(res, val, val >> 64, 0);
   assert(res == 1, "__lshlti3 result should be 1");


   __lshlti3(res, val, val >> 64, 1);
   assert(res == (1 << 1), "__lshlti3 result should be 2");

   __lshlti3(res, val, (val >> 64), 31);
   assert(res == 0x80000000, "__lshlti3 result should be 2^31");
   //assert(res == (1 << 31), "__lshlti3 result should be 2^31");
   
   __lshlti3(res, val, (val >> 64), 63);
   assert(res == 0x8000000000000000, "__lshlti3 result should be 2^63");
   //assert(res == (1 << 31), "__lshlti3 result should be 2^31");
   //assert(false, "Should have thrown divide by zero");
   //__lshlti3(res, val, (val >> 64), 65);
   //assert(res == 0x20000000000000000L, "__lshlti3 result should be 2^63");
   //assert(res == , "__lshlti3 result should be 2^63");
}