From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id 22B5A3858D28 for ; Thu, 5 Jan 2023 22:49:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 22B5A3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-x635.google.com with SMTP id gh17so93479953ejb.6 for ; Thu, 05 Jan 2023 14:49:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=FgFyegxZcKEwqXxNrP1hYmtFaDhwdlfhkd8qAJE+U3M=; b=hrQX+KuKA0VGccm0ex2qShuMoPZHFaCdm8KK8+JJIUQOj39v1BKt08KHWmEMPYXem6 Qjnvsx7RECdBg+PgCD1WR+MVohN8vZPHTfKV5LIxiw+GntUEh/n5EEXlikWdRHEOnUSC PhE9uuwIOGzIuwpaepYz5zYSE6JvyI4ur/tL6U19aCWWJoDwgM7Tc2gdXg1z1pGs4vd6 2XOPBpOj5iN3NdHSOgPZDAZ7P6gaxPMNujZpEp9MArm/9ECV6Z84kB43DUKDAP49Uj+B 3PWIr2+UZXfJ1W4ajFh52TaHRPdU3xa3OsT9DiX790T2KsR20T5wADWCcsMc3l88c2la D+7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FgFyegxZcKEwqXxNrP1hYmtFaDhwdlfhkd8qAJE+U3M=; b=QVtqCLpnU98iHJrhbQtvd5c1JU3sPg0qHqLpsMFPWy+hVDu+q5PALBHLcR511KlLuZ BWwQPztRtfhNzc14FonmB/o0AhHqljey2mEaaXhma3Upfnpwi4xnb6oAWcFWce4vlTE0 oW0GgYQJCkw7AAct1R+mGxqY1aqC7XG6JkZG4deCP9lWZb8nJ4yg5A7TF4ivoxWA8JpI 6PkPlUNKQuAlKdo9MrfF/xaONeDXmzNbXwULfmZ66t1jchQk+90UhJU/DaiGbk9jD0Dv Q1QlQWbEamPyK0EpchX5YV8mhRfca4QiHaPfeg4kkwnwqxgS+usCX7X54q7bCRGcfGOL ga4Q== X-Gm-Message-State: AFqh2kp0UBSGWWsqkFxh8QClGihOm4d0PCRL5UONppA5TYggUfQc1+/l th5pS1RUHK4qMc5ESJ9SKyJY7pr1VttudVLAyohgdMPd X-Google-Smtp-Source: AMrXdXs4YR+9lW+rrTYAi0I42tNzcK8Z7VKVLI0pIem1YawXW9YnKg+93Wm0scZ3OsnwFXqB/YG8bbOwZbwxiyv+E4U= X-Received: by 2002:a17:906:281b:b0:7c1:98f:be57 with SMTP id r27-20020a170906281b00b007c1098fbe57mr2982852ejc.97.1672958966828; Thu, 05 Jan 2023 14:49:26 -0800 (PST) MIME-Version: 1.0 References: <20220919195920.956393-1-adhemerval.zanella@linaro.org> <20220919195920.956393-4-adhemerval.zanella@linaro.org> In-Reply-To: <20220919195920.956393-4-adhemerval.zanella@linaro.org> From: Noah Goldstein Date: Thu, 5 Jan 2023 14:49:14 -0800 Message-ID: Subject: Re: [PATCH v5 03/17] Add string-maskoff.h generic header To: Adhemerval Zanella Cc: libc-alpha@sourceware.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP 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: On Mon, Sep 19, 2022 at 12:59 PM Adhemerval Zanella via Libc-alpha wrote: > > Macros to operate on unaligned access for string operations: > > - create_mask: create a mask based on pointer alignment to sets up > non-zero bytes before the beginning of the word so a following > operation (such as find zero) might ignore these bytes. > > - highbit_mask: create a mask with high bit of each byte being 1, > and the low 7 bits being all the opposite of the input. > > These macros are meant to be used on optimized vectorized string > implementations. > --- > sysdeps/generic/string-maskoff.h | 73 ++++++++++++++++++++++++++++++++ > 1 file changed, 73 insertions(+) > create mode 100644 sysdeps/generic/string-maskoff.h > > diff --git a/sysdeps/generic/string-maskoff.h b/sysdeps/generic/string-maskoff.h > new file mode 100644 > index 0000000000..831647bda6 > --- /dev/null > +++ b/sysdeps/generic/string-maskoff.h > @@ -0,0 +1,73 @@ > +/* Mask off bits. Generic C version. > + Copyright (C) 2022 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +#ifndef _STRING_MASKOFF_H > +#define _STRING_MASKOFF_H 1 > + > +#include > +#include > +#include > +#include > + > +/* Provide a mask based on the pointer alignment that sets up non-zero > + bytes before the beginning of the word. It is used to mask off > + undesirable bits from an aligned read from an unaligned pointer. > + For instance, on a 64 bits machine with a pointer alignment of > + 3 the function returns 0x0000000000ffffff for LE and 0xffffff0000000000 > + (meaning to mask off the initial 3 bytes). */ > +static inline op_t > +create_mask (uintptr_t i) > +{ > + i = i % sizeof (op_t); > + if (__BYTE_ORDER == __LITTLE_ENDIAN) > + return ~(((op_t)-1) << (i * CHAR_BIT)); > + else > + return ~(((op_t)-1) >> (i * CHAR_BIT)); > +} > + > +/* Setup an word with each byte being c_in. For instance, on a 64 bits > + machine with input as 0xce the functions returns 0xcececececececece. */ > +static inline op_t > +repeat_bytes (unsigned char c_in) > +{ > + return ((op_t)-1 / 0xff) * c_in; > +} > + > +/* Based on mask created by 'create_mask', mask off the high bit of each > + byte in the mask. It is used to mask off undesirable bits from an > + aligned read from an unaligned pointer, and also taking care to avoid > + match possible bytes meant to be matched. For instance, on a 64 bits > + machine with a mask created from a pointer with an alignment of 3 > + (0x0000000000ffffff) the function returns 0x7f7f7f0000000000 for BE > + and 0x00000000007f7f7f for LE. */ > +static inline op_t > +highbit_mask (op_t m) > +{ > + return m & repeat_bytes (0x7f); > +} > + > +/* Return the address of the op_t word containing the address P. For > + instance on address 0x0011223344556677 and op_t with size of 8, > + it returns 0x0011223344556670. */ > +static inline op_t * > +word_containing (char const *p) > +{ > + return (op_t *) (p - (uintptr_t) p % sizeof (op_t)); This can just be (p & (-sizeof(p)) I think. Other than that look goods. > +} > + > +#endif /* _STRING_MASKOFF_H */ > -- > 2.34.1 >