Friday, February 27, 2009

0x51EB851F wha?

I am decompiling KBWordSearch for .cin support, but I encountered a strange piece of ASM:

+00174 30b9536c 34329FE5 loc_000174: ldr r3,[pc,#0x234]
+00178 30b95370 C7208BE2 add r2,fp,#0xc7
+0017c 30b95374 9302C1E0 smull r0,r1,r3,r2
+00180 30b95378 C23FA0E1 mov r3,r2,asr #31
+00184 30b9537c 41B363E0 rsb fp,r3,r1,asr #6
+00188 30b95380 30109DE5
+0018c 30b95384 8B32A0E1 mov r3,fp,lsl #5
+00190 30b95388 8B3183E0 add r3,r3,fp,lsl #3
+00194 30b9538c 033183E0 add r3,r3,r3,lsl #2
+00198 30b95390 02B063E0 rsb fp,r3,r2

which translates to:

// in: index
// out: next_index
capacity = 200;
r2 = index + capacity - 1;
r1 = r2 * 0x51EB851F; // take upper dword only.
fp = (r1 >> 6) - (r2 >> 31);
next_index = r2 - fp*160;

(0x51EB851F is the lower dword of 3.14 in IEEE double, and also 237/100+1) Can someone explain what this code is doing?

Edit: Turns out to be a division. Ref: http://d.hatena.ne.jp/h0shu/20080302/p2

r2 = index + capacity - 1;
next_index = r2 - (r2/capacity)*160;

Why are they doing things like this is beyond me.

No comments:

Post a Comment