flat assembler
Message board for the users of flat assembler.
![]() Goto page Previous 1, 2 |
Author |
|
saigon 12 Jun 2006, 17:47
Quote: And about replacing DIV with MUL there is a trick to do that but I don't remember how to calculate the multiplicator number to simulate division. We have a programmer without much mathematical experience ![]() This is one of the simplest things to do. Let's assume you want to divide 8 by 4, to get the multiplicator number, just divide 1 with 4 and you have your multiplicator ![]() Theoretically, you still have to divide to get the multiplicator, so you won't get off this annoying word, "divide" ![]() |
|||
![]() |
|
LocoDelAssembly 12 Jun 2006, 18:56
saigon, 1 DIV 4 is 0, not very good multiplicator isn't it?
![]() Of course your method works fine for floating poing where 1 FDIV 4 is 0.25 but for integer reciprocal you need the technics described in Agner Fog's optimization manual (or other sources). Quote:
I passed the high school and I still don't know how I did that :S Anyway I'm paying for that at the university, I'm 22 years old and I'm still wanting to pass the maths courses of the first year ![]() |
|||
![]() |
|
Tomasz Grysztar 12 Jun 2006, 19:29
In context of the link I gave several posts above: is there anyone but me interested in p-adic calculations here?
|
|||
![]() |
|
saigon 12 Jun 2006, 19:36
@locodelassembly: What I wanted to explain is what you explained
![]() ![]() @Tomasz: I am interested in p-adic calculations ![]() |
|||
![]() |
|
Just me 12 Jun 2006, 20:35
|
|||
![]() |
|
Borsuc 14 Jun 2006, 13:51
Hey, dividing integers by multiplying is not a true mathmatical subject, it's more on numerical/bit analysis.
Just try this: - when you multiply a 16-bit number with the max value+1 (which is 65536) what does happen? simple: the multiplicand gets placed in the upper 16-bits, which when using 16-bit means it's placed in dx, ok so far? ![]() - to find the "magic" number, let's do some tricks. In math: Code: x*5 --- = x 5 Code: x*65536 ------- y - Ok, what can we observe? Simple: (x/y)*65536 just places the result (x/y) in dx (we're in 16-bit). But of course, in math, this is the same as: x*(65536/y), so we just GOT RID of the division!! 65536/y is just a constant, in fact it's that "magic" number. note1: you see now where 6554 comes from? it's 65536/10, rounded of course. note2: this is the same with 32-bit or any other size. Just replace 65536 with the max number+1 (for example, in 32-bit it is 0x100000000 which is 0xFFFFFFFF+1). The result is, in 32-bit, placed in edx. hint: generally, numerical/bit analysis is done by a lot of testing, so keep testing. Also, you can see how general math is used here, and how useful it can be, even when you might consider it useless. ![]() _________________ Previously known as The_Grey_Beast |
|||
![]() |
|
LocoDelAssembly 14 Jun 2006, 14:22
Thanks The_Grey_Beast
![]() |
|||
![]() |
|
Tomasz Grysztar 14 Jun 2006, 14:50
The_Grey_Beast wrote: Hey, dividing integers by multiplying is not a true mathmatical subject, it's more on numerical/bit analysis. I can ensure you that what I wrote in that other thread about dividing by multiplying is a true and pure abstract algebra. The formal reasoning for the general case is even more so. And your approach focuses on obtaining the approximate result of division, while in general the magic-number division should give the same result as regular integer division done by CPU - division with the remainder, thus the result should be a dividend - the floor integer. Consider x=200, y=100, 65536/100=655 (rounded), x*655=131000, which is 1:65464, so you'd get 200/100=1 this way! Similar, but better method was that proposed by NightWare in the other thread - you'd take (65535 div 100)+1=656 as a multiplier, this one would give correct result for this case, however - as I also pointed out there - it still may give wrong results even with relatively small numbers. |
|||
![]() |
|
ATV 15 Jun 2006, 14:16
This is little offtopic, but some of these has been used in Hugi Size Compo
Code: imul bx,ax,128 ;bh=ax/2 (works for 0-511) imul bx,ax,86 ;bh=ax/3 (works for 0-127 ) imul bx,ax,64 ;bh=ax/4 (works for 0-1023 ) imul bx,ax,52 ;bh=ax/5 (works for 0-63 ) imul bx,ax,43 ;bh=ax/6 (works for 0-130 ) imul bx,ax,37 ;bh=ax/7 (works for 0-89 ) imul bx,ax,32 ;bh=ax/8 (works for 0-2047 ) imul bx,ax,29 ;bh=ax/9 (works for 0-52 ) imul bx,ax,26 ;bh=ax/10 (works for 0-68 ) imul bx,ax,24 ;bh=ax/11 (works for 0-31 ) imul bx,ax,22 ;bh=ax/12 (works for 0-34 ) imul bx,ax,20 ;bh=ax/13 (works for 0-63 ) imul bx,ax,16 ;bh=ax/16 (works for 0-4095 ) imul bx,ax,13 ;bh=ax/20 (works for 0-78 ) imul bx,ax,10 ;bh=ax/26 (works for 0-76 ) imul bx,ax,8 ;bh=ax/32 (works for 0-8191 ) imul bx,ax,7 ;bh=ax/37 (works for 0-109 ) imul bx,ax,6 ;bh=ax/43 (works for 0-127 ) imul bx,ax,4 ;bh=ax/64 (works for 0-16383 ) imul bx,ax,2 ;bh=ax/128 (works for 0-32767 ) |
|||
![]() |
|
saigon 16 Jun 2006, 06:48
@ATV: Thanks for the list! Helps me a lot right now.
[finnish] Hei! Miten menee?? Me taidetaan olla vähemmistössä tässä foorumissa jotka puhuu suomee, vai? ![]() ![]() PS: Mä en ole suomalainen, jos sitä tuumailit, osaan vaan liian monta kieltä laskeakseni, lol ![]() [/finnish] |
|||
![]() |
|
ATV 20 Jun 2006, 13:05
saigon, you are lucky one, I have lost lot of hairs even with english text.
|
|||
![]() |
|
Goto page Previous 1, 2 < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.