~linaro-toolchain-dev/cortex-strings/trunk

« back to all changes in this revision

Viewing changes to src/linaro-a9/strlen.s

  • Committer: Dr. David Alan Gilbert
  • Date: 2011-02-10 18:39:03 UTC
  • Revision ID: david.gilbert@linaro.org-20110210183903-35d3te7ercbhv1d4
Import of fast memchr, strlen and simple strchr from Dave Gilbert's repo

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
       .syntax unified
 
2
        .arch armv7-a
 
3
 
 
4
@ Copyright (c) 2010-2011, Linaro Limited
 
5
@ All rights reserved.
 
6
 
 
7
@ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 
8
 
 
9
@    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 
10
@    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 
11
@    * Neither the name of Linaro Limited nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 
12
 
 
13
@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
14
 
 
15
@ Written by Dave Gilbert <david.gilbert@linaro.org>
 
16
 
 
17
@ This strlen routine is optimised on a Cortex-A9 and should work on all ARMv7
 
18
@ processors.   This routine is reasonably fast for short strings, but is
 
19
@ probably slower than a simple implementation if all your strings are very short
 
20
 
 
21
@ 2011-02-08 david.gilbert@linaro.org
 
22
@    Extracted from local git 6848613a
 
23
 
 
24
 
 
25
@-----------------------------------------------------------------------------------------------------------------------------
 
26
        .thumb_func
 
27
        .align 2
 
28
        .p2align 4,,15
 
29
        .global strlen
 
30
        .type strlen,%function
 
31
strlen:
 
32
  @ r0 = string
 
33
  @ returns count of bytes in string not including terminator
 
34
  mov     r1, r0
 
35
  push    { r4,r6 }
 
36
  mvns    r6, #0     @ all F
 
37
  movs    r4, #0
 
38
  tst     r0, #7
 
39
  beq     2f
 
40
 
 
41
1:
 
42
  ldrb  r2, [r1], #1
 
43
  tst   r1, #7       @ Hit alignment yet?
 
44
  cbz   r2, 10f      @ Exit if we found the 0
 
45
  bne   1b
 
46
 
 
47
  @ So we're now aligned
 
48
2:
 
49
  ldmia r1!,{r2,r3}
 
50
  uadd8 r2, r2, r6   @ Parallel add 0xff - sets the GE bits for anything that wasn't 0
 
51
  sel   r2, r4, r6   @ bytes are 00 for none-00 bytes, or ff for 00 bytes - NOTE INVERSION
 
52
  uadd8 r3, r3, r6   @ Parallel add 0xff - sets the GE bits for anything that wasn't 0
 
53
  sel   r3, r2, r6   @ bytes are 00 for none-00 bytes, or ff for 00 bytes - NOTE INVERSION
 
54
  cmp   r3, #0
 
55
  beq   2b
 
56
 
 
57
strlenendtmp:
 
58
  @ One (or more) of the bytes we loaded was 0 - but which one?
 
59
  @ r2 has the mask corresponding to the first loaded word
 
60
  @ r3 has a combined mask of the two words - but if r2 was all-non 0 
 
61
  @ then it's just the 2nd words
 
62
  cmp   r2, #0
 
63
  itte  eq
 
64
  moveq r2, r3        @ the end is in the 2nd word
 
65
  subeq r1,r1,#3
 
66
  subne r1,r1,#7
 
67
 
 
68
  @ r1 currently points to the 2nd byte of the word containing the 0
 
69
  tst   r2, # (1<<0)  @ 1st character
 
70
  bne   10f
 
71
  adds  r1,r1,#1
 
72
  tst   r2, # (1<<8)  @ 2nd character
 
73
  ittt  eq
 
74
  addeq r1,r1,#1
 
75
  tsteq r2, # (3<<15) @ 2nd & 3rd character
 
76
  @ If not the 3rd must be the last one
 
77
  addeq   r1,r1,#1
 
78
 
 
79
10:
 
80
  @ r0 is still at the beginning, r1 is pointing 1 byte after the terminator
 
81
  sub   r0, r1, r0
 
82
  subs  r0, r0, #1
 
83
  pop   { r4, r6 }
 
84
  bx    lr
 
85