flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Blog > Division by multiplication - revisited

Goto page Previous  1, 2
Thread Post new topic Reply to topic
Tomasz Grysztar
Assembly Artist

Joined: 16 Jun 2003
Posts: 6741
Location: Kraków, Poland

Furs wrote:
I'm a bit confused. So I need to iterate all shift values, but how can I check if the condition holds if I don't know the value of N? Should I assume the highest value possible for it that it can have? I mean, N is a runtime value, only the divisor is at compile time.

Yes, you can consider just the highest possible value of N, if inequality holds for it, then it holds for all the smaller values too.

For example if P = 1 then NP<2^n is true as long as N<2^n. If P = 2 then to have 2N<2^n we need N<2^{n-1}, the allowed range of input becomes smaller.

Furs wrote:
And also if I divide 2^32 by 641, the remainder is 640, not 1, so how come it works?
EDIT: I'm guessing P is not the remainder, but rather D-remainder, am I right? I looked before at your example with 10, and you said P = 4, which fits just as well as on 641. (i.e. 10-6 = 4, and 641-640 = 1)

Oh, you're right, I confused the terminology, P is the complement of the remainder, P = D - R. In that post above where I gave all the formulas it is explained correctly.
Post 28 Feb 2018, 23:56
View user's profile Send private message Visit poster's website Reply with quote

Joined: 04 Mar 2016
Posts: 1040

Ok, the thing I gathered together works great (with all that I got from here, including your first method if the 0xFFFF...FF value is possible, I bet nobody else uses it Razz). Now I can feel at peace knowing the stupid thing can spit out the most optimal constants/method. Thanks Wink

A related question. What's the best (fastest, in general) way to check if a number is divisible by a constant? Sure, I can use the division here, and see if the result multiplied with the divisor is equal to the original value... but that's 2 muls. So I found this here: https://math.stackexchange.com/questions/1251327/is-there-a-fast-divisibility-check-for-a-fixed-divisor

For odd divisors it's fantastic, one mul and one cmp, literally. But it needs two branches if the divisor is even, which sucks (or a lot of extra overhead code/registers usage, unless you can find a better way, problem is the ZF can't be used except with cmov, no adc/sbb tricks). Any better way for even divisors? (I'm not a math guru so I don't really understand how it works, lol)

I did implement them both in C++ with constexpr (compile-time) forcing, and it works fine, but it'd be nice to have a better way for even divisors. (by "better way" I mean in the resulting asm of course, not having two branches is a good win unless I need 3-4 extra instructions, then it's not so good)

(well I have them in C++ so far for HLL usage, which I just use it to compile a simple function if I want the constants for asm usage by looking at the output... like what I was originally doing, except GCC's optimizer sucks, though it's a bit ugly since I had to use internal constexpr functions to force it at compile-time, GCC is too dumb with loops otherwise; I don't know if anyone is interested in my C++ code with them)

For example, let's say checking if number is divisible by 6. So far my code spits this out:

test al1
jnz .no
imul eaxeax0xAAAAAAAB
cmp eax0x55555555
ja .no

Post 10 Mar 2018, 16:31
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< Last Thread | Next Thread >

Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2018, Tomasz Grysztar.
Powered by rwasa.