ВходНаше всё Теги codebook 无线电组件 Поиск Опросы Закон Понедельник
22 июля
104124 Топик полностью
AVR (02.11.2007 12:11, просмотров: 176) ответил Andreas на Вроде здесь проскакивала ссылка на несложный алгоритм вычисления квадратного корня, никто не помнит?
Кому и кобыла невеста <asm> ;------------------------------------------------------------------------------ ; 16-bit integer square root ; ; Algorithm: ; 1. Calculate a rough result estimation as 2^(Nbits(X)/2) ; 2. Multiply the found rough result by itself (square), ; 3. If a square is bigger than X then make the bit 0, ; 4. Otherwise, keep the one bit. ; 5. Keep shifting/squareing for all N (max 8) bits. ; ; w4 = X ; w6 = result ; ; w0:w1 scratch ; w2 = iteration counter/bit pointer ; ; 20..62 clocks, 18 program words sqrt16: clr w6 ; Zero a raw result push.s ; Save w0..w3, SR sqrt16e: ff1l w4,w2 ; Find a leftmost "1" position in X (K) bra c,3f ; Exit if X = 0 subr w2,#16,w2 ; Calculate a number of significant bits in X (N=16-K) lsr w2,w2 ; and divide it by 2 to get a first rough result estimation bclr SR,#Z ; Ensure Z =0 for BSW in a 1st iteration do w2,1f ; Repeat Nbits/2 times mov DCOUNT,w2 ; w2 = M = current rolling "1" bit number bsw.z w6,w2 ; Set an Mth bit of a result mul.uu w6,w6,w0 ; Square a raw result cp w4,w0 ; Compare a squared raw result with X (CY is set if op1 >= op2!) bra z,2f ; Break a DO loop if an exact square is calculated 1: bsw.c w6,w2 ; Reset an Mth bit of a result if squared > X 2: bset CORCON,#EDT ; Handle an early DO termination if exact sqrt 3: pop.s ; Restore w0..w3, SR return ;------------------------------------------------------------------------------ </asm>