PS2SDK
PS2 Homebrew Libraries
strlen.c
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 <stddef.h>
19 #include <string.h>
20 
21 /* MIPS16 needs to come first. */
22 
23 #if defined(__mips16)
24 size_t
25 strlen (const char *str)
26 {
27  const char *start = str;
28 
29  while (*str++ != '\0')
30  ;
31 
32  return str - start - 1;
33 }
34 #elif defined(__mips64)
35 __asm__("" /* 64-bit MIPS targets */
36  " .set noreorder\n"
37  " .set nomacro\n"
38  " .globl strlen\n"
39  " .ent strlen\n"
40  "strlen:\n"
41  " daddiu $2,$4,1\n"
42  "\n"
43  "1: lbu $3,0($4)\n"
44  " bnez $3,1b\n"
45  " daddiu $4,$4,1\n"
46  "\n"
47  " jr $31\n"
48  " dsubu $2,$4,$2\n"
49  " .end strlen\n"
50  " .set macro\n"
51  " .set reorder\n");
52 
53 #else
54 __asm__("" /* 32-bit MIPS targets */
55  " .set noreorder\n"
56  " .set nomacro\n"
57  " .globl strlen\n"
58  " .ent strlen\n"
59  "strlen:\n"
60  " addiu $2,$4,1\n"
61  "\n"
62  "1: lbu $3,0($4)\n"
63 #if defined(_R3000)
64  " nop \n"
65 #endif
66  " bnez $3,1b\n"
67  " addiu $4,$4,1\n"
68  "\n"
69  " jr $31\n"
70  " subu $2,$4,$2\n"
71  " .end strlen\n"
72  " .set macro\n"
73  " .set reorder\n");
74 #endif