First public contribution.
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\common\arm\carray.cia
15 // Machine coded arrays for ARM
20 #include "../common.h"
22 #ifdef __ARRAY_MACHINE_CODED__
23 extern "C" void PanicBadArrayFindMode();
25 EXPORT_C __NAKED__ TAny*& RPointerArrayBase::At(TInt /*anIndex*/) const
27 asm("ldmia r0, {r2,r3} "); // r2=iCount, r3=iEntries
28 asm("cmp r1, #0 "); // check anIndex>=0
29 asm("cmpge r2, r1 "); // if so, check iCount>anIndex
30 asm("addgt r0, r3, r1, lsl #2 "); // address of entry = iEntries+4*anIndex
32 asm("ble 1f "); // avoid conditional return on ARMv6
38 asm("b " CSM_Z18PanicBadArrayIndexv);
41 EXPORT_C __NAKED__ TInt RPointerArrayBase::Append(const TAny* /*anEntry*/)
43 asm("ldmia r0, {r2,r3,r12} "); // r2=iCount, r3=iEntries, r12=iAllocated
45 asm("beq ptr_append_1 ");
46 asm("ptr_append_0: ");
47 asm("str r1, [r3, r2, lsl #2] ");
48 asm("add r2, r2, #1 ");
52 asm("ptr_append_1: ");
53 asm("stmfd sp!, {r0,r1,r2,lr} ");
54 asm("bl " CSM_ZN17RPointerArrayBase4GrowEv);
56 asm("bne ptr_append_2 ");
57 asm("ldmfd sp!, {r0,r1,r2,lr} ");
58 asm("ldmia r0, {r2, r3} ");
59 asm("b ptr_append_0 ");
60 asm("ptr_append_2: "); // error enlarging array
61 asm("add sp, sp, #12 ");
65 EXPORT_C __NAKED__ TInt RPointerArrayBase::Find(const TAny* /*anEntry*/) const
67 asm("ldmia r0, {r2,r3} "); // r2=iCount, r3=iEntries
68 asm("mov r0, #0 "); // r0=0 (will be index+1)
69 asm("subs r2, r2, #1 "); // r2=iCount-1
72 asm("ldr r12, [r3], #4 "); // r12=iEntries[r0]
73 asm("add r0, r0, #1 "); // r0 = index+1
74 asm("cmp r2, r0 "); // C=1 iff iCount-1>=index+1 iff index<=iCount-2 iff this isn't last entry
75 asm("teq r12, r1 "); // check for equality, doesn't affect C
76 asm("bhi 1b "); // loop if C=1 & Z=0, i.e. if no match and this isn't last entry
78 asm("movne r0, #0 "); // if no match set r0=0
79 asm("sub r0, r0, #1 "); // r0 was index+1, return index
83 EXPORT_C __NAKED__ TInt RPointerArrayBase::Find(const TAny* /*anEntry*/, TGeneralIdentityRelation /*anIdentity*/) const
85 asm("stmfd sp!, {r4-r8,lr} ");
86 __EH_FRAME_PUSH2(r4-r8,lr)
87 asm("ldmia r0, {r4,r5} "); // r4=iCount, r5=iEntries
91 asm("subs r4, r4, #1 "); // r4=iCount-1
92 asm("bmi ptr_find2_return "); // if count=0, return -1
93 asm("ptr_find2_loop: ");
94 asm("ldr r1, [r5], #4 "); // r1=pointer to entry r6
95 asm("add r6, r6, #1 ");
96 asm("mov r0, r7 "); // r0=anEntry
99 asm("bne ptr_find2_return "); // if equal, return r6
101 asm("blt ptr_find2_loop ");
103 asm("ptr_find2_return: "); // return r6
108 EXPORT_C __NAKED__ TInt RPointerArrayBase::BinarySearchSigned(TInt /*anEntry*/, TInt& /*anIndex*/) const
114 EXPORT_C __NAKED__ TInt RPointerArrayBase::BinarySearchSigned(TInt /*anEntry*/, TInt& /*anIndex*/, TInt /*aMode*/) const
116 asm("stmfd sp!, {r4-r6,lr} ");
117 __EH_FRAME_PUSH2(r4-r6,lr)
118 asm("mov r6, r2 "); // r6=&anIndex
119 asm("ldmia r0, {r2,r4} "); // r2=count, r4=iEntries
120 asm("bl BinarySearchSigned ");
121 asm("str r2, [r6] "); // store index
124 // Binary search list of signed integers
126 // List address in r4
129 // Return with: r0=0 if match found, r0=-1 otherwise
130 // Z flag set if match found, clear if not
131 // r2=index of match or next higher
133 asm("BinarySearchSigned: ");
135 asm("cmp r3, #%a0" : : "i" ((TInt)EArrayFindMode_Limit) );
136 asm("bhs PanicBadArrayFindMode ");
138 asm("mov r3, r3, lsl #30 "); // match mode -> bits 30,31 (00000000=any, 40000000=first, 80000000=last)
139 asm("orr r3, r3, #1 "); // set NOT FOUND flag
140 asm("cmp r2, #0 "); // r2 will be right index
142 asm("mov r5, #0 "); // r5 = left index
144 asm("add r12, r2, r5 ");
145 asm("mov r12, r12, lsr #1 "); // r12 = mid index
146 asm("ldr r0, [r4, r12, lsl #2] "); // r0 = entry[mid]
147 asm("subs r0, r0, r1 "); // r0 = entry[mid] - match
148 asm("beq 2f "); // if match branch out
150 asm("addlt r5, r12, #1 "); // else if entry<match left=mid+1
151 asm("movgt r2, r12 "); // else if entry>match right=mid
152 asm("subs r0, r2, r5 "); // right > left ?
153 asm("bgt 1b "); // r0 = 0 when loop terminates
155 asm("tst r3, #1 "); // test not found flag
156 asm("mvnnes r0, #0 "); // if set r0=-1 = KErrNotFound
159 asm("bics r3, r3, #1 "); // clear NOT FOUND flag, test for find mode ANY (Z set if so)
160 asm("bne 3b "); // if not, V=0 (left from subs), N=1 for last, 0 for first, Z=0 => LAST->LT FIRST->GT
161 asm("mov r2, r12 "); // if so, r2 = mid
162 __JUMP(,lr); // and return with r0 = 0
165 EXPORT_C __NAKED__ TInt RPointerArrayBase::BinarySearchUnsigned(TUint /*anEntry*/, TInt& /*anIndex*/) const
171 EXPORT_C __NAKED__ TInt RPointerArrayBase::BinarySearchUnsigned(TUint /*anEntry*/, TInt& /*anIndex*/, TInt /*aMode*/) const
173 asm("stmfd sp!, {r4-r6,lr} ");
174 __EH_FRAME_PUSH2(r4-r6,lr)
175 asm("mov r6, r2 "); // r6=&anIndex
176 asm("ldmia r0, {r2,r4} "); // r2=count, r4=iEntries
177 asm("bl BinarySearchUnsigned ");
178 asm("str r2, [r6] "); // store index
181 // Binary search list of unsigned integers
183 // List address in r4
186 // Return with: r0=0 if match found, r0=-1 otherwise
187 // Z flag set if match found, clear if not
188 // r2=index of match or next higher
190 asm("BinarySearchUnsigned: ");
192 asm("cmp r3, #%a0" : : "i" ((TInt)EArrayFindMode_Limit) );
193 asm("bhs PanicBadArrayFindMode ");
195 asm("mov r3, r3, lsl #30 "); // match mode -> bits 30,31 (00000000=any, 40000000=first, 80000000=last)
196 asm("orr r3, r3, #1 "); // set NOT FOUND flag
197 asm("cmp r2, #0 "); // r2 will be right index
199 asm("mov r5, #0 "); // r5 = left index
201 asm("add r12, r2, r5 ");
202 asm("mov r12, r12, lsr #1 "); // r12 = mid index
203 asm("ldr r0, [r4, r12, lsl #2] "); // r0 = entry[mid]
204 asm("subs r0, r1, r0 "); // r0 = match - entry[mid]
205 asm("beq 2f "); // if match branch out
207 asm("addhi r5, r12, #1 "); // else if entry<match left=mid+1 HI = C &~ Z
208 asm("movlo r2, r12 "); // else if entry>match right=mid LO = ~C
209 asm("subs r0, r2, r5 "); // right > left ?
210 asm("bgt 1b "); // r0 = 0 when loop terminates
212 asm("tst r3, #1 "); // test not found flag
213 asm("mvnnes r0, #0 "); // if set r0=-1 = KErrNotFound
215 asm("2: "); // N=0 Z=1 C=1 V=0 r0=0 here
216 asm("bics r3, r3, #1 "); // clear NOT FOUND flag, test for find mode ANY (Z set if so)
217 asm("cmpne r3, #0x60000000 "); // HI if LAST, LO if FIRST
218 asm("bne 3b "); // if not ANY, branch back
219 asm("mov r2, r12 "); // if ANY, r2 = mid
220 __JUMP(,lr); // and return with r0 = 0
223 EXPORT_C __NAKED__ TInt RPointerArrayBase::BinarySearch(const TAny* /*anEntry*/, TInt& /*anIndex*/, TGeneralLinearOrder /*anOrder*/, TInt /*aMode*/) const
225 asm("stmfd sp!, {r2,r4-r11,lr} "); // store &anIndex, r4-r11, lr
226 __EH_FRAME_ADDRESS(sp,4)
227 __EH_FRAME_PUSH2(r4-r11,lr)
228 asm("ldmia r0, {r5,r6} "); // r5=count, r6=iEntries
229 asm("ldr r11, [sp, #40] "); // r11 = aMode
230 asm("mov r7, r3 "); // r7=anOrder
231 asm("mov r4, r1 "); // r1=anEntry
232 asm("bl BinarySearchPointers "); // r0=KErrNone if match, KErrNotFound if not
233 asm("ldmfd sp!, {r2,r4} "); // r2=&anIndex, restore r4
234 // Dont need to FRAME RESTORE here since there's no barrier here
235 asm("str r5, [r2] "); // store index
236 __POPRET("r5-r11,"); // restore r5-r11 and return
238 // Binary search list of pointers
239 // Pointer to match value in r4
240 // List address in r6
242 // Pointer to ordering function in r7
244 // Return with: r0=0 if match found, r0=-1 otherwise
245 // Z flag set if match found, clear otherwise
246 // r5=index of match or next higher
247 // r9,r10,r11 modified
248 asm("BinarySearchPointers: ");
250 asm("cmp r11, #%a0" : : "i" ((TInt)EArrayFindMode_Limit) );
251 asm("bhs PanicBadArrayFindMode ");
253 asm("movs r11, r11, lsl #30 "); // match mode -> bits 30,31 (00000000=any, 40000000=first, 80000000=last)
254 asm("eorne r11, r11, #0xC0000000 "); // match mode -> bits 30,31 (00000000=any, 80000000=first, 40000000=last)
255 asm("orr r11, r11, #1 "); // set NOT FOUND flag
257 asm("cmp r5, #0 "); // r5 will be right index
259 asm("mov r10, #0 "); // r10 = left index
261 asm("add r8, r5, r10 ");
262 asm("mov r8, r8, lsr #1 "); // r8 = mid index
264 /** the latency of the indirect call should mask the latency of the ldr
265 arm1136 requires base register to be valid one cycle early
267 asm("mov r0, r4 "); // r0 points to match value
268 asm("ldr r1, [r6, r8, lsl #2] "); // r1 points to entry[mid]
269 __JUMPL(7); // call ordering function (match, entry)
271 asm("biceq r11, r11, #1 "); // if match clear NOT FOUND flag
272 asm("addeqs r0, r0, r11 "); // and add match mode to r0 (>0 if LAST, <0 if FIRST, 0 if ANY)
273 asm("beq 2f "); // branch out if match and ANY
274 asm("addgt r10, r8, #1 "); // else if match > entry, left = mid + 1
275 asm("movlt r5, r8 "); // else if match < entry, right = mid
276 asm("subs r0, r5, r10 "); // loop if right > left
277 asm("bgt 1b "); // finish loop with r0 = 0
279 asm("tst r11, #1 "); // test not found flag
280 asm("mvnnes r0, #0 "); // if set r0=-1 = KErrNotFound
283 asm("mov r5, r8 "); // if ANY, r8 = mid
284 __JUMP(,r9); // and return with r0 = 0, Z=1
287 EXPORT_C __NAKED__ TInt RPointerArrayBase::FindIsqSigned(TInt /*anEntry*/) const
293 EXPORT_C __NAKED__ TInt RPointerArrayBase::FindIsqSigned(TInt /*anEntry*/, TInt /*aMode*/) const
296 // sp needs correct alignment
297 asm("stmfd sp!, {r4-r6,lr} ");
298 __EH_FRAME_PUSH2(r4-r6,lr)
300 asm("stmfd sp!, {r4,r5,lr} ");
302 asm("mov r3, r2 "); // r3 = match mode
303 asm("ldmia r0, {r2,r4} "); // r2=count, r4=iEntries
304 asm("bl BinarySearchSigned ");
305 asm("moveq r0, r2 "); // if match r0=match index; if no match, r0=KErrNotFound
313 EXPORT_C __NAKED__ TInt RPointerArrayBase::FindIsqUnsigned(TUint /*anEntry*/) const
319 EXPORT_C __NAKED__ TInt RPointerArrayBase::FindIsqUnsigned(TUint /*anEntry*/, TInt /*aMode*/) const
322 // sp needs correct alignment
323 asm("stmfd sp!, {r4-r6,lr} ");
324 __EH_FRAME_PUSH2(r4-r6,lr)
326 asm("stmfd sp!, {r4,r5,lr} ");
328 asm("mov r3, r2 "); // r3 = match mode
329 asm("ldmia r0, {r2,r4} "); // r2=count, r4=iEntries
330 asm("bl BinarySearchUnsigned ");
331 asm("moveq r0, r2 "); // if match r0=match index; if no match, r0=KErrNotFound
339 EXPORT_C __NAKED__ TInt RPointerArrayBase::FindIsq(const TAny* /*anEntry*/, TGeneralLinearOrder /*anOrder*/) const
345 EXPORT_C __NAKED__ TInt RPointerArrayBase::FindIsq(const TAny* /*anEntry*/, TGeneralLinearOrder /*anOrder*/, TInt /*aMode*/) const
348 asm("stmfd sp!, {r3-r11,lr} ");
349 __EH_FRAME_PUSH2(r4-r6,lr)
350 asm("ldmia r0, {r5,r6} "); // r5=count, r6=iEntries
351 asm("mov r11, r3 "); // r11 = aMode
352 asm("mov r7, r2 "); // r7=anOrder
353 asm("mov r4, r1 "); // r1=anEntry
354 asm("bl BinarySearchPointers ");
355 asm("moveq r0, r5 "); // if match, r0=match index
359 #ifndef __KERNEL_MODE__
360 EXPORT_C __NAKED__ void RPointerArrayBase::HeapSortSigned()
363 asm("stmfd sp!, {r4-r10,lr} ");
364 __EH_FRAME_PUSH2(r4-r10,lr)
366 asm("stmfd sp!, {r4-r9,lr} ");
368 asm("ldmia r0, {r4,r5} "); // r4=iCount, r5=iEntries
369 asm("bl HeapSortSigned ");
375 // Heap sort list of signed integers
376 // List address in r5, count in r4
377 // r4=ss, r6=sh, r7=si
379 asm("HeapSortSigned: ");
382 asm("mov r6, r4, lsr #1 ");
383 asm("hss_loop_start1: ");
384 asm("sub r6, r6, #1 ");
385 asm("ldr r7, [r5, r6, lsl #2] ");
388 asm("b hss_loop_start2 ");
389 asm("hss_loop_inner: ");
390 asm("ldr r0, [r5, r8, lsl #2] ");
391 asm("str r0, [r5, r9, lsl #2] ");
393 asm("hss_loop_start2: ");
394 asm("add r8, r8, #1 ");
395 asm("add r8, r8, r8 ");
397 asm("bgt hss_loop_inner_end ");
398 asm("add r0, r5, r8, lsl #2 ");
399 asm("ldmneda r0, {r1,r2} ");
400 asm("ldreq r1, [r0, #-4] ");
401 asm("subeq r8, r8, #1 ");
402 asm("beq hss_loop_inner2 ");
404 asm("subgt r8, r8, #1 ");
405 asm("movle r1, r2 ");
406 asm("hss_loop_inner2: ");
408 asm("bgt hss_loop_inner ");
409 asm("hss_loop_inner_end: ");
410 asm("str r7, [r5, r9, lsl #2] ");
412 asm("bne hss_loop_start1 ");
413 asm("sub r4, r4, #1 ");
414 asm("ldr r7, [r5, r4, lsl #2] ");
415 asm("ldr r0, [r5, #0] ");
416 asm("str r0, [r5, r4, lsl #2] ");
420 asm("bgt hss_loop_start2 ");
421 asm("str r7, [r5, #0] ");
425 EXPORT_C __NAKED__ void RPointerArrayBase::HeapSortUnsigned()
427 asm("stmfd sp!, {r4-r9,lr} ");
428 asm("ldmia r0, {r4,r5} "); // r4=iCount, r5=iEntries
429 asm("bl HeapSortUnsigned ");
432 #endif // !__KERNEL_MODE__
434 __NAKED__ void HeapSortUnsigned(TUint* aEntries,TInt aCount)
436 asm("stmfd sp!, {r4-r9,lr} ");
437 asm("mov r4,r1"); // r4=iCount
438 asm("mov r5,r0"); // r5=iEntries
439 asm("bl HeapSortUnsigned ");
442 // Heap sort list of unsigned integers
443 // List address in r5, count in r4
444 // r4=ss, r6=sh, r7=si
446 asm("HeapSortUnsigned: ");
449 asm("mov r6, r4, lsr #1 ");
450 asm("hsu_loop_start1: ");
451 asm("sub r6, r6, #1 ");
452 asm("ldr r7, [r5, r6, lsl #2] ");
455 asm("b hsu_loop_start2 ");
456 asm("hsu_loop_inner: ");
457 asm("ldr r0, [r5, r8, lsl #2] ");
458 asm("str r0, [r5, r9, lsl #2] ");
460 asm("hsu_loop_start2: ");
461 asm("add r8, r8, #1 ");
462 asm("add r8, r8, r8 ");
464 asm("bgt hsu_loop_inner_end ");
465 asm("add r0, r5, r8, lsl #2 ");
466 asm("ldmneda r0, {r1,r2} ");
467 asm("ldreq r1, [r0, #-4] ");
468 asm("subeq r8, r8, #1 ");
469 asm("beq hsu_loop_inner2 ");
471 asm("subhi r8, r8, #1 ");
472 asm("movls r1, r2 ");
473 asm("hsu_loop_inner2: ");
475 asm("bhi hsu_loop_inner ");
476 asm("hsu_loop_inner_end: ");
477 asm("str r7, [r5, r9, lsl #2] ");
479 asm("bne hsu_loop_start1 ");
480 asm("sub r4, r4, #1 ");
481 asm("ldr r7, [r5, r4, lsl #2] ");
482 asm("ldr r0, [r5, #0] ");
483 asm("str r0, [r5, r4, lsl #2] ");
487 asm("bgt hsu_loop_start2 ");
488 asm("str r7, [r5, #0] ");
492 #ifndef __KERNEL_MODE__
493 EXPORT_C __NAKED__ void RPointerArrayBase::HeapSort(TGeneralLinearOrder /*anOrder*/)
495 asm("stmfd sp!, {r3-r11,lr} ");
497 __EH_FRAME_ADDRESS(sp,4)
498 // we can push the callee save regs
499 __EH_FRAME_PUSH2(r4-r11,lr)
500 asm("ldmia r0, {r4,r5} "); // r4=iCount, r5=iEntries
501 asm("mov r10, r1 "); // r10=anOrder
502 asm("bl HeapSortPointers ");
505 // Heap sort list of pointers
506 // List address in r5, count in r4, r10 points to ordering function
507 // r4=ss, r6=sh, r7=si
508 // r8,r9,r11 modified
509 asm("HeapSortPointers: ");
513 asm("mov r6, r4, lsr #1 ");
514 asm("hsp_loop_start1: ");
515 asm("sub r6, r6, #1 ");
516 asm("ldr r7, [r5, r6, lsl #2] ");
519 asm("b hsp_loop_start2 ");
520 asm("hsp_loop_inner: ");
521 asm("ldr r0, [r5, r8, lsl #2] ");
522 asm("str r0, [r5, r9, lsl #2] ");
524 asm("hsp_loop_start2: ");
525 asm("add r8, r8, #1 ");
526 asm("add r8, r8, r8 ");
528 asm("bgt hsp_loop_inner_end ");
529 asm("subeq r8, r8, #1 ");
530 asm("beq hsp_loop_inner2 ");
531 asm("add r0, r5, r8, lsl #2 ");
532 asm("ldmda r0, {r0,r1} ");
535 asm("subgt r8, r8, #1 ");
536 asm("hsp_loop_inner2: ");
537 asm("ldr r0, [r5, r8, lsl #2] ");
541 asm("bgt hsp_loop_inner ");
542 asm("hsp_loop_inner_end: ");
543 asm("str r7, [r5, r9, lsl #2] ");
545 asm("bne hsp_loop_start1 ");
546 asm("sub r4, r4, #1 ");
547 asm("ldr r7, [r5, r4, lsl #2] ");
548 asm("ldr r0, [r5, #0] ");
549 asm("str r0, [r5, r4, lsl #2] ");
553 asm("bgt hsp_loop_start2 ");
554 asm("str r7, [r5, #0] ");
557 #endif // __KERNEL_MODE__
559 EXPORT_C __NAKED__ TAny* RArrayBase::At(TInt /*anIndex*/) const
561 asm("ldmia r0, {r2,r3,r12} "); // r2=iCount, r3=iEntries, r12=iEntrySize
562 asm("cmp r1, #0 "); // check anIndex>=0
563 asm("cmpge r2, r1 "); // if so, check iCount>anIndex
564 asm("mlagt r0, r1, r12, r3 "); // if ok, r0=anIndex*iEntrySize+iEntries
572 asm("b " CSM_Z18PanicBadArrayIndexv);
575 EXPORT_C __NAKED__ TInt RArrayBase::Append(const TAny* /*anEntry*/)
577 asm("stmfd sp!, {lr} ");
578 asm("ldmia r0, {r3,r12} "); // r3=iCount, r12=iEntries
579 asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(RArrayBase,iAllocated));
581 asm("beq simple_append_1 ");
582 asm("simple_append_0: ");
583 asm("add r2, r3, #1 ");
584 asm("str r2, [r0] "); // iCount++
585 asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(RArrayBase,iEntrySize));
586 asm("mla r0, r2, r3, r12 "); // r0=iEntries+iEntrySize*iCount
587 asm("bl wordmove "); // r1=anEntry, r2=iEntrySize, do copy
588 asm("mov r0, #0 "); // return KErrNone;
591 asm("simple_append_1: ");
592 asm("stmfd sp!, {r0,r1,r2} ");
593 asm("bl " CSM_ZN10RArrayBase4GrowEv);
595 asm("bne simple_append_2 ");
596 asm("ldmfd sp!, {r0,r1,r2} ");
597 asm("ldmia r0, {r3, r12} ");
598 asm("b simple_append_0 ");
599 asm("simple_append_2: "); // error enlarging array
600 asm("add sp, sp, #12 ");
604 EXPORT_C __NAKED__ TInt RArrayBase::Find(const TAny* /*anEntry*/) const
606 asm("ldmia r0, {r0,r2,r3,r12} "); // r0=count, r2=iEntries, r3=iEntrySize, r12=iKeyOffset
607 asm("stmfd sp!, {r4,lr} "); // save r4,lr
608 asm("subs r0, r0, #1 "); // r0 = count-1
609 asm("blt 0f "); // skip if count was zero
610 asm("ldr r1, [r1, r12] "); // r1=key of match entry
611 asm("sub r4, r0, #1 "); // r4 = iCount-2
613 asm("ldr lr, [r2, r12] "); // lr=key of current entry
614 asm("add r2, r2, r3 "); // step r2 to next entry
615 asm("subs r0, r0, #1 "); // C=1 iff this isn't last entry
616 asm("teq lr, r1 "); // check for match - C unaffected
617 asm("bhi 1b "); // loop if C=1 & Z=0, i.e. if no match and this isn't last entry
619 asm("mvnne r0, #0 "); // if no match, return -1
620 asm("subeq r0, r4, r0 "); // if match, index = (iCount-2)-r0
624 EXPORT_C __NAKED__ TInt RArrayBase::Find(const TAny* /*anEntry*/, TGeneralIdentityRelation /*anIdentity*/) const
626 asm("stmfd sp!, {r4-r10,lr} "); // save r4-r10,lr
627 __EH_FRAME_PUSH2(r4-r10,lr)
628 asm("ldmia r0, {r4,r5,r6} "); // r4=count, r5=iEntries, r6=iEntrySize
629 asm("mov r8, r1 "); // r8=anEntry
630 asm("mov r9, r2 "); // r9=anIdentity
631 asm("sub r7, r4, #1 "); // r7=iCount-1
632 asm("b simple_find2_start ");
633 asm("simple_find2_loop: ");
634 asm("mov r1, r5 "); // r1->current entry
635 asm("mov r0, r8 "); // r0=anEntry
638 asm("bne simple_find2_return ");
639 asm("add r5, r5, r6 "); // else step to next entry
640 asm("simple_find2_start: ");
641 asm("subs r4, r4, #1 ");
642 asm("bpl simple_find2_loop ");
643 asm("add r4, r7, #1 "); // no match, arrange to return -1
644 asm("simple_find2_return: ");
645 asm("sub r0, r7, r4 "); // index=count-r4
649 EXPORT_C __NAKED__ TInt RArrayBase::BinarySearchSigned(const TAny* /*anEntry*/, TInt& /*anIndex*/) const
655 EXPORT_C __NAKED__ TInt RArrayBase::BinarySearchSigned(const TAny* /*anEntry*/, TInt& /*anIndex*/, TInt /*aMode*/) const
657 asm("stmfd sp!, {r4-r8,lr} ");
658 __EH_FRAME_PUSH2(r4-r8,lr)
659 asm("mov r8, r2 "); // r8=&anIndex
660 asm("ldmia r0, {r2,r4,r5,r6} "); // r2=count, r3=iEntries, r5=entry size, r6=key offset
661 asm("cmp r5, #4 "); // check for 4 byte entries
662 asm("ldr r1, [r1, r6] "); // r1=match key
663 asm("beq 1f "); // if 4 byte entries, call simpler routine
664 asm("bl BinarySearchSignedKey "); // if not, call general routine
667 asm("bl BinarySearchSigned "); // use simpler routine for 4 byte entries
669 asm("str r2, [r8] ");
672 // Binary search list of signed integers
674 // List address in r4
677 // EntrySize in r5, KeyOffset in r6
678 // Return with: r0=0 if match found, r0 nonzero otherwise
679 // r2=index of match or next higher
681 asm("BinarySearchSignedKey: ");
683 asm("cmp r3, #%a0" : : "i" ((TInt)EArrayFindMode_Limit) );
684 asm("bhs PanicBadArrayFindMode ");
686 asm("mov r3, r3, lsl #30 "); // match mode -> bits 30,31 (00000000=any, 40000000=first, 80000000=last)
687 asm("orr r3, r3, #1 "); // set NOT FOUND flag
688 asm("cmp r2, #0 "); // r2 will be right index
690 asm("mov r7, #0 "); // r7 will be left index
692 asm("add r12, r2, r7 ");
693 asm("mov r12, r12, lsr #1 "); // r12 = mid index
694 asm("mla r0, r12, r5, r6 "); // r0 = key offset + entry size * mid index
695 asm("ldr r0, [r4, r0] "); // r0 = key[mid]
696 asm("subs r0, r0, r1 "); // r0 = entry[mid] - match
697 asm("beq 2f "); // if match branch out
699 asm("addlt r7, r12, #1 "); // else if entry<match left=mid+1
700 asm("movgt r2, r12 "); // else if entry>match right=mid
701 asm("subs r0, r2, r7 "); // right > left ?
702 asm("bgt 1b "); // r0 = 0 when loop terminates
704 asm("tst r3, #1 "); // test not found flag
705 asm("mvnnes r0, #0 "); // if set r0=-1 = KErrNotFound
708 asm("bics r3, r3, #1 "); // clear NOT FOUND flag, test for find mode ANY (Z set if so)
709 asm("bne 3b "); // if not, V=0 (left from subs), N=1 for last, 0 for first, Z=0 => LAST->LT FIRST->GT
710 asm("mov r2, r12 "); // if so, r2 = mid
711 __JUMP(,lr); // and return with r0 = 0
714 EXPORT_C __NAKED__ TInt RArrayBase::BinarySearchUnsigned(const TAny* /*anEntry*/, TInt& /*anIndex*/) const
720 EXPORT_C __NAKED__ TInt RArrayBase::BinarySearchUnsigned(const TAny* /*anEntry*/, TInt& /*anIndex*/, TInt /*aMode*/) const
722 asm("stmfd sp!, {r4-r8,lr} ");
723 __EH_FRAME_PUSH2(r4-r8,lr)
724 asm("mov r8, r2 "); // r8=&anIndex
725 asm("ldmia r0, {r2,r4,r5,r6} "); // r2=count, r4=iEntries, r5=entry size, r6=key offset
726 asm("cmp r5, #4 "); // check for 4 byte entries
727 asm("ldr r1, [r1, r6] "); // r1=match key
728 asm("beq 1f "); // if 4 byte entries, call simpler routine
729 asm("bl BinarySearchUnsignedKey "); // if not, call general routine
732 asm("bl BinarySearchUnsigned "); // use simpler routine for 4 byte entries
734 asm("str r2, [r8] ");
737 // Binary search list of unsigned integers
739 // List address in r4
742 // EntrySize in r5, KeyOffset in r6
743 // Return with: r0=0 if match found, r0 nonzero otherwise
744 // r2=index of match or next higher
746 asm("BinarySearchUnsignedKey: ");
748 asm("cmp r3, #%a0" : : "i" ((TInt)EArrayFindMode_Limit) );
749 asm("bhs PanicBadArrayFindMode ");
751 asm("mov r3, r3, lsl #30 "); // match mode -> bits 30,31 (00000000=any, 40000000=first, 80000000=last)
752 asm("orr r3, r3, #1 "); // set NOT FOUND flag
753 asm("cmp r2, #0 "); // r2 will be right index
755 asm("mov r7, #0 "); // r7 will be left index
757 asm("add r12, r2, r7 ");
758 asm("mov r12, r12, lsr #1 "); // r12 = mid index
759 asm("mla r0, r12, r5, r6 "); // r0 = key offset + entry size * mid index
760 asm("ldr r0, [r4, r0] "); // r0 = key[mid]
761 asm("subs r0, r1, r0 "); // r0 = match - entry[mid]
762 asm("beq 2f "); // if match branch out
764 asm("addhi r7, r12, #1 "); // else if entry<match left=mid+1 HI = C &~ Z
765 asm("movlo r2, r12 "); // else if entry>match right=mid LO = ~C
766 asm("subs r0, r2, r7 "); // right > left ?
767 asm("bgt 1b "); // r0 = 0 when loop terminates
769 asm("tst r3, #1 "); // test not found flag
770 asm("mvnnes r0, #0 "); // if set r0=-1 = KErrNotFound
772 asm("2: "); // N=0 Z=1 C=1 V=0 r0=0 here
773 asm("bics r3, r3, #1 "); // clear NOT FOUND flag, test for find mode ANY (Z set if so)
774 asm("cmpne r3, #0x60000000 "); // HI if LAST, LO if FIRST
775 asm("bne 3b "); // if not ANY, branch back
776 asm("mov r2, r12 "); // if ANY, r2 = mid
777 __JUMP(,lr); // and return with r0 = 0
780 EXPORT_C __NAKED__ TInt RArrayBase::BinarySearch(const TAny* /*anEntry*/, TInt& /*anIndex*/, TGeneralLinearOrder /*anOrder*/, TInt /*aMode*/) const
782 asm("stmfd sp!, {r3-r11,lr} ");
784 __EH_FRAME_ADDRESS(sp,4)
785 // we can push the callee save regs
786 __EH_FRAME_PUSH2(r4-r11,lr)
787 asm("ldmia r0, {r5,r6,r11} "); // r5=count, r6=iEntries, r11=entry size
788 asm("ldr r9, [sp, #40] "); // r9 = aMode
789 asm("mov r4, r1 "); // r4=anEntry
790 asm("mov r7, r3 "); // r7=anOrder
791 asm("bl BinarySearchEntries ");
792 asm("str r5, [r2] "); // store index
795 // Binary search list of general entries
796 // Pointer to match value in r4
797 // List address in r6
800 // Pointer to ordering function in r7
802 // Return with: r0=0 if match found, r0 nonzero otherwise
803 // r5=index of match or next higher
806 asm("BinarySearchEntries: ");
808 asm("cmp r9, #%a0" : : "i" ((TInt)EArrayFindMode_Limit) );
809 asm("bhs PanicBadArrayFindMode ");
811 asm("stmfd sp!, {r2,lr} ");
812 asm("movs r9, r9, lsl #30 "); // match mode -> bits 30,31 (00000000=any, 40000000=first, 80000000=last)
813 asm("eorne r9, r9, #0xC0000000 "); // match mode -> bits 30,31 (00000000=any, 80000000=first, 40000000=last)
814 asm("orr r9, r9, #1 "); // set NOT FOUND flag
815 asm("cmp r5, #0 "); // r5 will be right index
817 asm("mov r10, #0 "); // r10 will be left index
819 asm("add r8, r5, r10 ");
820 asm("mov r8, r8, lsr #1 "); // r8 = mid index
821 asm("mla r1, r8, r11, r6 "); // r1 = r8*entry size + list address = &entry[mid]
822 asm("mov r0, r4 "); // r0 points to match value
823 __JUMPL(7); // call ordering function (match, entry)
825 asm("biceq r9, r9, #1 "); // if match clear NOT FOUND flag
826 asm("addeqs r0, r0, r9 "); // and add match mode to r0 (>0 if LAST, <0 if FIRST, 0 if ANY)
827 asm("beq 2f "); // branch out if match and ANY
828 asm("addgt r10, r8, #1 "); // else if match > entry, left = mid + 1
829 asm("movlt r5, r8 "); // else if match < entry, right = mid
830 asm("subs r0, r5, r10 "); // loop if right > left
831 asm("bgt 1b "); // finish loop with r0 = 0
833 asm("tst r9, #1 "); // test not found flag
834 asm("mvnnes r0, #0 "); // if set r0=-1 = KErrNotFound
837 asm("mov r5, r8 "); // if ANY, r8 = mid
838 __POPRET("r2,"); // and return with r0 = 0, Z=1
841 EXPORT_C __NAKED__ TInt RArrayBase::FindIsqSigned(const TAny* /*anEntry*/) const
847 EXPORT_C __NAKED__ TInt RArrayBase::FindIsqSigned(const TAny* /*anEntry*/, TInt /*aMode*/) const
850 // sp needs to be aligned correctly
851 asm("stmfd sp!, {r4-r8,lr} ");
852 __EH_FRAME_PUSH2(r4-r8,lr)
854 asm("stmfd sp!, {r4-r7,lr} ");
856 asm("mov r3, r2 "); // r3 = match mode
857 asm("ldmia r0, {r2,r4,r5,r6} "); // r2=count, r4=iEntries, r5=entry size, r6=key offset
858 asm("cmp r5, #4 "); // check for 4 byte entries
859 asm("ldr r1, [r1, r6] "); // r1=match key
860 asm("beq 1f "); // use simpler routine for 4 byte entries
861 asm("bl BinarySearchSignedKey "); // else call general routine
864 asm("bl BinarySearchSigned ");
866 asm("moveq r0, r2 "); // if match r0=index else r0=KErrNotFound
874 EXPORT_C __NAKED__ TInt RArrayBase::FindIsqUnsigned(const TAny* /*anEntry*/) const
880 EXPORT_C __NAKED__ TInt RArrayBase::FindIsqUnsigned(const TAny* /*anEntry*/, TInt /*aMode*/) const
883 // sp needs to be aligned correctly
884 asm("stmfd sp!, {r4-r8,lr} ");
885 __EH_FRAME_PUSH2(r4-r8,lr)
887 asm("stmfd sp!, {r4-r7,lr} ");
889 asm("mov r3, r2 "); // r3 = match mode
890 asm("ldmia r0, {r2,r4,r5,r6} "); // r2=count, r4=iEntries, r5=entry size, r6=key offset
891 asm("cmp r5, #4 "); // check for 4 byte entries
892 asm("ldr r1, [r1, r6] "); // r1=match key
893 asm("beq 1f "); // use simpler routine for 4 byte entries
894 asm("bl BinarySearchUnsignedKey "); // else call general routine
897 asm("bl BinarySearchUnsigned ");
899 asm("moveq r0, r2 "); // if match r0=index else r0=KErrNotFound
907 EXPORT_C __NAKED__ TInt RArrayBase::FindIsq(const TAny* /*anEntry*/, TGeneralLinearOrder /*anOrder*/) const
913 EXPORT_C __NAKED__ TInt RArrayBase::FindIsq(const TAny* /*anEntry*/, TGeneralLinearOrder /*anOrder*/, TInt /*aMode*/) const
915 asm("stmfd sp!, {r3-r11,lr} ");
917 __EH_FRAME_ADDRESS(sp,4)
918 // we can push the callee save regs
919 __EH_FRAME_PUSH2(r4-r11,lr)
920 asm("ldmia r0, {r5,r6,r11} "); // r5=count, r6=iEntries, r11=entry size
921 asm("mov r4, r1 "); // r4=anEntry
922 asm("mov r7, r2 "); // r7=anOrder
923 asm("mov r9, r3 "); // r9 = aMode
924 asm("bl BinarySearchEntries ");
925 asm("moveq r0, r5 "); // if match r0=index
929 #ifndef __KERNEL_MODE__
930 EXPORT_C __NAKED__ void RArrayBase::HeapSortSigned()
933 // need sp aligned correctly
934 asm("stmfd sp!, {r3-r11,lr} ");
935 __EH_FRAME_ADDRESS(sp,4)
936 __EH_FRAME_PUSH2(r4-r11,lr)
938 asm("stmfd sp!, {r4-r11,lr} ");
940 asm("ldmia r0, {r4,r5,r10,r11} "); // r4=iCount, r5=iEntries, r10=entry size, r11=key offset
942 asm("bleq HeapSortSigned ");
944 asm("blne HeapSortSignedKey ");
951 // Heap sort list of entries by signed key
952 // List address in r5, count in r4, entry size in r10, key offset in r11
955 asm("HeapSortSignedKey: ");
958 asm("mov r7, lr "); // save lr in r7
959 asm("sub sp, sp, r10 "); // get some temporary space on the stack
960 asm("mov r6, r4, lsr #1 ");
961 asm("hssk_loop_start1: ");
962 asm("sub r6, r6, #1 ");
963 asm("mla r1, r6, r10, r5 "); // [sp]=entry[r6]
969 asm("b hssk_loop_start2 ");
970 asm("hssk_loop_inner: ");
971 asm("mla r0, r9, r10, r5 "); // r0=&entry[r9]
972 asm("mla r1, r8, r10, r5 "); // r1=&entry[r8]
974 asm("bl wordmove "); // entry[r9]=entry[r8]
976 asm("hssk_loop_start2: ");
977 asm("add r8, r8, #1 ");
978 asm("add r8, r8, r8 ");
980 asm("bgt hssk_loop_inner_end ");
981 asm("mla r0, r8, r10, r5 ");
982 asm("ldrne r2, [r0, r11]! "); // r2=key[r8]
983 asm("addeq r0, r0, r11 ");
984 asm("ldr r1, [r0, -r10] "); // r1=key[r8-1]
985 asm("subeq r8, r8, #1 ");
986 asm("beq hssk_loop_inner2 ");
988 asm("subgt r8, r8, #1 ");
989 asm("movle r1, r2 ");
990 asm("hssk_loop_inner2: ");
991 asm("ldr r2, [sp, r11] "); // r2=key[sp]
993 asm("bgt hssk_loop_inner ");
994 asm("hssk_loop_inner_end: ");
995 asm("mla r0, r9, r10, r5 "); // r0=&entry[r9]
998 asm("bl wordmove "); // entry[r9]=[sp]
1000 asm("bne hssk_loop_start1 ");
1001 asm("sub r4, r4, #1 ");
1002 asm("mla r1, r4, r10, r5 "); // r1=&entry[r4]
1004 asm("mov r2, r10 ");
1005 asm("bl wordmove "); // [sp]=entry[r4]
1006 asm("mla r0, r4, r10, r5 "); // r0=&entry[r4]
1007 asm("mov r1, r5 "); // r1=&entry[0]
1008 asm("mov r2, r10 ");
1009 asm("bl wordmove "); // entry[0]=entry[r4]
1013 asm("bgt hssk_loop_start2 ");
1014 asm("mov r0, r5 "); // r0=&entry[0]
1016 asm("mov r2, r10 ");
1017 asm("bl wordmove "); // entry[0]=[sp]
1018 asm("add sp, sp, r10 "); // free temporary stack space
1022 EXPORT_C __NAKED__ void RArrayBase::HeapSortUnsigned()
1025 // need sp aligned correctly
1026 asm("stmfd sp!, {r3-r11,lr} ");
1027 __EH_FRAME_ADDRESS(sp,4)
1028 __EH_FRAME_PUSH2(r4-r11,lr)
1030 asm("stmfd sp!, {r4-r11,lr} ");
1032 asm("ldmia r0, {r4,r5,r10,r11} "); // r4=iCount, r5=iEntries, r10=entry size, r11=key offset
1033 asm("cmp r10, #4 ");
1034 asm("bleq HeapSortUnsigned ");
1035 asm("cmp r10, #4 ");
1036 asm("blne HeapSortUnsignedKey ");
1038 __POPRET("r3-r11,");
1040 __POPRET("r4-r11,");
1043 // Heap sort list of entries by unsigned key
1044 // List address in r5, count in r4, entry size in r10, key offset in r11
1047 asm("HeapSortUnsignedKey: ");
1050 asm("mov r7, lr "); // save lr in r7
1051 asm("sub sp, sp, r10 "); // get some temporary space on the stack
1052 asm("mov r6, r4, lsr #1 ");
1053 asm("hsuk_loop_start1: ");
1054 asm("sub r6, r6, #1 ");
1055 asm("mla r1, r6, r10, r5 "); // [sp]=entry[r6]
1057 asm("mov r2, r10 ");
1058 asm("bl wordmove ");
1061 asm("b hsuk_loop_start2 ");
1062 asm("hsuk_loop_inner: ");
1063 asm("mla r0, r9, r10, r5 "); // r0=&entry[r9]
1064 asm("mla r1, r8, r10, r5 "); // r1=&entry[r8]
1065 asm("mov r2, r10 ");
1066 asm("bl wordmove "); // entry[r9]=entry[r8]
1068 asm("hsuk_loop_start2: ");
1069 asm("add r8, r8, #1 ");
1070 asm("add r8, r8, r8 ");
1072 asm("bgt hsuk_loop_inner_end ");
1073 asm("mla r0, r8, r10, r5 ");
1074 asm("ldrne r2, [r0, r11]! "); // r2=key[r8]
1075 asm("addeq r0, r0, r11 ");
1076 asm("ldr r1, [r0, -r10] "); // r1=key[r8-1]
1077 asm("subeq r8, r8, #1 ");
1078 asm("beq hsuk_loop_inner2 ");
1080 asm("subhi r8, r8, #1 ");
1081 asm("movls r1, r2 ");
1082 asm("hsuk_loop_inner2: ");
1083 asm("ldr r2, [sp, r11] "); // r2=key[sp]
1085 asm("bhi hsuk_loop_inner ");
1086 asm("hsuk_loop_inner_end: ");
1087 asm("mla r0, r9, r10, r5 "); // r0=&entry[r9]
1089 asm("mov r2, r10 ");
1090 asm("bl wordmove "); // entry[r9]=[sp]
1092 asm("bne hsuk_loop_start1 ");
1093 asm("sub r4, r4, #1 ");
1094 asm("mla r1, r4, r10, r5 "); // r1=&entry[r4]
1096 asm("mov r2, r10 ");
1097 asm("bl wordmove "); // [sp]=entry[r4]
1098 asm("mla r0, r4, r10, r5 "); // r0=&entry[r4]
1099 asm("mov r1, r5 "); // r1=&entry[0]
1100 asm("mov r2, r10 ");
1101 asm("bl wordmove "); // entry[0]=entry[r4]
1105 asm("bgt hsuk_loop_start2 ");
1106 asm("mov r0, r5 "); // r0=&entry[0]
1108 asm("mov r2, r10 ");
1109 asm("bl wordmove "); // entry[0]=[sp]
1110 asm("add sp, sp, r10 "); // free temporary stack space
1114 EXPORT_C __NAKED__ void RArrayBase::HeapSort(TGeneralLinearOrder anOrder)
1117 // need sp aligned correctly
1118 asm("stmfd sp!, {r3-r11,lr} ");
1119 __EH_FRAME_ADDRESS(sp,4)
1120 __EH_FRAME_PUSH2(r4-r11,lr)
1122 asm("stmfd sp!, {r4-r11,lr} ");
1124 asm("ldmia r0, {r4,r5,r10,r11} ");
1126 asm("bl HeapSortEntries ");
1128 __POPRET("r3-r11,");
1130 __POPRET("r4-r11,");
1133 // Heap sort list of entries
1134 // List address in r5, count in r4, entry size in r10, key offset in r11
1135 // Address of ordering function in r7
1138 asm("HeapSortEntries: ");
1141 asm("str lr, [sp, #-4]! ");
1142 asm("mov r8, sp "); // original SP
1143 asm("sub sp, sp, r10 "); // get some temporary space on the stack
1144 asm("sub sp, sp, #4 "); // make room for original SP
1145 asm("bic sp, sp, #4 "); // align stack to 8 byte boundary
1146 asm("str r8, [sp, r10] "); // save original SP
1147 asm("mov r6, r4, lsr #1 ");
1148 asm("hse_loop_start1: ");
1149 asm("sub r6, r6, #1 ");
1150 asm("mla r1, r6, r10, r5 "); // [sp]=entry[r6]
1152 asm("mov r2, r10 ");
1153 asm("bl wordmove ");
1156 asm("b hse_loop_start2 ");
1157 asm("hse_loop_inner: ");
1158 asm("mla r0, r9, r10, r5 "); // r0=&entry[r9]
1159 asm("mla r1, r8, r10, r5 "); // r1=&entry[r8]
1160 asm("mov r2, r10 ");
1161 asm("bl wordmove "); // entry[r9]=entry[r8]
1163 asm("hse_loop_start2: ");
1164 asm("add r8, r8, #1 ");
1165 asm("add r8, r8, r8 ");
1167 asm("bgt hse_loop_inner_end ");
1168 asm("subeq r8, r8, #1 ");
1169 asm("beq hse_loop_inner2 ");
1170 asm("mla r1, r8, r10, r5 "); // r1=&entry[r8]
1171 asm("sub r0, r1, r10 "); // r0=&entry[r8-1]
1173 asm("cmp r0, #0 "); // compare entry[r8-1] with entry[r8]
1174 asm("subgt r8, r8, #1 ");
1175 asm("hse_loop_inner2: ");
1176 asm("mla r0, r8, r10, r5 "); // r0=&entry[r8]
1179 asm("cmp r0, #0 "); // compare entry[r8] with [sp]
1180 asm("bgt hse_loop_inner ");
1181 asm("hse_loop_inner_end: ");
1182 asm("mla r0, r9, r10, r5 "); // r0=&entry[r9]
1184 asm("mov r2, r10 ");
1185 asm("bl wordmove "); // entry[r9]=[sp]
1187 asm("bne hse_loop_start1 ");
1188 asm("sub r4, r4, #1 ");
1189 asm("mla r1, r4, r10, r5 "); // r1=&entry[r4]
1191 asm("mov r2, r10 ");
1192 asm("bl wordmove "); // [sp]=entry[r4]
1193 asm("mla r0, r4, r10, r5 "); // r0=&entry[r4]
1194 asm("mov r1, r5 "); // r1=&entry[0]
1195 asm("mov r2, r10 ");
1196 asm("bl wordmove "); // entry[0]=entry[r4]
1200 asm("bgt hse_loop_start2 ");
1201 asm("mov r0, r5 "); // r0=&entry[0]
1203 asm("mov r2, r10 ");
1204 asm("bl wordmove "); // entry[0]=[sp]
1205 asm("ldr sp, [sp, r10] "); // restore stack pointer, freeing temporary stack space
1208 #endif // __KERNEL_MODE__
1209 #endif // __ARRAY_MACHINE_CODED__