|
10 | 10 | #include "llvm/ADT/ArrayRef.h"
|
11 | 11 | #include "llvm/ADT/DenseMap.h"
|
12 | 12 | #include "llvm/ADT/SmallString.h"
|
| 13 | +#include "llvm/ADT/StringExtras.h" |
13 | 14 | #include "llvm/ADT/Twine.h"
|
14 | 15 | #include "llvm/Support/Alignment.h"
|
15 | 16 | #include "gtest/gtest.h"
|
@@ -2805,6 +2806,89 @@ TEST(APIntTest, multiply) {
|
2805 | 2806 | EXPECT_EQ(64U, i96.countr_zero());
|
2806 | 2807 | }
|
2807 | 2808 |
|
| 2809 | +TEST(APIntOpsTest, MulHiLo) { |
| 2810 | + |
| 2811 | + // Unsigned |
| 2812 | + |
| 2813 | + // 32 bits |
| 2814 | + APInt i32a(32, 0x0001'E235); |
| 2815 | + APInt i32b(32, 0xF623'55AD); |
| 2816 | + EXPECT_EQ(0x0001'CFA1, APIntOps::mulHiU(i32a, i32b)); |
| 2817 | + EXPECT_EQ(0x7CA0'76D1, APIntOps::mulLoU(i32a, i32b)); |
| 2818 | + |
| 2819 | + // 64 bits |
| 2820 | + APInt i64a(64, 0x1234'5678'90AB'CDEF); |
| 2821 | + APInt i64b(64, 0xFEDC'BA09'8765'4321); |
| 2822 | + EXPECT_EQ(0x121F'A000'A372'3A57, APIntOps::mulHiU(i64a, i64b)); |
| 2823 | + EXPECT_EQ(0xC24A'442F'E556'18CF, APIntOps::mulLoU(i64a, i64b)); |
| 2824 | + |
| 2825 | + // 128 bits |
| 2826 | + APInt i128a(128, "1234567890ABCDEF1234567890ABCDEF", 16); |
| 2827 | + APInt i128b(128, "FEDCBA0987654321FEDCBA0987654321", 16); |
| 2828 | + APInt i128ResHi = APIntOps::mulHiU(i128a, i128b); |
| 2829 | + std::string strResHi = toString(i128ResHi, 16, false, true, true, true); |
| 2830 | + EXPECT_STREQ("0x121F'A000'A372'3A57'E689'8431'2C3A'8D7E", strResHi.c_str()); |
| 2831 | + APInt i128ResLo = APIntOps::mulLoU(i128a, i128b); |
| 2832 | + std::string strResLo = toString(i128ResLo, 16, false, true, true, true); |
| 2833 | + EXPECT_STREQ("0x96B4'2860'6E1E'6BF5'C24A'442F'E556'18CF", strResLo.c_str()); |
| 2834 | + |
| 2835 | + // Signed |
| 2836 | + |
| 2837 | + // 32 bits |
| 2838 | + APInt i32c(32, 0x1234'5678); // +ve |
| 2839 | + APInt i32d(32, 0x10AB'CDEF); // +ve |
| 2840 | + APInt i32e(32, 0xFEDC'BA09); // -ve |
| 2841 | + |
| 2842 | + EXPECT_EQ(0x012F'7D02, APIntOps::mulHiS(i32c, i32d)); |
| 2843 | + EXPECT_EQ(0x2A42'D208, APIntOps::mulLoS(i32c, i32d)); |
| 2844 | + |
| 2845 | + EXPECT_EQ(0xFFEB'4988, APIntOps::mulHiS(i32c, i32e)); |
| 2846 | + EXPECT_EQ(0x09CA'3A38, APIntOps::mulLoS(i32c, i32e)); |
| 2847 | + |
| 2848 | + EXPECT_EQ(0x0001'4B68, APIntOps::mulHiS(i32e, i32e)); |
| 2849 | + EXPECT_EQ(0x22A9'1451, APIntOps::mulLoS(i32e, i32e)); |
| 2850 | + |
| 2851 | + // 64 bits |
| 2852 | + APInt i64c(64, 0x1234'5678'90AB'CDEF); // +ve |
| 2853 | + APInt i64d(64, 0x1234'5678'90FE'DCBA); // +ve |
| 2854 | + APInt i64e(64, 0xFEDC'BA09'8765'4321); // -ve |
| 2855 | + |
| 2856 | + EXPECT_EQ(0x014B'66DC'328E'10C1, APIntOps::mulHiS(i64c, i64d)); |
| 2857 | + EXPECT_EQ(0xFB99'7041'84EF'03A6, APIntOps::mulLoS(i64c, i64d)); |
| 2858 | + |
| 2859 | + EXPECT_EQ(0xFFEB'4988'12C6'6C68, APIntOps::mulHiS(i64c, i64e)); |
| 2860 | + EXPECT_EQ(0xC24A'442F'E556'18CF, APIntOps::mulLoS(i64c, i64e)); |
| 2861 | + |
| 2862 | + EXPECT_EQ(0x0001'4B68'2174'FA18, APIntOps::mulHiS(i64e, i64e)); |
| 2863 | + EXPECT_EQ(0xCEFE'A12C'D7A4'4A41, APIntOps::mulLoS(i64e, i64e)); |
| 2864 | + |
| 2865 | + // 128 bits |
| 2866 | + APInt i128c(128, "1234567890ABCDEF1234567890ABCDEF", 16); // +ve |
| 2867 | + APInt i128d(128, "1234567890FEDCBA1234567890FEDCBA", 16); // +ve |
| 2868 | + APInt i128e(128, "FEDCBA0987654321FEDCBA0987654321", 16); // -ve |
| 2869 | + |
| 2870 | + i128ResHi = APIntOps::mulHiS(i128c, i128d); |
| 2871 | + strResHi = toString(i128ResHi, 16, false, true, true, true); |
| 2872 | + EXPECT_STREQ("0x14B'66DC'328E'10C1'FE30'3DF9'EA0B'2529", strResHi.c_str()); |
| 2873 | + i128ResLo = APIntOps::mulLoS(i128c, i128d); |
| 2874 | + strResLo = toString(i128ResLo, 16, false, true, true, true); |
| 2875 | + EXPECT_STREQ("0xF87E'475F'3C6C'180D'FB99'7041'84EF'03A6", strResLo.c_str()); |
| 2876 | + |
| 2877 | + i128ResHi = APIntOps::mulHiS(i128c, i128e); |
| 2878 | + strResHi = toString(i128ResHi, 16, false, true, true, true); |
| 2879 | + EXPECT_STREQ("0xFFEB'4988'12C6'6C68'D455'2DB8'9B8E'BF8F", strResHi.c_str()); |
| 2880 | + i128ResLo = APIntOps::mulLoS(i128c, i128e); |
| 2881 | + strResLo = toString(i128ResLo, 16, false, true, true, true); |
| 2882 | + EXPECT_STREQ("0x96B4'2860'6E1E'6BF5'C24A'442F'E556'18CF", strResLo.c_str()); |
| 2883 | + |
| 2884 | + i128ResHi = APIntOps::mulHiS(i128e, i128e); |
| 2885 | + strResHi = toString(i128ResHi, 16, false, true, true, true); |
| 2886 | + EXPECT_STREQ("0x1'4B68'2174'FA18'CCBA'AC10'2958'C4B5", strResHi.c_str()); |
| 2887 | + i128ResLo = APIntOps::mulLoS(i128e, i128e); |
| 2888 | + strResLo = toString(i128ResLo, 16, false, true, true, true); |
| 2889 | + EXPECT_STREQ("0x9BB8'01D4'DF88'14DC'CEFE'A12C'D7A4'4A41", strResLo.c_str()); |
| 2890 | +} |
| 2891 | + |
2808 | 2892 | TEST(APIntTest, RoundingUDiv) {
|
2809 | 2893 | for (uint64_t Ai = 1; Ai <= 255; Ai++) {
|
2810 | 2894 | APInt A(8, Ai);
|
|
0 commit comments