1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc.  Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 /*
18 FUNCTION
19 	<<strcmp>>---character string compare
20 
21 INDEX
22 	strcmp
23 
24 SYNOPSIS
25 	#include <string.h>
26 	int strcmp(const char *<[a]>, const char *<[b]>);
27 
28 DESCRIPTION
29 	<<strcmp>> compares the string at <[a]> to
30 	the string at <[b]>.
31 
32 RETURNS
33 	If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>,
34 	<<strcmp>> returns a number greater than zero.  If the two
35 	strings match, <<strcmp>> returns zero.  If <<*<[a]>>>
36 	sorts lexicographically before <<*<[b]>>>, <<strcmp>> returns a
37 	number less than zero.
38 
39 PORTABILITY
40 <<strcmp>> is ANSI C.
41 
42 <<strcmp>> requires no supporting OS subroutines.
43 
44 QUICKREF
45 	strcmp ansi pure
46 */
47 
48 #include <string.h>
49 #include <limits.h>
50 #include <stdint.h>
51 
52 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
53 #define UNALIGNED(X, Y) \
54   (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
55 
56 /* DETECTNULL returns nonzero if (long)X contains a NULL byte. */
57 #if LONG_MAX == 2147483647L
58 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
59 #else
60 #if LONG_MAX == 9223372036854775807L
61 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
62 #else
63 #error long int is not a 32bit or 64bit type.
64 #endif
65 #endif
66 
67 #ifndef DETECTNULL
68 #error long int is not a 32bit or 64bit byte
69 #endif
70 
71 int
strcmp(const char * s1,const char * s2)72 strcmp (const char *s1,
73 	const char *s2)
74 {
75 #if ((defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)) && !defined(FAST_STRCMP)) \
76     || defined(PICOLIBC_NO_OUT_OF_BOUNDS_READS)
77   while (*s1 != '\0' && *s1 == *s2)
78     {
79       s1++;
80       s2++;
81     }
82 
83   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
84 #else
85   unsigned long *a1;
86   unsigned long *a2;
87 
88   /* If s1 or s2 are unaligned, then compare bytes. */
89   if (!UNALIGNED (s1, s2))
90     {
91       /* If s1 and s2 are word-aligned, compare them a word at a time. */
92       a1 = (unsigned long*)s1;
93       a2 = (unsigned long*)s2;
94       while (*a1 == *a2)
95         {
96           /* To get here, *a1 == *a2, thus if we find a null in *a1,
97 	     then the strings must be equal, so return zero.  */
98           if (DETECTNULL (*a1))
99 	    return 0;
100 
101           a1++;
102           a2++;
103         }
104 
105       /* A difference was detected in last few bytes of s1, so search bytewise */
106       s1 = (char*)a1;
107       s2 = (char*)a2;
108     }
109 
110   while (*s1 != '\0' && *s1 == *s2)
111     {
112       s1++;
113       s2++;
114     }
115   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
116 #endif /* not PREFER_SIZE_OVER_SPEED */
117 }
118