Efficient computation of the high order bits of a 32 bit integer multiplication

前端 未结 3 1198
青春惊慌失措
青春惊慌失措 2021-01-04 19:24

Many CPUs have single assembly opcodes for returning the high order bits of a 32 bit integer multiplication. Normally multiplying two 32 bit integers produc

3条回答
  •  囚心锁ツ
    2021-01-04 19:38

    I don't think there's a way to do this in standard C/C++ better than what you already have. What I'd do is write up a simple assembly wrapper that returned the result you want.

    Not that you're asking about Windows, but as an example even though Windows has an API that sounds like it does what you want (a 32 by 32 bit multiply while obtaining the full 64 bit result), it implements the multiply as a macro that does what you're doing:

    #define UInt32x32To64( a, b ) (ULONGLONG)((ULONGLONG)(DWORD)(a) * (DWORD)(b))
    

提交回复
热议问题