3 ms·
Sometimes you want to do things high level languages don't support. For instance C doesn't let you retrieve the 64-bit result from a 32x32-bit multiply. You hav
by kevin_thibedeau 12y ago
Sometimes you want to do things high level languages don't support. For instance C doesn't let you retrieve the 64-bit result from a 32x32-bit multiply. You have to cast the operands to 64-bit first to get the result even though the hardware provides the answer you want without any extra steps. If this is performance critical code it may require dropping down to assembly.
- vardump 12y agoYou don't need assembler for that. Just use compiler intrinsics, they usually compile down to a single instruction.
- psykotic 12y ago> For instance C doesn't let you retrieve the 64-bit result from a 32x32-bit multiply. You have to cast the operands to 64-bit first to get the result even though the hardware provides the answer you want without any extra steps. Any optimizing compiler should generate a single 32-bit MUL for uint64_t z = (uint64_t) x * (uint64_t) y. It's easy for instruction selection to match against such patterns. A better example would be generating a single instruction for the 128-bit product of 64-bit integers on a 64-bit processor. There are no standard types for 128-bit integers, but you can still avoid assembly code by using compiler-specific types or intrinsics. With GCC, Clang and ICC, you can write unsigned __int128 z = (unsigned __int128) x * (unsigned __int128) y. The odd one out is MSVC, where you have to use the _umul128 intrisic.