This is just a minor overhaul of several utility functions, in part because it kept bothering me and in part because i was bored.
136 lines
4.4 KiB
C
136 lines
4.4 KiB
C
/* See the end of this file for copyright and license terms. */
|
|
|
|
#include <gay/types.h>
|
|
|
|
#include <string.h>
|
|
|
|
/*
|
|
* The two and four byte routines use the same trick as strlen(),
|
|
* see the respective file for details and where it's from.
|
|
*/
|
|
|
|
static inline void fourbyte_strcpy(char *to, const char *from)
|
|
{
|
|
/* make sure we are aligned first */
|
|
#pragma unroll(3)
|
|
while ((uintptr_t)to % 4) {
|
|
if ((*to++ = *from++) == '\0')
|
|
return;
|
|
}
|
|
|
|
/* copy in chunks of 4 bytes until there is a terminator */
|
|
u32 *to32 = (u32 *)to;
|
|
const u32 *from32 = (const u32 *)from;
|
|
while ( ((*from32 - 0x01010101) & (~*from32 & 0x80808080)) == 0 )
|
|
*to32++ = *from32++;
|
|
|
|
to = (char *)to32;
|
|
from = (const char *)from32;
|
|
|
|
/* copy the remaining bytes (can only be within 1 and 4) */
|
|
#pragma unroll(4)
|
|
while ((*to++ = *from++) != '\0');
|
|
/* nothing */
|
|
}
|
|
|
|
static inline void twobyte_strcpy(char *to, const char *from)
|
|
{
|
|
if ((uintptr_t)to % 2) {
|
|
if ((*to++ = *from++) == '\0')
|
|
return;
|
|
}
|
|
|
|
u16 *to16 = (u16 *)to;
|
|
const u16 *from16 = (const u16 *)from;
|
|
while ( ((*from16 - 0x0101) & (~*from16 & 0x8080)) == 0 )
|
|
*to16++ = *from16++;
|
|
|
|
to = (char *)to16;
|
|
from = (const char *)from16;
|
|
|
|
/* copy the remaining bytes (can only be within 1 and 2) */
|
|
#pragma unroll(2)
|
|
while ((*to++ = *from++) != '\0');
|
|
/* nothing */
|
|
}
|
|
|
|
#ifdef WEAK_STRCPY
|
|
#define STRCPY_NAME __strcpy
|
|
#else
|
|
#define STRCPY_NAME strcpy
|
|
#endif
|
|
|
|
char *STRCPY_NAME(char *restrict to, const char *restrict from)
|
|
{
|
|
char *save = to;
|
|
|
|
/*
|
|
* Check if the pointers can both be aligned to 2 or 4 bytes and copy
|
|
* in whole blocks of 2 or 4 bytes respectively if that's the case.
|
|
* This optimization is utterly useless because i'm almost certainly
|
|
* gonna write (or "import", rather) dedicated assembly routines but
|
|
* hey it can't hurt to introduce new sources for bugs right?
|
|
*/
|
|
if (((uintptr_t)to % 4) == ((uintptr_t)from % 4)) {
|
|
fourbyte_strcpy(to, from);
|
|
} else if (((uintptr_t)to % 2) == ((uintptr_t)from % 2)) {
|
|
twobyte_strcpy(to, from);
|
|
} else {
|
|
while ((*to++ = *from++) != '\0');
|
|
/* nothing */
|
|
}
|
|
|
|
return save;
|
|
}
|
|
|
|
#ifdef WEAK_STRCPY
|
|
#include <gay/cdefs.h>
|
|
__weak __alias(__strcpy) char *strcpy(char *restrict, const char *restrict);
|
|
#endif /* WEAK_STRCPY */
|
|
|
|
/*
|
|
* This file is part of GayBSD.
|
|
* Copyright (c) 2021 fef <owo@fef.moe>.
|
|
*
|
|
* GayBSD is nonviolent software: you may only use, redistribute, and/or
|
|
* modify it under the terms of the Cooperative Nonviolent Public License
|
|
* (CNPL) as found in the LICENSE file in the source code root directory
|
|
* or at <https://git.pixie.town/thufie/npl-builder>; either version 7
|
|
* of the license, or (at your option) any later version.
|
|
*
|
|
* GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent
|
|
* permitted by applicable law. See the CNPL for details.
|
|
*
|
|
* ==========================================================================
|
|
*
|
|
* This file is derived from FreeBSD source code.
|
|
*
|
|
* Copyright (c) 1988, 1993
|
|
* The Regents of the University of California. All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*
|
|
* $FreeBSD$
|
|
*/
|