xref: /freebsd/sys/libkern/memcmp.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1ae72afe0SDavid E. O'Brien /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4ae72afe0SDavid E. O'Brien  * Copyright (c) 1990, 1993
5ae72afe0SDavid E. O'Brien  *	The Regents of the University of California.  All rights reserved.
6ae72afe0SDavid E. O'Brien  *
7ae72afe0SDavid E. O'Brien  * This code is derived from software contributed to Berkeley by
8ae72afe0SDavid E. O'Brien  * Chris Torek.
9ae72afe0SDavid E. O'Brien  *
10ae72afe0SDavid E. O'Brien  * Redistribution and use in source and binary forms, with or without
11ae72afe0SDavid E. O'Brien  * modification, are permitted provided that the following conditions
12ae72afe0SDavid E. O'Brien  * are met:
13ae72afe0SDavid E. O'Brien  * 1. Redistributions of source code must retain the above copyright
14ae72afe0SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer.
15ae72afe0SDavid E. O'Brien  * 2. Redistributions in binary form must reproduce the above copyright
16ae72afe0SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer in the
17ae72afe0SDavid E. O'Brien  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19ae72afe0SDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
20ae72afe0SDavid E. O'Brien  *    without specific prior written permission.
21ae72afe0SDavid E. O'Brien  *
22ae72afe0SDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23ae72afe0SDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24ae72afe0SDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25ae72afe0SDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26ae72afe0SDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27ae72afe0SDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28ae72afe0SDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29ae72afe0SDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30ae72afe0SDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31ae72afe0SDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32ae72afe0SDavid E. O'Brien  * SUCH DAMAGE.
33ae72afe0SDavid E. O'Brien  */
34ae72afe0SDavid E. O'Brien 
35ae72afe0SDavid E. O'Brien #include <sys/cdefs.h>
36ae72afe0SDavid E. O'Brien #include <sys/libkern.h>
37ae72afe0SDavid E. O'Brien 
38ae72afe0SDavid E. O'Brien /*
39ae72afe0SDavid E. O'Brien  * Compare memory regions.
40ae72afe0SDavid E. O'Brien  */
41ae72afe0SDavid E. O'Brien int
42*ba96f377SMateusz Guzik (memcmp)(const void *s1, const void *s2, size_t n)
43ae72afe0SDavid E. O'Brien {
44ae72afe0SDavid E. O'Brien 	if (n != 0) {
45ae72afe0SDavid E. O'Brien 		const unsigned char *p1 = s1, *p2 = s2;
46ae72afe0SDavid E. O'Brien 
47ae72afe0SDavid E. O'Brien 		do {
48ae72afe0SDavid E. O'Brien 			if (*p1++ != *p2++)
49ae72afe0SDavid E. O'Brien 				return (*--p1 - *--p2);
50ae72afe0SDavid E. O'Brien 		} while (--n != 0);
51ae72afe0SDavid E. O'Brien 	}
52ae72afe0SDavid E. O'Brien 	return (0);
53ae72afe0SDavid E. O'Brien }
54