DEMO.DESIGN
Frequently Asked Questions
 
оглавление | demo party в ex-СССР | infused bytes e-mag | новости от ib/news | другие проекты | письмо | win koi lat

следующий фpагмент (2)
- [38] Talks about assembler (2:5030/84) --------------------------- TALKS.ASM - Msg : 14 of 15 From : Andrey Petrov 2:461/29.444 12 Oct 95 09:34:00 To : Rinat Sadretdinow Subj : Big arithmetic -------------------------------------------------------------------------------- Hi ! 08 Oct 95 11:19:42 (Rinat Sadretdinow to All) > Hикто не может предложить алгоритм умножения 64-битных беззнаковых > чисел для получения 128-битного результата? mov eax, dword ptr a0 ; Вычислили a0 * b0 mul dword ptr b0 mov dword ptr x0, eax mov dword ptr x1, edx mov eax, dword ptr a0 ; Умножили a0 * b1 и сложили mul dword ptr b1 add dword ptr x1, eax adc dword ptr x2, edx mov eax, dword ptr a1 ; Умножили a1 * b0 и сложили mul dword ptr b0 add dword ptr x1, eax adc dword ptr x2, edx mov eax, dword ptr a1 ; Умножили a1 * b1 и сложили mul dword ptr b1 add dword ptr x2, eax adc dword ptr x3, edx a: a0 dd ? a1 dd ? b: b0 dd ? b1 dd ? x: x0 dd 0 ; Обязательно нули x1 dd 0 x2 dd 0 x3 dd 0 > И соответсвенно беззнакового деления 128-битного числа на 64-битное > для получения 64-битного. IMHO, только в лоб (в столбик). Использовать DIV так просто не получится (а может, это просто мне думать лень ? Как всегда ;( С уважением, ANS (Armourer)
следующий фpагмент (3)|пpедыдущий фpагмент (1)
----------------------------------------------------------------------------- - Nice msgs (2:5030/84) ------------------------------------------- !NICE_MAIL - Msg : 14 of 15 From : bwh@beach.cis.ufl.edu 2:5030/144.99 25 Mar 94 17:05:00 To : All Subj : Re: Fixed Point Multiply -------------------------------------------------------------------------------- In article <2mu9r1$2pg@owl.und.ac.za> smith9@batis.bis.und.ac.za (Grant Smith) writes: > That is all well and good, but what happens if your 16x16 * 16x16 multiply > overflows the top 16 bits? when you shift it back, you get garbage! That was > what my origional problem was, and the reason I posted the message. Well, depending on the processor, you need to somehow access the higher 32 bits (the overflow bits), which can't be done from C. On an Intel 32-bit processor (386 or better) then you could do the following: mov eax, [a] ; Put a 16:16 fixedpoint value in 32-bit register EAX mov ebx, [b] ; Put a 16:16 fixedpoint value in 32-bit register EBX imul ebx ; Multiply the two, giving you a 16:32 value in EDX:EAX ; which would cause overflow in C shrd eax, edx, 16 ; Shift EDX:EAX to the right 16-bits to get a value ; represented as 16:16 in EAX mov [result], eax ; Store the answer This, of course, is specific to intel processors. There is no way that I am aware of that this can be done portably. The equivalent C code to the above would cause overflow and NOT trap the high bits in EDX. I have an article on 32-bit fixed point on Intel processors coming up in a future PC Techniques (next month's or the one after that, don't know) if anyone wants to keep an eye out on that. Brian -- +---------------------------------------------------------------+ | Brian Hook | Specializing in real-time 3D graphics | | Box 90315 |---------------------------------------| | Gainesville, FL 32607 | Internet: bwh@cis.ufl.edu | +---------------------------------------------------------------+
следующий фpагмент (4)|пpедыдущий фpагмент (2)
- Nice msgs (2:5030/84) ------------------------------------------- !NICE_MAIL - Msg : 13 of 14 From : sch@unx.sas.com 2:5030/144.99 24 Mar 94 23:12:00 To : All Subj : Re: Fixed Point Multiply -------------------------------------------------------------------------------- smith9@batis.bis.und.ac.za (Grant Smith) writes: >Hi there! >Okay, call me stupid, but how do you multiply two fixed point values >together? >Let us say we have x which is 1.2435 shl 16 > and y which is 5.2436 shl 16 >how do I do a x*y and keep the answer having the same 16x16 format? >Bye... Integer multiplies are trivially easy. Note that the worst case result is twice as many bits as its multpliers. If A is 16 bits and B is 16 bits, the result of multiplying them *could* be up to 32 bits. A = multiplicand B = multiplier C = result neg = negative result flag neg = 0; C = 0; if(A < 0) { A = -A; neg = !neg; } if(B < 0) { B = -B; neg = !neg; } while(B) { if(B & 1) C = C + A; B = B >> 1; A = A << 1; } if(neg) C = -C; print C For efficiency, put the smaller number in the multiplier. The loop stops at the highest bit set in the multiplier. If consistency is important, change the loop to loop N times, where N is the word size of the multiplier.

Всего 3 фpагмент(а/ов) |пpедыдущий фpагмент (3)

Если вы хотите дополнить FAQ - пожалуйста пишите.

design/collection/some content by Frog,
DEMO DESIGN FAQ (C) Realm Of Illusion 1994-2000,
При перепечатке материалов этой страницы пожалуйста ссылайтесь на источник: "DEMO.DESIGN FAQ, http://www.enlight.ru/demo/faq".