1 /*
2 * strlen.c -- strlen function. On at least some MIPS chips, a simple
3 * strlen is faster than the 'optimized' C version.
4 *
5 * Copyright (c) 2001, 2002 Red Hat, Inc.
6 *
7 * The authors hereby grant permission to use, copy, modify, distribute,
8 * and license this software and its documentation for any purpose, provided
9 * that existing copyright notices are retained in all copies and that this
10 * notice is included verbatim in any distributions. No written agreement,
11 * license, or royalty fee is required for any of the authorized uses.
12 * Modifications to this software may be copyrighted by their authors
13 * and need not follow the licensing terms described here, provided that
14 * the new terms are clearly indicated on the first page of each file where
15 * they apply.
16 */
17
18 #include <picolibc.h>
19
20 #include <stddef.h>
21 #include <string.h>
22
23 /* MIPS16 needs to come first. */
24
25 #if defined(__mips16)
26 size_t
strlen(const char * str)27 strlen (const char *str)
28 {
29 const char *start = str;
30
31 while (*str++ != '\0')
32 ;
33
34 return str - start - 1;
35 }
36 #elif defined(__mips64)
37 __asm__("" /* 64-bit MIPS targets */
38 " .set noreorder\n"
39 " .set nomacro\n"
40 " .globl strlen\n"
41 " .ent strlen\n"
42 "strlen:\n"
43 " daddiu $2,$4,1\n"
44 "\n"
45 "1: lbu $3,0($4)\n"
46 " bnez $3,1b\n"
47 " daddiu $4,$4,1\n"
48 "\n"
49 " jr $31\n"
50 " dsubu $2,$4,$2\n"
51 " .end strlen\n"
52 " .set macro\n"
53 " .set reorder\n");
54
55 #else
56 __asm__("" /* 32-bit MIPS targets */
57 " .set noreorder\n"
58 " .set nomacro\n"
59 " .globl strlen\n"
60 " .ent strlen\n"
61 "strlen:\n"
62 " addiu $2,$4,1\n"
63 "\n"
64 "1: lbu $3,0($4)\n"
65 #if defined(_R3000)
66 " nop \n"
67 #endif
68 " bnez $3,1b\n"
69 " addiu $4,$4,1\n"
70 "\n"
71 " jr $31\n"
72 " subu $2,$4,$2\n"
73 " .end strlen\n"
74 " .set macro\n"
75 " .set reorder\n");
76 #endif
77