flat assembler
Message board for the users of flat assembler.

Index > Main > Compute 10^n/10 using only 1 register with 2 instructions

Goto page Previous  1, 2, 3
Author
Thread Post new topic Reply to topic
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16861
Location: In your JS exploiting you and your system
revolution
Tomasz Grysztar wrote:
What is especially nice about truncated 2-adic arithmetics is that you can use it to perform several operations on rational numbers, and as long as you know that the final result is going to be an integer in 32-bit range, you know that result is going to be precise. You need such additional knowledge because just by looking at the lowest 32 bits of 2-adic result you are not able to tell an integer from a fraction.
I understand the 2-adic thing much better now. Thanks for the explanations.

I find them to be very interesting.

I also wonder if it could be "faster" (hehe, whatever that means) when computing a division to make the divide loop very efficient for only a fixed numerator of 1 and when the loop is done multiply by the final numerator. Pseudo code
Code:
 mov B,divisor
 compute A = 1/B ;efficient single purpose loop
 return A * numerator ;using imul    
Post 29 Jan 2019, 09:49
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 1431
Furs
Amazing thread, should be made a sticky, I don't want it lost. Maybe in some useful tricks section or so Wink

Maybe we should have a thread with such code tricks, possibly in more languages (I have in constexpr C++ also, computing the constant itself fully at compile-time, zero overhead there). Then we can link this thread and others from there so it's not lost, and only that thread be made sticky.

Tomasz Grysztar wrote:
Well, except that B needs to be odd, but if it's even you can start with shifting both A and B right in parallel until B becomes odd
What if A is 1? Shifting it right would make it zero, so it's clearly wrong.
Post 29 Jan 2019, 12:40
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7434
Location: Kraków, Poland
Tomasz Grysztar
Furs wrote:
What if A is 1? Shifting it right would make it zero, so it's clearly wrong.
Yes, this is the "underflow" condition in my function, which is treated as invalid input (sets CF to 1). We lose any bits that go too far right. But this can be handled in general by shifting the entire working space left by a sufficient number of digits (p-adic numbers always extend only finitely to the right, so you should be able to find some upper bound for your set of calculations). Then it becomes similar to fixed-point arithmetics.
Post 29 Jan 2019, 13:06
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7434
Location: Kraków, Poland
Tomasz Grysztar
For the purpose of playing with 2-adic numbers using fasmg I made this macro that implements the same algorithm for division:
Code:
struc div2adic? A, B, BITS:64
        .a = A
        .b = B
        while .b and 1 = 0
                assert .a and 1 = 0
                .b = .b shr 1
                .a = .a shr 1
        end while
        . = 0
        .product = 0
        while .product xor .a
                .index = bsf (.product xor .a)
                if .index >= BITS
                        break
                end if
                . = . or 1 shl .index
                .product = .product + .b shl .index
        end while
end struc    
Use it like this:
Code:
i3 div2adic 1,3
dq i3 ; 0AAAAAAAAAAAAAAABh    
Note that it can compute an arbitrary number of bits:
Code:
i7 div2adic 1,7, 512
ddqq i7 ; 0B6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB6DB7h    
Post 15 Oct 2019, 11:10
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7434
Location: Kraków, Poland
Tomasz Grysztar
There is one more thing worth mentioning: even though 2-adic reciprocals are only useful for division when you know that the number is divisible exactly, it is also quite easy to verify that it was the case (so you can have error handling).

For example, if we compute 32 lowest digits of A/B (let's call that 32-bit value Q), then if we multiply it by B, we get 32 lowest digits of (A/B)*B = A. The only problem is that when A was not exactly divisible by B, then Q*B has some additional digits (above the 32) and it just happens to have the same low 32 digits as A.

Therefore to test whether the number was exactly divisible, it is enough to check whether Q*B overflows out of 32 bits. For unsigned case this can be tested by comparing Q with (1 shl 32)/B.

For example, to divide an unsigned number by 7 and signal an error when the number was not exactly divisible:
Code:
        imul    eax,0B6DB6DB7h   
        cmp     eax,(1 shl 32)/7
        ja      not_divisible    
For signed case we would have two boundaries to check (we need to ensure that Q*B has high bits all the same as the sign bit of A).


Last edited by Tomasz Grysztar on 08 Nov 2019, 06:34; edited 1 time in total
Post 07 Nov 2019, 19:39
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16861
Location: In your JS exploiting you and your system
revolution
Yes, makes sense.

But it fails for 0xfffffffc (0x24924924 * 7)

0xB6DB6DB7 * 0xfffffffc = 0x(...B6DB6DB4)24924924
(1 shl 32) / 7 = 0x24924924(.6DB6DB6DB...)

So the JAE test is taken.
Post 08 Nov 2019, 05:49
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7434
Location: Kraków, Poland
Tomasz Grysztar
Oh, right, this should be JA there. The divisor must be odd, so (1 shl 32)/B is never going to be exact (and it is rounded down). So when the numbers are equal, there is still no overflow.

In other words the condition for no overflow is Q <= (1 shl 32)/B < \frac{2^{32}}{B}.
Post 08 Nov 2019, 06:21
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3

< 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


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.