Faster assembler strlen for coldfire using the load-a-whole-word-and-test-i-for-nullb...
[kugel-rb.git] / firmware / target / coldfire / strlen-coldfire.S
blobf1e5aca9816d359eaa2c03abb9a42fdd36f0874f
1 /***************************************************************************
2  *             __________               __   ___.
3  *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
4  *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
5  *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
6  *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
7  *                     \/            \/     \/    \/            \/
8  * $Id$
9  *
10  * Copyright (C) 2010 Nils Wallménius
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18  * KIND, either express or implied.
19  *
20  ****************************************************************************/
22 /* size_t strlen(const char *str) */
24     .section .text,"ax",@progbits
25     .align   2
26     .globl   strlen
27     .type    strlen, @function
29 strlen:
30     move.l   4(%sp), %a0    /* %a0 = *str */
31     move.l   %a0, %a1       /* %a1 = start address */
32     move.l   %a0, %d0
33     andi.l   #3, %d0        /* %d0 = %a0 & 3 */
34     jmp.l    (2,%pc,%d0.l*2)
35     bra.b    .bytes0
36     bra.b    .bytes3
37     bra.b    .bytes2
38     bra.b    .bytes1
39 .bytes3:
40     tst.b    (%a0)+
41     beq.b    .done
42 .bytes2:
43     tst.b    (%a0)+
44     beq.b    .done
45 .bytes1:
46     tst.b    (%a0)+
47     beq.b    .done
48 .bytes0:
50     1:
51     move.l   (%a0)+, %d0    /* load %d0 increment %a0 */
52     /* use trick to test the whole word for null bytes */
53     move.l   %d0, %d1
54     subi.l   #0x01010101, %d1
55     not.l    %d0
56     and.l    %d1, %d0
57     andi.l   #0x80808080, %d0
58     beq.b    1b             /* if the test was false repeat */
60  /* ok, so the last word contained a 0 byte, test individual bytes */
61     subq.l   #4, %a0
62     tst.b    (%a0)+
63     beq.b    .done
64     tst.b    (%a0)+
65     beq.b    .done
66     tst.b    (%a0)+
67     beq.b    .done
68  /* last byte must be 0 so we don't need to load it, so we don't increment a0
69     so we jump past the subq instr */
70     .word    0x51fa         /* trapf.w, shadow next instr */
72 .done:
73     subq.l   #1, %a0        /* %a0 is 1 too large due to the last increment */
74     sub.l    %a1, %a0       /* how many times did we repeat? */
75     move.l   %a0, %d0       /* return value in %d0 */
76     rts
77     .size    strlen, .-strlen