OSDN Git Service

[_Roast_Math] fraction_test.cpp
authorMyun2 <myun2@nwhite.info>
Tue, 29 May 2012 08:43:01 +0000 (17:43 +0900)
committerMyun2 <myun2@nwhite.info>
Tue, 29 May 2012 08:43:01 +0000 (17:43 +0900)
roast/test/roast_test_3rd/fraction_test.cpp

index cb03e92..d4f5805 100644 (file)
@@ -3,8 +3,32 @@
 
 using namespace roast;
 
+template <typename T>
+::std::pair<T,T> wide_mul(const T& a, const T&b)
+{
+       static const unsigned int half_bits = 8 * sizeof(T) / 2;
+       
+       T a_high = a >> half_bits;
+       T a_low = a & ((T)-1) >> half_bits;
+       T b_high = b >> half_bits;
+       T b_low = b & ((T)-1) >> half_bits;
+
+       T lowlow = a_low * b_low;
+       T highlow = a_high * b_low;
+       T lowhigh = a_low * b_high;
+       T highhigh = a_high * b_high;
+
+       return ::std::pair<T,T>(
+               highhigh +,
+               lowlow + );
+}
+
+
 int main()
 {
+       ::std::pair<unsigned long,unsigned long> r = wide_mul((unsigned long)0x12345678,(unsigned long)0x12345678);
+       printf("%08X%08X\n", r.first, r.second);
+
        printf("%d\n", gcd(20,10) );
        printf("%d\n", gcd(33,11) );
        printf("%d\n", gcd(34,22) );