int128_via_intrinsic_ut.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334
  1. #include <library/cpp/int128/int128.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. // from https://a.yandex-team.ru/arc/trunk/arcadia/library/ticket_parser/c/src/ut/utils_ut.cpp?rev=4221861
  4. #if defined(Y_HAVE_INT128)
  5. Y_UNIT_TEST_SUITE(Int128ViaIntrinsicSuite) {
  6. using guint128_t = unsigned __int128;
  7. guint128_t toGcc(ui128 num) {
  8. guint128_t res = 0;
  9. res |= GetLow(num);
  10. res |= guint128_t(GetHigh(num)) << 64;
  11. return res;
  12. }
  13. Y_UNIT_TEST(bigintTest) {
  14. UNIT_ASSERT(guint128_t(127) == toGcc(ui128(127)));
  15. UNIT_ASSERT(guint128_t(127) * guint128_t(127) == toGcc(ui128(127) * ui128(127)));
  16. UNIT_ASSERT(guint128_t(127) + guint128_t(127) == toGcc(ui128(127) + ui128(127)));
  17. UNIT_ASSERT(guint128_t(127) << 3 == toGcc(ui128(127) << 3));
  18. UNIT_ASSERT(guint128_t(127) >> 1 == toGcc(ui128(127) >> 1));
  19. UNIT_ASSERT(guint128_t(1000000000027UL) * guint128_t(1000000000027UL) == toGcc(ui128(1000000000027UL) * ui128(1000000000027UL)));
  20. UNIT_ASSERT(guint128_t(1000000000027UL) + guint128_t(1000000000027UL) == toGcc(ui128(1000000000027UL) + ui128(1000000000027UL)));
  21. UNIT_ASSERT(guint128_t(1000000000027UL) << 3 == toGcc(ui128(1000000000027UL) << 3));
  22. UNIT_ASSERT(guint128_t(1000000000027UL) >> 1 == toGcc(ui128(1000000000027UL) >> 1));
  23. UNIT_ASSERT((guint128_t(1000000000027UL) * guint128_t(1000000000027UL)) << 3 == toGcc((ui128(1000000000027UL) * ui128(1000000000027UL)) << 3));
  24. UNIT_ASSERT((guint128_t(1000000000027UL) + guint128_t(1000000000027UL)) >> 1 == toGcc((ui128(1000000000027UL) + ui128(1000000000027UL)) >> 1));
  25. UNIT_ASSERT((ui64)(guint128_t(1000000000027UL) * guint128_t(1000000000027UL)) == GetLow(ui128(1000000000027UL) * ui128(1000000000027UL)));
  26. }
  27. }
  28. #endif