Getting the high part of 64 bit integer multiplication

后端 未结 5 1435
無奈伤痛
無奈伤痛 2020-11-28 13:30

In C++, say that:

uint64_t i;
uint64_t j;

then i * j will yield an uint64_t that has as value the lower part of t

5条回答
  •  一整个雨季
    2020-11-28 14:14

    Long multiplication should be ok performance.

    Split a*b into (hia+loa)*(hib+lob). This gives 4 32 bit multiplies plus some shifts. Do them in 64 bits, and do the carries manually, and you'll get the high portion.

    Note that an approximation of the high portion can be done with fewer multiplies -- accurate within 2^33 or so with 1 multiply, and within 1 with 3 multiplies.

    I do not think there is a portable alternative.

提交回复
热议问题