tag:blogger.com,1999:blog-5246987755651065286.post2311110512720302412..comments2024-02-22T16:15:42.388-08:00Comments on cbloom rants: 06-13-09 - Integer division by constantscbloomhttp://www.blogger.com/profile/10714564834899413045noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-5246987755651065286.post-42751985286939449752009-06-13T23:13:31.398-07:002009-06-13T23:13:31.398-07:00"2) most of them are designed to work for the..."2) most of them are designed to work for the full range of arguments. Often you know that you only need to work on a limited range of one parameter, and you can find much simpler versions for limited ranges. "cbloomhttps://www.blogger.com/profile/10714564834899413045noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-41475786472078196202009-06-13T22:25:45.253-07:002009-06-13T22:25:45.253-07:00The canonical reference for this stuff is Granlund...The canonical reference for this stuff is Granlund and Montgomery's "Division by invariant integers using multiplication" from 1994Ö<br /><br /><a href="http://portal.acm.org/citation.cfm?id=178249" rel="nofollow">http://portal.acm.org/citation.cfm?id=178249</a><br /><br />Peter Montgomery did the math, Torbjörn did the implementation in gcc.Christer Ericsonhttps://www.blogger.com/profile/12641617282440449995noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-84728626560837230482009-06-13T16:08:58.761-07:002009-06-13T16:08:58.761-07:00Sigh.Sigh.cbloomhttps://www.blogger.com/profile/10714564834899413045noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-783114211905809282009-06-13T15:26:57.195-07:002009-06-13T15:26:57.195-07:00Hi,
Actually compiler already does this for you.....Hi,<br /><br />Actually compiler already does this for you... ;)<br /><br />For example this is what compiler generate for:<br /><br />return a/15;<br /><br />MSVC:<br /> mov eax, -2004318071<br /> mul DWORD PTR _a$[esp-4]<br /> shr edx, 3<br /> mov eax, edx<br /><br />GCC:<br /> 9 0003 BA898888 movl $-2004318071, %edx<br /> 9 88<br /> 10 0008 8B4508 movl 8(%ebp), %eax<br /> 11 000b F7E2 mull %edx<br /> 12 000d C1EA03 shrl $3, %edx<br /> 13 0010 89D0 movl %edx, %eax<br /><br />Anyway, check this out:<br />http://hackersdelight.org/magic.htm<br /><br />BKLABranimir Karadžićhttps://www.blogger.com/profile/08258281434387357224noreply@blogger.com