Split Multiplication of integers

前端 未结 4 1762
挽巷
挽巷 2020-12-20 07:52

I need an algorithm that uses two 32-bit integers as parameters, and returns the multiplication of these parameters split into two other 32-bit integers: 32-highest-bits par

4条回答
  •  眼角桃花
    2020-12-20 08:29

    Just use 16 bits digits.

    void multiply(uint32_t a, uint32_t b, uint32_t* h, uint32_t* l) {
        uint32_t const base = 0x10000;
        uint32_t al = a%base, ah = a/base, bl = b%base, bh = b/base;
        *l = al*bl;
        *h = ah*bh;
        uint32_t rlh = *l/base + al*bh;
        *h += rlh/base;
        rlh = rlh%base + ah*bl;
        *h += rlh/base;
        *l = (rlh%base)*base + *l%base;
    }
    

提交回复
热议问题