From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 578D33857C42 for ; Tue, 5 Dec 2023 15:24:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 578D33857C42 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 578D33857C42 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701789873; cv=none; b=ZZAgE5+wTPWlxwSKN8pv1t1ocunPrTD7bIOybG+Y/EsZj4H4+x/bzFqI58VFVfUprmREbkQOKkECTheg1JUn+W2ANSInSHcQEQ9PWW4/JahVLwC5qQjzG5hV6rI6el30kF2GaHX6PwUs1FpOOJWQYRmebvA4QE/si0q6bWltNOg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701789873; c=relaxed/simple; bh=ygL01pXYWdJdZzXs96TfBK7XnAoNeDW9FdpaRPgU1cE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=HvfKiBHeYM1IQV+7QrxQxKST728Ra5rTrG1umwLV4T8dYKKrdZ8VsoEWDsKYg3q3RHO2G+x+IhbHqzp1J8gJLotMsXWrURKnG0NXDdlB9FznmUUFe8fSn+K3+3KIt8PLIrHgh0OWbkk+Iddded+YOR6ZYjSPUTODTZaoIKgfC68= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701789869; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=K97oqYBnc0HQYx69Jtvm1mxmhjB3EQCDBSewEPe4mMo=; b=W5KPH9rAGJ4GxccFLleFb91RinUCOptNJxLRGiDy99pjn2jj7ajQYT6485hMZF7nmy62Y/ hUwAH5fBbNGvZ/izv2JzT3LPhIG8z0HpEgq4bjbuvivAabakJ1AFy5QdJ42vymHy9drUMU T+p16dK3Ooo5YtJvjQG30ooFl8RWGoU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-58-IwkxteA8Nim99C3tqar14Q-1; Tue, 05 Dec 2023 10:23:30 -0500 X-MC-Unique: IwkxteA8Nim99C3tqar14Q-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A52A41854C9C; Tue, 5 Dec 2023 15:23:18 +0000 (UTC) Received: from calimero.vinschen.de (unknown [10.39.192.52]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 64CB6492BE6; Tue, 5 Dec 2023 15:23:18 +0000 (UTC) Received: by calimero.vinschen.de (Postfix, from userid 500) id 0B8C0A80A6B; Tue, 5 Dec 2023 16:23:17 +0100 (CET) Date: Tue, 5 Dec 2023 16:23:17 +0100 From: Corinna Vinschen To: James Tirta Halim Cc: newlib@sourceware.org Subject: Re: [PATCH] strchr, strchrnul: implement strchr() as strchrnul() Message-ID: Reply-To: newlib@sourceware.org Mail-Followup-To: James Tirta Halim , newlib@sourceware.org References: <20231205145839.474295-1-tirtajames45@gmail.com> MIME-Version: 1.0 In-Reply-To: <20231205145839.474295-1-tirtajames45@gmail.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Why? Please add a descriptive commit message with the reasoning and the desired result for this change. Thanks, Corinna On Dec 5 21:58, James Tirta Halim wrote: > --- > newlib/libc/string/strchr.c | 81 +--------------------------------- > newlib/libc/string/strchrnul.c | 77 +++++++++++++++++++++++++++++++- > 2 files changed, 77 insertions(+), 81 deletions(-) > > diff --git a/newlib/libc/string/strchr.c b/newlib/libc/string/strchr.c > index 96f30be04..382275b1d 100644 > --- a/newlib/libc/string/strchr.c > +++ b/newlib/libc/string/strchr.c > @@ -30,87 +30,10 @@ QUICKREF > #include > #include > > -/* Nonzero if X is not aligned on a "long" boundary. */ > -#define UNALIGNED(X) ((long)X & (sizeof (long) - 1)) > - > -/* How many bytes are loaded each iteration of the word copy loop. */ > -#define LBLOCKSIZE (sizeof (long)) > - > -#if LONG_MAX == 2147483647L > -#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) > -#else > -#if LONG_MAX == 9223372036854775807L > -/* Nonzero if X (a long int) contains a NULL byte. */ > -#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080) > -#else > -#error long int is not a 32bit or 64bit type. > -#endif > -#endif > - > -/* DETECTCHAR returns nonzero if (long)X contains the byte used > - to fill (long)MASK. */ > -#define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK)) > - > char * > strchr (const char *s1, > int i) > { > - const unsigned char *s = (const unsigned char *)s1; > - unsigned char c = i; > - > -#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) > - unsigned long mask,j; > - unsigned long *aligned_addr; > - > - /* Special case for finding 0. */ > - if (!c) > - { > - while (UNALIGNED (s)) > - { > - if (!*s) > - return (char *) s; > - s++; > - } > - /* Operate a word at a time. */ > - aligned_addr = (unsigned long *) s; > - while (!DETECTNULL (*aligned_addr)) > - aligned_addr++; > - /* Found the end of string. */ > - s = (const unsigned char *) aligned_addr; > - while (*s) > - s++; > - return (char *) s; > - } > - > - /* All other bytes. Align the pointer, then search a long at a time. */ > - while (UNALIGNED (s)) > - { > - if (!*s) > - return NULL; > - if (*s == c) > - return (char *) s; > - s++; > - } > - > - mask = c; > - for (j = 8; j < LBLOCKSIZE * 8; j <<= 1) > - mask = (mask << j) | mask; > - > - aligned_addr = (unsigned long *) s; > - while (!DETECTNULL (*aligned_addr) && !DETECTCHAR (*aligned_addr, mask)) > - aligned_addr++; > - > - /* The block of bytes currently pointed to by aligned_addr > - contains either a null or the target char, or both. We > - catch it using the bytewise search. */ > - > - s = (unsigned char *) aligned_addr; > - > -#endif /* not PREFER_SIZE_OVER_SPEED */ > - > - while (*s && *s != c) > - s++; > - if (*s == c) > - return (char *)s; > - return NULL; > + s1 = strchrnul(s1, i); > + return s1 && *s1 ? (char *) s1 : NULL; > } > diff --git a/newlib/libc/string/strchrnul.c b/newlib/libc/string/strchrnul.c > index f5c3eb25d..69f66db63 100644 > --- a/newlib/libc/string/strchrnul.c > +++ b/newlib/libc/string/strchrnul.c > @@ -29,12 +29,85 @@ QUICKREF > */ > > #include > +#include > + > +/* Nonzero if X is not aligned on a "long" boundary. */ > +#define UNALIGNED(X) ((long)X & (sizeof (long) - 1)) > + > +/* How many bytes are loaded each iteration of the word copy loop. */ > +#define LBLOCKSIZE (sizeof (long)) > + > +#if LONG_MAX == 2147483647L > +#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) > +#else > +#if LONG_MAX == 9223372036854775807L > +/* Nonzero if X (a long int) contains a NULL byte. */ > +#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080) > +#else > +#error long int is not a 32bit or 64bit type. > +#endif > +#endif > + > +/* DETECTCHAR returns nonzero if (long)X contains the byte used > + to fill (long)MASK. */ > +#define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK)) > > char * > strchrnul (const char *s1, > int i) > { > - char *s = strchr(s1, i); > + const unsigned char *s = (const unsigned char *)s1; > + unsigned char c = i; > + > +#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) > + unsigned long mask,j; > + unsigned long *aligned_addr; > + > + /* Special case for finding 0. */ > + if (!c) > + { > + while (UNALIGNED (s)) > + { > + if (!*s) > + return (char *) s; > + s++; > + } > + /* Operate a word at a time. */ > + aligned_addr = (unsigned long *) s; > + while (!DETECTNULL (*aligned_addr)) > + aligned_addr++; > + /* Found the end of string. */ > + s = (const unsigned char *) aligned_addr; > + while (*s) > + s++; > + return (char *) s; > + } > + > + /* All other bytes. Align the pointer, then search a long at a time. */ > + while (UNALIGNED (s)) > + { > + if (!*s || *s == c) > + return (char *) s; > + s++; > + } > + > + mask = c; > + for (j = 8; j < LBLOCKSIZE * 8; j <<= 1) > + mask = (mask << j) | mask; > + > + aligned_addr = (unsigned long *) s; > + while (!DETECTNULL (*aligned_addr) && !DETECTCHAR (*aligned_addr, mask)) > + aligned_addr++; > + > + /* The block of bytes currently pointed to by aligned_addr > + contains either a null or the target char, or both. We > + catch it using the bytewise search. */ > + > + s = (unsigned char *) aligned_addr; > + > +#endif /* not PREFER_SIZE_OVER_SPEED */ > > - return s ? s : (char *)s1 + strlen(s1); > + while (*s && *s != c) > + s++; > + return (char *) s; > } > -- > 2.43.0