From patchwork Wed Jan 11 20:45:44 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: 62967 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 50238382FAD6 for ; Wed, 11 Jan 2023 20:47:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 50238382FAD6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673470046; bh=PddHxuIFSkRHPD25pU6F1VW7PwC2Yf02K6BCpa6PwPc=; 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=Btk2Flj03IaauwzH1deVVty1WyK6swdPRP29UPr+nH9LE2F6/I7ajmzt2i4Dpxqy2 Pog1H0DDDMHNBPWdlsrEL57u7qHF0C3KKlUtVaAf4OCKRN9FN+lLYMV2Fd4U5STJIz ZuBviX36v2Dgs532xSzoI/vZ1H+Fvw0VKKtyHMdc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2d.google.com (mail-oa1-x2d.google.com [IPv6:2001:4860:4864:20::2d]) by sourceware.org (Postfix) with ESMTPS id 7B9553858C31 for ; Wed, 11 Jan 2023 20:46:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7B9553858C31 Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-12c8312131fso16851188fac.4 for ; Wed, 11 Jan 2023 12:46:11 -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=PddHxuIFSkRHPD25pU6F1VW7PwC2Yf02K6BCpa6PwPc=; b=uPdN2XSpc/IbLdFU2JXeOClhXUluZ4kiSLO0tQpRYdiDzUoMU0b9lEgNivZ6SgI9KN 1bZGhT97tepLBoPphgmhbcPCgvJdBsGN5dGWCwCDQyBkiZs8dYK1J9S6pUQEn4WhgBlu +mWJHDSDa7p5/q209wJyuLNM/P5vaU6IpINm1GzU2RvRftNS5VAu/R3s7S8jnWHSMuN1 d/PaqVq7gRh3/5iTXugW4k2AvOqg2O2nMV0XRDuZtrEsoEDTntFvIpbix24yfbfgBDCn PDRGm4BUkzwrkiS8H/lyt/XfChP7dMypiUPbz+HiZV4CoYIWe0NwUV12bbNc29UZtpMt bJkQ== X-Gm-Message-State: AFqh2kohk3csLkC4mZX5WnQkLsDYA5cdGN/3u8uZ+f7oIR8d/HD9aus9 JpxxoQcYxUjMreoKDgw30gbpA4MFZnhT0Ssubkk= X-Google-Smtp-Source: AMrXdXuS9uoOPwyocdk1bpSis2bMPi6lCKk0FF5xBvxQxi5dX7+j5iJV1r+IX2j6SRJN8amVsjYjgg== X-Received: by 2002:a05:6870:610f:b0:14f:b4be:2f70 with SMTP id s15-20020a056870610f00b0014fb4be2f70mr38273199oae.9.1673469970232; Wed, 11 Jan 2023 12:46:10 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a93a:a504:f3f6:dd7b:801]) by smtp.gmail.com with ESMTPSA id kw18-20020a056870ac1200b0014c8b5d54b2sm7990274oab.20.2023.01.11.12.46.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Jan 2023 12:46:09 -0800 (PST) To: libc-alpha@sourceware.org, Noah Goldstein , Richard Henderson Cc: Adhemerval Zanella Netto Subject: [PATCH v7 03/17] Add string-maskoff.h generic header Date: Wed, 11 Jan 2023 17:45:44 -0300 Message-Id: <20230111204558.2402155-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230111204558.2402155-1-adhemerval.zanella@linaro.org> References: <20230111204558.2402155-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.6 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+patchwork=sourceware.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. - 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 | 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..73edd5ad0f --- /dev/null +++ b/sysdeps/generic/string-maskoff.h @@ -0,0 +1,73 @@ +/* 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 + +/* 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)); +} + +/* 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 (op_t)); +} + +#endif /* _STRING_MASKOFF_H */