Faster 16bit multiplication algorithm for 8-bit MCU

前端 未结 6 1407
没有蜡笔的小新
没有蜡笔的小新 2021-02-12 11:27

I\'m searching for an algorithm to multiply two integer numbers that is better than the one below. Do you have a good idea about that? (The MCU - AT Tiny 84/85 or similar - wher

6条回答
  •  爱一瞬间的悲伤
    2021-02-12 11:58

    Well, mix of LUT and shift usually works

    Something along the line, multiplying 8 bit entities. Lets consider them made up of two quads

    uint4_t u1, l1, u2, l2;
    uint8_t a = 16*u1 + l1;
    uint8_t b = 16*u2 + l2;
    
    product = 256*u1*u2 + 16*u1*l2 + 16*u2*l1 + l1*l1;
    
    inline uint4_t hi( uint8_t v ) { return v >> 4; }
    inline uint4_t lo( uint8_t v ) { return v & 15; }
    
    inline uint8_t LUT( uint4_t x, uint4_t y ) {
        static uint8_t lut[256] = ...;
        return lut[x | y << 4]
    }
    
    uint16_t multiply(uint8_t a, uint8_t b) {
        return (uint16_t)LUT(hi(a), hi(b)) << 8 +
               ((uint16_t)LUT(hi(a), lo(b)) + (uint16_t)LUT(lo(a), hi(b)) << 4 +
               (uint16_t)LUT(lo(a), lo(b));
    }
    

    just fill lut[] with results of multiplication. In your case depending on memory you could go with quads (256 sized LUT) or with bytes (65536 size LUT) or anything in between

提交回复
热议问题