\hypertarget{memcchr_8c_source}{}\doxysection{memcchr.\+c}
\label{memcchr_8c_source}\index{libkern/memcchr.c@{libkern/memcchr.c}}
\mbox{\hyperlink{memcchr_8c}{Go to the documentation of this file.}}
\begin{DoxyCode}{0}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00001}00001 \textcolor{comment}{/*-\/}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00002}00002 \textcolor{comment}{ * SPDX-\/License-\/Identifier: BSD-\/2-\/Clause-\/FreeBSD}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00003}00003 \textcolor{comment}{ *}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00004}00004 \textcolor{comment}{ * Copyright (c) 2012 Ed Schouten <ed@FreeBSD.org>}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00005}00005 \textcolor{comment}{ * All rights reserved.}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00006}00006 \textcolor{comment}{ *}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00007}00007 \textcolor{comment}{ * Redistribution and use in source and binary forms, with or without}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00008}00008 \textcolor{comment}{ * modification, are permitted provided that the following conditions}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00009}00009 \textcolor{comment}{ * are met:}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00010}00010 \textcolor{comment}{ * 1. Redistributions of source code must retain the above copyright}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00011}00011 \textcolor{comment}{ *    notice, this list of conditions and the following disclaimer.}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00012}00012 \textcolor{comment}{ * 2. Redistributions in binary form must reproduce the above copyright}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00013}00013 \textcolor{comment}{ *    notice, this list of conditions and the following disclaimer in the}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00014}00014 \textcolor{comment}{ *    documentation and/or other materials provided with the distribution.}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00015}00015 \textcolor{comment}{ *}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00016}00016 \textcolor{comment}{ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00017}00017 \textcolor{comment}{ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00018}00018 \textcolor{comment}{ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00019}00019 \textcolor{comment}{ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00020}00020 \textcolor{comment}{ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00021}00021 \textcolor{comment}{ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00022}00022 \textcolor{comment}{ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00023}00023 \textcolor{comment}{ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00024}00024 \textcolor{comment}{ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00025}00025 \textcolor{comment}{ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00026}00026 \textcolor{comment}{ * SUCH DAMAGE.}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00027}00027 \textcolor{comment}{ */}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00028}00028 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00029}00029 \textcolor{preprocessor}{\#include <sys/cdefs.h>}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00030}\mbox{\hyperlink{memcchr_8c_a8c3ef283470a68cd30afe9b8a72cb60f}{00030}} \mbox{\hyperlink{memcchr_8c_a8c3ef283470a68cd30afe9b8a72cb60f}{\_\_FBSDID}}(\textcolor{stringliteral}{"{}\$FreeBSD\$"{}});}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00031}00031 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00032}00032 \textcolor{preprocessor}{\#include <sys/libkern.h>}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00033}00033 \textcolor{preprocessor}{\#include <sys/limits.h>}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00034}00034 \textcolor{preprocessor}{\#include <sys/param.h>}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00035}00035 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00036}00036 \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00037}00037 \textcolor{comment}{ * memcchr(): find first character in buffer not matching `c'.}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00038}00038 \textcolor{comment}{ *}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00039}00039 \textcolor{comment}{ * This function performs the complement of memchr().  To provide decent}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00040}00040 \textcolor{comment}{ * performance, this function compares data from the buffer one word at}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00041}00041 \textcolor{comment}{ * a time.}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00042}00042 \textcolor{comment}{ *}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00043}00043 \textcolor{comment}{ * This code is inspired by libc's strlen(), written by Xin Li.}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00044}00044 \textcolor{comment}{ */}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00045}00045 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00046}00046 \textcolor{preprocessor}{\#if LONG\_BIT != 32 \&\& LONG\_BIT != 64}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00047}00047 \textcolor{preprocessor}{\#error Unsupported word size}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00048}00048 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00049}00049 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00050}\mbox{\hyperlink{memcchr_8c_ae33e2dfb5c1456c17782ba1e0894d783}{00050}} \textcolor{preprocessor}{\#define LONGPTR\_MASK (sizeof(long) -\/ 1)}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00051}00051 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00052}\mbox{\hyperlink{memcchr_8c_ae9b3bf7ebc71922cb8010c2a2bfec7fc}{00052}} \textcolor{preprocessor}{\#define TESTBYTE                                \(\backslash\)}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00053}00053 \textcolor{preprocessor}{        do \{                                    \(\backslash\)}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00054}00054 \textcolor{preprocessor}{                if (*p != (unsigned char)c)     \(\backslash\)}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00055}00055 \textcolor{preprocessor}{                        goto done;              \(\backslash\)}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00056}00056 \textcolor{preprocessor}{                p++;                            \(\backslash\)}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00057}00057 \textcolor{preprocessor}{        \} while (0)}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00058}00058 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00059}00059 \textcolor{keywordtype}{void} *}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00060}\mbox{\hyperlink{memcchr_8c_a63d59e6985b764b409b00ffcab353e37}{00060}} \mbox{\hyperlink{memcchr_8c_a63d59e6985b764b409b00ffcab353e37}{memcchr}}(\textcolor{keyword}{const} \textcolor{keywordtype}{void} *begin, \textcolor{keywordtype}{int} \mbox{\hyperlink{iconv__converter__if_8m_a4e1e0e72dd773439e333c84dd762a9c3}{c}}, \textcolor{keywordtype}{size\_t} n)}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00061}00061 \{}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00062}00062         \textcolor{keyword}{const} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{long} *lp;}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00063}00063         \textcolor{keyword}{const} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} *p, *end;}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00064}00064         \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{long} \mbox{\hyperlink{bcopy_8c_af03d9538f05191fade94d45d8ff21e1a}{word}};}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00065}00065 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00066}00066         \textcolor{comment}{/* Four or eight repetitions of `c'. */}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00067}00067         \mbox{\hyperlink{bcopy_8c_af03d9538f05191fade94d45d8ff21e1a}{word}} = (\textcolor{keywordtype}{unsigned} char)\mbox{\hyperlink{iconv__converter__if_8m_a4e1e0e72dd773439e333c84dd762a9c3}{c}};}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00068}00068         \mbox{\hyperlink{bcopy_8c_af03d9538f05191fade94d45d8ff21e1a}{word}} |= \mbox{\hyperlink{bcopy_8c_af03d9538f05191fade94d45d8ff21e1a}{word}} << 8;}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00069}00069         \mbox{\hyperlink{bcopy_8c_af03d9538f05191fade94d45d8ff21e1a}{word}} |= \mbox{\hyperlink{bcopy_8c_af03d9538f05191fade94d45d8ff21e1a}{word}} << 16;}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00070}00070 \textcolor{preprocessor}{\#if LONG\_BIT >= 64}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00071}00071         \mbox{\hyperlink{bcopy_8c_af03d9538f05191fade94d45d8ff21e1a}{word}} |= \mbox{\hyperlink{bcopy_8c_af03d9538f05191fade94d45d8ff21e1a}{word}} << 32;}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00072}00072 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00073}00073 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00074}00074         \textcolor{comment}{/* Don't perform memory I/O when passing a zero-\/length buffer. */}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00075}00075         \textcolor{keywordflow}{if} (n == 0)}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00076}00076                 \textcolor{keywordflow}{return} (NULL);}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00077}00077 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00078}00078         \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00079}00079 \textcolor{comment}{         * First determine whether there is a character unequal to `c'}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00080}00080 \textcolor{comment}{         * in the first word.  As this word may contain bytes before}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00081}00081 \textcolor{comment}{         * `begin', we may execute this loop spuriously.}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00082}00082 \textcolor{comment}{         */}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00083}00083         lp = (\textcolor{keyword}{const} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{long} *)((uintptr\_t)begin \& \string~LONGPTR\_MASK);}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00084}00084         end = (\textcolor{keyword}{const} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} *)begin + n;}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00085}00085         \textcolor{keywordflow}{if} (*lp++ != \mbox{\hyperlink{bcopy_8c_af03d9538f05191fade94d45d8ff21e1a}{word}})}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00086}00086                 \textcolor{keywordflow}{for} (p = begin; p < (\textcolor{keyword}{const} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} *)lp;)}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00087}00087                         \mbox{\hyperlink{memcchr_8c_ae9b3bf7ebc71922cb8010c2a2bfec7fc}{TESTBYTE}};}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00088}00088 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00089}00089         \textcolor{comment}{/* Now compare the data one word at a time. */}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00090}00090         \textcolor{keywordflow}{for} (; (\textcolor{keyword}{const} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} *)lp < end; lp++) \{}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00091}00091                 \textcolor{keywordflow}{if} (*lp != \mbox{\hyperlink{bcopy_8c_af03d9538f05191fade94d45d8ff21e1a}{word}}) \{}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00092}00092                         p = (\textcolor{keyword}{const} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} *)lp;}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00093}00093                         \mbox{\hyperlink{memcchr_8c_ae9b3bf7ebc71922cb8010c2a2bfec7fc}{TESTBYTE}};}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00094}00094                         \mbox{\hyperlink{memcchr_8c_ae9b3bf7ebc71922cb8010c2a2bfec7fc}{TESTBYTE}};}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00095}00095                         \mbox{\hyperlink{memcchr_8c_ae9b3bf7ebc71922cb8010c2a2bfec7fc}{TESTBYTE}};}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00096}00096 \textcolor{preprocessor}{\#if LONG\_BIT >= 64}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00097}00097                         \mbox{\hyperlink{memcchr_8c_ae9b3bf7ebc71922cb8010c2a2bfec7fc}{TESTBYTE}};}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00098}00098                         \mbox{\hyperlink{memcchr_8c_ae9b3bf7ebc71922cb8010c2a2bfec7fc}{TESTBYTE}};}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00099}00099                         \mbox{\hyperlink{memcchr_8c_ae9b3bf7ebc71922cb8010c2a2bfec7fc}{TESTBYTE}};}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00100}00100                         \mbox{\hyperlink{memcchr_8c_ae9b3bf7ebc71922cb8010c2a2bfec7fc}{TESTBYTE}};}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00101}00101 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00102}00102                         \textcolor{keywordflow}{goto} \mbox{\hyperlink{iconv__converter__if_8m_a20e720e6f86f7cc3db940beaea90f26b}{done}};}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00103}00103                 \}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00104}00104         \}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00105}00105 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00106}00106         \textcolor{keywordflow}{return} (NULL);}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00107}00107 }
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00108}00108 \mbox{\hyperlink{iconv__converter__if_8m_a20e720e6f86f7cc3db940beaea90f26b}{done}}:}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00109}00109         \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00110}00110 \textcolor{comment}{         * If the end of the buffer is not word aligned, the previous}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00111}00111 \textcolor{comment}{         * loops may obtain an address that's beyond the end of the}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00112}00112 \textcolor{comment}{         * buffer.}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00113}00113 \textcolor{comment}{         */}}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00114}00114         \textcolor{keywordflow}{if} (p < end)}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00115}00115                 \textcolor{keywordflow}{return} (\_\_DECONST(\textcolor{keywordtype}{void} *, p));}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00116}00116         \textcolor{keywordflow}{return} (NULL);}
\DoxyCodeLine{\Hypertarget{memcchr_8c_source_l00117}00117 \}}

\end{DoxyCode}
