aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/int128/ut/int128_via_intrinsic_ut.cpp
blob: d19ebc959a2bb049a3bffabeaeb177d3c4e7c2dc (plain) (blame)
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
#include <library/cpp/int128/int128.h>

#include <library/cpp/testing/unittest/registar.h>

// from https://a.yandex-team.ru/arc/trunk/arcadia/library/ticket_parser/c/src/ut/utils_ut.cpp?rev=4221861

#if defined(Y_HAVE_INT128)
Y_UNIT_TEST_SUITE(Int128ViaIntrinsicSuite) {
    using guint128_t = unsigned __int128;
    guint128_t toGcc(ui128 num) {
        guint128_t res = 0;
        res |= GetLow(num);
        res |= guint128_t(GetHigh(num)) << 64;
        return res;
    }

    Y_UNIT_TEST(bigintTest) { 
        UNIT_ASSERT(guint128_t(127) == toGcc(ui128(127)));
        UNIT_ASSERT(guint128_t(127) * guint128_t(127) == toGcc(ui128(127) * ui128(127)));
        UNIT_ASSERT(guint128_t(127) + guint128_t(127) == toGcc(ui128(127) + ui128(127)));
        UNIT_ASSERT(guint128_t(127) << 3 == toGcc(ui128(127) << 3));
        UNIT_ASSERT(guint128_t(127) >> 1 == toGcc(ui128(127) >> 1));

        UNIT_ASSERT(guint128_t(1000000000027UL) * guint128_t(1000000000027UL) == toGcc(ui128(1000000000027UL) * ui128(1000000000027UL)));
        UNIT_ASSERT(guint128_t(1000000000027UL) + guint128_t(1000000000027UL) == toGcc(ui128(1000000000027UL) + ui128(1000000000027UL)));
        UNIT_ASSERT(guint128_t(1000000000027UL) << 3 == toGcc(ui128(1000000000027UL) << 3));
        UNIT_ASSERT(guint128_t(1000000000027UL) >> 1 == toGcc(ui128(1000000000027UL) >> 1));
        UNIT_ASSERT((guint128_t(1000000000027UL) * guint128_t(1000000000027UL)) << 3 == toGcc((ui128(1000000000027UL) * ui128(1000000000027UL)) << 3));
        UNIT_ASSERT((guint128_t(1000000000027UL) + guint128_t(1000000000027UL)) >> 1 == toGcc((ui128(1000000000027UL) + ui128(1000000000027UL)) >> 1));

        UNIT_ASSERT((ui64)(guint128_t(1000000000027UL) * guint128_t(1000000000027UL)) == GetLow(ui128(1000000000027UL) * ui128(1000000000027UL)));
    }
}
#endif