yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* Determine the length of a string. |
| 2 | For SPARC v7. |
| 3 | Copyright (C) 1996, 1999, 2003 Free Software Foundation, Inc. |
| 4 | This file is part of the GNU C Library. |
| 5 | Contributed by Jakub Jelinek <jj@ultra.linux.cz>. |
| 6 | |
| 7 | The GNU C Library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | The GNU C Library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with the GNU C Library; if not, write to the Free |
| 19 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307 USA. */ |
| 21 | |
| 22 | /* Normally, this uses ((xword - 0x01010101) & 0x80808080) test |
| 23 | to find out if any byte in xword could be zero. This is fast, but |
| 24 | also gives false alarm for any byte in range 0x81-0xff. It does |
| 25 | not matter for correctness, as if this test tells us there could |
| 26 | be some zero byte, we check it byte by byte, but if bytes with |
| 27 | high bits set are common in the strings, then this will give poor |
| 28 | performance. You can #define EIGHTBIT_NOT_RARE and the algorithm |
| 29 | will use one tick slower, but more precise test |
| 30 | ((xword - 0x01010101) & (~xword) & 0x80808080), |
| 31 | which does not give any false alarms (but if some bits are set, |
| 32 | one cannot assume from it which bytes are zero and which are not). |
| 33 | It is yet to be measured, what is the correct default for glibc |
| 34 | in these days for an average user. |
| 35 | */ |
| 36 | |
| 37 | .text |
| 38 | .align 4 |
| 39 | 10: ldub [%o0], %o5 |
| 40 | cmp %o5, 0 |
| 41 | be 1f |
| 42 | add %o0, 1, %o0 |
| 43 | andcc %o0, 3, %g0 |
| 44 | be 4f |
| 45 | or %o4, %lo(0x80808080), %o3 |
| 46 | ldub [%o0], %o5 |
| 47 | cmp %o5, 0 |
| 48 | be 2f |
| 49 | add %o0, 1, %o0 |
| 50 | andcc %o0, 3, %g0 |
| 51 | be 5f |
| 52 | sethi %hi(0x01010101), %o4 |
| 53 | ldub [%o0], %o5 |
| 54 | cmp %o5, 0 |
| 55 | be 3f |
| 56 | add %o0, 1, %o0 |
| 57 | b 11f |
| 58 | or %o4, %lo(0x01010101), %o2 |
| 59 | 1: retl |
| 60 | mov 0, %o0 |
| 61 | 2: retl |
| 62 | mov 1, %o0 |
| 63 | 3: retl |
| 64 | mov 2, %o0 |
| 65 | |
| 66 | ENTRY(strlen) |
| 67 | mov %o0, %o1 |
| 68 | andcc %o0, 3, %g0 |
| 69 | bne 10b |
| 70 | sethi %hi(0x80808080), %o4 |
| 71 | or %o4, %lo(0x80808080), %o3 |
| 72 | 4: sethi %hi(0x01010101), %o4 |
| 73 | 5: or %o4, %lo(0x01010101), %o2 |
| 74 | 11: ld [%o0], %o5 |
| 75 | 12: sub %o5, %o2, %o4 |
| 76 | #ifdef EIGHTBIT_NOT_RARE |
| 77 | andn %o4, %o5, %o4 |
| 78 | #endif |
| 79 | andcc %o4, %o3, %g0 |
| 80 | be 11b |
| 81 | add %o0, 4, %o0 |
| 82 | |
| 83 | srl %o5, 24, %g5 |
| 84 | andcc %g5, 0xff, %g0 |
| 85 | be 13f |
| 86 | add %o0, -4, %o4 |
| 87 | srl %o5, 16, %g5 |
| 88 | andcc %g5, 0xff, %g0 |
| 89 | be 13f |
| 90 | add %o4, 1, %o4 |
| 91 | srl %o5, 8, %g5 |
| 92 | andcc %g5, 0xff, %g0 |
| 93 | be 13f |
| 94 | add %o4, 1, %o4 |
| 95 | andcc %o5, 0xff, %g0 |
| 96 | bne,a 12b |
| 97 | ld [%o0], %o5 |
| 98 | add %o4, 1, %o4 |
| 99 | 13: retl |
| 100 | sub %o4, %o1, %o0 |
| 101 | END(strlen) |
| 102 | libc_hidden_def(strlen) |