Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"06-13-09 - Integer division by constants"

4 Comments -

1 – 4 of 4
Blogger Branimir Karadžić said...

Hi,

Actually compiler already does this for you... ;)

For example this is what compiler generate for:

return a/15;

MSVC:
mov eax, -2004318071
mul DWORD PTR _a$[esp-4]
shr edx, 3
mov eax, edx

GCC:
9 0003 BA898888 movl $-2004318071, %edx
9 88
10 0008 8B4508 movl 8(%ebp), %eax
11 000b F7E2 mull %edx
12 000d C1EA03 shrl $3, %edx
13 0010 89D0 movl %edx, %eax

Anyway, check this out:
http://hackersdelight.org/magic.htm

BKLA

June 13, 2009 at 3:26 PM

Blogger cbloom said...

Sigh.

June 13, 2009 at 4:08 PM

Blogger Christer Ericson said...

The canonical reference for this stuff is Granlund and Montgomery's "Division by invariant integers using multiplication" from 1994Ö

http://portal.acm.org/citation.cfm?id=178249

Peter Montgomery did the math, Torbjörn did the implementation in gcc.

June 13, 2009 at 10:25 PM

Blogger cbloom said...

"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. "

June 13, 2009 at 11:13 PM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.