on wikipedia i read about these types, but is it possible to declare and use them in C?
This is a discussion on octaword, double quadword within the C Programming forums, part of the General Programming Boards category; on wikipedia i read about these types, but is it possible to declare and use them in C?...
on wikipedia i read about these types, but is it possible to declare and use them in C?
No, but then it depends on what does octaword and double quadword actually correspond to. Maybe a long or long long will suffice, or you could use an arbitrary precision arithmetic library.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Octaword is 16 bytes, no (8 * 2)? There is no such type in C. So if you really need something that big, you'd have to either use an array or an arbitrary precision arithmetic library (whoa, thanks for that text, laserlight; I love copy n' paste!).
I believe the standard says that long long is 8 bytes? Or does it only say that it's >= long?
The 1999 edition of the C standard states that sizeof(long long) >= sizeof(long), and one can infer that the range of long long has a size of at least 64 bits.
Yes, if the range of long long was at least 128 bits in size.Originally Posted by Elysia
Last edited by laserlight; 07-07-2010 at 02:35 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If you have a native 64-bit type, it isn't difficult to do 128-bit arithmetic. It does require some non-portable programming in order to detect carry conditions, but it's not complicated.
Basically:
The non-portable code is hidden inside GetCarryBit().Code:void AddOctWord( uint64 lo1, uint64 hi1, uint64 lo2, uint64 hi2, uint64 *loResult, uint64 *hiResult ) { int carry; *loResult = lo1 + lo2; carry = GetCarryBit(); *hiResult = hi1 + hi2 + carry; }
Subtraction, multiplication, division are similar. For multiplication, realize that these numbers are of the form:
lo+hi*2^64.
Thus, the product of two 128-bit quantities is:
(lo1+hi1*2^64)*(lo2+hi2*2^64)
Using distributive law, the above is equal to:
lo1*lo2 + lo1*hi2*2^64 + lo2*hi1*2^64 + hi1*hi2*2^128
Factor it:
(hi1*hi2)*2^128 + (lo1*hi2 + lo2*hi1)*2^64 + lo1*lo2
Now the operations are all purely 64-bit. Just be aware that the first term can overflow if the quantities being multiplied have a geometric average greater than 2^64.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}