From patchwork Tue Jan 10 21:00:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 640896 Delivered-To: patch@linaro.org Received: by 2002:a17:522:f3c4:b0:4b4:3859:abed with SMTP id in4csp2915580pvb; Tue, 10 Jan 2023 13:01:34 -0800 (PST) X-Google-Smtp-Source: AMrXdXtpqQp59WJDdh4WoSs4mNRXTo1XoiIjounV4+uSOErho93G6DDXvHnJnhwsGmFn+Aqh3CHq X-Received: by 2002:a05:6402:414:b0:495:77a9:f10d with SMTP id q20-20020a056402041400b0049577a9f10dmr15878865edv.42.1673384494150; Tue, 10 Jan 2023 13:01:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673384494; cv=none; d=google.com; s=arc-20160816; b=L+GiBdHx+7wbKJcJWjUkjfhDuHf7v/DbjQ979ECEb1kymqWo7nKaY3M456WGOs1wwo JrK2g4hhonLNARnDYRT7FzU/YinnbQBxwb8qA8i0bnzwXvhlr9DqI0NCmcA8ZM9HGBXt 5f2bra/m8g0x23yji8Ic+OOy4F1v+GhBNkaBDRjqCtfJJsUPm4P7x+v2XAwQLapQ7Qhf a7OhA8TXw+v7pn/jDZPAOHS12Hrnla1YgeB/6+kzrTimTLJoWl9+PLR8k4MTYAY8++sj RaYFDEnNeSx6UIc2Q28F6ovKICywTOQjj+fIclD3fDyXJuAEYr6i4F8gIaZ9BOVJdrfY rV5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=RB25T/ws3uWabx2xGRO3jr0wdlqMybwD8hb8NBqZcC8=; b=LpHp0JBEAgr+dyvKMjWkdcKRa6mFjhHuK8Tbz+s1wgrVcEUrlW9+sdPcJdbnBeVTom ManR5yXaOqAL9tDtEtUftlcnvKaKAhAUnjQY9YoHC8bwK72azsfeX4vISwiUN6R6GPl1 oNSMI0I5SZFkpHyabKjx6r8q7RQ6EpW1X9MssE11+sfdVcH5AJ8PM3hbYfjTmBxJ2n9/ oJXsQSW5182avvCo6O9aEFaZdMjMdJ+2hGG5ZsiBU2wdcq+JnW6MrxqM6AAejZOAn548 01aV2qOaTe9TKyXTZvk/EZriN+bc0TkX/G1CyY7ey8gQjYxlhvh4dwf2N92jVQlQSBE3 LPHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=LdPKstpn; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Return-Path: Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id b9-20020a056402278900b00484d2a31c84si14071897ede.473.2023.01.10.13.01.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jan 2023 13:01:34 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=LdPKstpn; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EFEDE3858033 for ; Tue, 10 Jan 2023 21:01:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EFEDE3858033 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673384493; bh=RB25T/ws3uWabx2xGRO3jr0wdlqMybwD8hb8NBqZcC8=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=LdPKstpnTkDFyXjKvfivRKAZ1NH23cky7j1G1ah6tL71cokx89q/5NfHZOuA1CQ+2 OhF8+cAsk1GSji1w9aljByn2EOJEPg5DGzHd4mfJ7NXm8W1Y+MvjXU8JYFJD4wmz/u MlTjONUTwpf0DRs9de/0cHD7Xo+acshrzputsYE0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc32.google.com (mail-oo1-xc32.google.com [IPv6:2607:f8b0:4864:20::c32]) by sourceware.org (Postfix) with ESMTPS id 5D0EE3858033 for ; Tue, 10 Jan 2023 21:01:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5D0EE3858033 Received: by mail-oo1-xc32.google.com with SMTP id y194-20020a4a45cb000000b004a08494e4b6so3547705ooa.7 for ; Tue, 10 Jan 2023 13:01:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RB25T/ws3uWabx2xGRO3jr0wdlqMybwD8hb8NBqZcC8=; b=MHPQ8z3xwS+3iSsNoqkAXP+nci7oEUEghUEnCm5nEvt8D/07dJc0LIK7ibA+Pj+p0L T510zCmejm1o7reG9vb+/AkBS8sgS2RFSORCsdbs9HTwR2UgtqgtTvxxPry53vurQfJj RKvttQ2qTLYxnAbEk+KuB57qWGli1bi3eMa5cw7/lRl52/e4NQ/Sbto9FGCnBWWBz83M D2VqxNKgCWCKWhgTMj16UMzoOQMDxojdfkqMMd/3oWLKcAw92OwiRIjA5iasA6KRxxBL 4KIX8ydUAuiDOwAx2dYO10YRtwDZ6855fTPmU1sINdgSVGRIx8ZzLgUtENuJdvGF+qfj Ek9A== X-Gm-Message-State: AFqh2koGoyVpH8jb1yOimcVgqLtNYa1tzZ18teNxiXSxcvanWPBQ92ks 4WbsAesWzhhTjIDZdJq/gAsp8p8/nkKmcP2IdoE= X-Received: by 2002:a4a:928f:0:b0:4f1:e1c7:2723 with SMTP id i15-20020a4a928f000000b004f1e1c72723mr7495661ooh.8.1673384480811; Tue, 10 Jan 2023 13:01:20 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a93a:e8a0:dd55:3328:997]) by smtp.gmail.com with ESMTPSA id r5-20020a4a83c5000000b0049ee88e86f9sm6202193oog.10.2023.01.10.13.01.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jan 2023 13:01:20 -0800 (PST) To: libc-alpha@sourceware.org, Noah Goldstein , Richard Henderson Cc: Adhemerval Zanella Netto Subject: [PATCH v6 03/17] Add string-maskoff.h generic header Date: Tue, 10 Jan 2023 18:00:52 -0300 Message-Id: <20230110210106.1457686-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230110210106.1457686-1-adhemerval.zanella@linaro.org> References: <20230110210106.1457686-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org Sender: "Libc-alpha" From: Adhemerval Zanella Netto 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. - check_mask: eturn the mask WORD shifted based on S_INT address value, to ignore values not presented in the aligned word read. - repeat_bytes: setup an word with each byte being c_in. - 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. - word_containing: return the address of the op_t word containing the addres. These macros are meant to be used on optimized vectorized string implementations. --- sysdeps/generic/string-maskoff.h | 85 ++++++++++++++++++++++++++++++++ 1 file changed, 85 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..6ad4e0b6f9 --- /dev/null +++ b/sysdeps/generic/string-maskoff.h @@ -0,0 +1,85 @@ +/* Mask off bits. Generic C version. + Copyright (C) 2023 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 +#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 __always_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)); +} + +/* Return the mask WORD shifted based on S_INT address value, to ignore + values not presented in the aligned word read. */ +static __always_inline op_t +check_mask (op_t word, uintptr_t s_int) +{ + if (__BYTE_ORDER == __LITTLE_ENDIAN) + return word >> (CHAR_BIT * (s_int % sizeof (s_int))); + else + return word << (CHAR_BIT * (s_int % sizeof (s_int))); +} + +/* 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 __always_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 __always_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 __always_inline op_t * +word_containing (char const *p) +{ + return (op_t *) ((uintptr_t) p & -sizeof(p)); +} + +#endif /* _STRING_MASKOFF_H */