d7samurai

nmul

Jan 13th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.37 KB | None | 0 0
  1. // normalized byte multiplication:
  2. // range [0, 255] maps to [0.0, 1.0]
  3. // examples:
  4. // 0xff * 0xff = 0xff   (255 * 255 = 255)
  5. // 0xff * 0x7f = 0x7f   (255 * 127 = 127)
  6. // 0x7f * 0x7f = 0x3f   (127 * 127 =  63)
  7. // 0x01 * 0xff = 0x01   (  1 * 255 =   1)
  8. // 0x01 * 0x7f = 0x00   (  1 * 127 =   0)
  9.  
  10. inline byte nmul(byte a, byte b)
  11. {
  12.     return (((uint16)a + 1) * b) >> 8;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment