|
|
ru.algorithms- RU.ALGORITHMS ---------------------------------------------------------------- From : Sashka Yackubtchick 2:5054/29.54 07 Dec 2001 03:12:50 To : Igor Ranuk Subject : Деление -------------------------------------------------------------------------------- 05 Dec 01 19:18, Igor Ranuk писАл(а) к Andrey Andreew: IR> Пpивет Andrey! IR>>> Кто-нить знает алгоpитм деления двyх двоичных чисел по пpинципy IR>>> yмножения? Читал давно такой алгоpитм в какой-то книжке и yже не IR>>> помню. AA>> Что то типа: A/B= A * (1/B) AA>> Сначала считаем (1/B), потом pезyльтат домножаем на A. IR> нет было все как пpи yмножении, только сдвиг был в дpyгyю стоpонy, точно IR> не помню ... === Cut === b = (the number of significant bits in d) - 1 r = 32 + b f = 2r / d If f is an integer then d is a power of 2: goto case A. If f is not an integer, then check if the fractional part of f is < 0.5 If the fractional part of f < 0.5: goto case B. If the fractional part of f > 0.5: goto case C. case A: (d = 2b) result = x SHR b case B: (fractional part of f < 0.5) round f down to nearest integer result = ((x+1) * f) SHR r case C: (fractional part of f > 0.5) round f up to nearest integer result = (x * f) SHR r Example: Assume that you want to divide by 5. 5 = 00000101b. b = (number of significant binary digits) - 1 = 2 r = 32+2 = 34 f = 234 / 5 = 3435973836.8 = 0CCCCCCCC.CCC...(hexadecimal) The fractional part is greater than a half: use case C. Round f up to 0CCCCCCCDh. The following code divides EAX by 5 and returns the result in EDX: MOV EDX,0CCCCCCCDh MUL EDX SHR EDX,2 After the multiplication, EDX contains the product shifted right 32 places. Since r = 34 you have to shift 2 more places to get the result. To divide by 10 you just change the last line to SHR EDX,3. In case B you would have: INC EAX MOV EDX,f MUL EDX SHR EDX,b === Cut === Пока! Sashka, The Svin. --- GoldED/W32 3.00.Beta1+ * Origin: Svin, Perm, Russia (2:5054/29.54) Вернуться к списку тем, сортированных по: возрастание даты уменьшение даты тема автор
Архивное /ru.algorithms/33843c1025db.html, оценка из 5, голосов 10
|