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 X-Patchwork-Id: 641265 Delivered-To: patch@linaro.org Received: by 2002:a17:522:f3c4:b0:4b4:3859:abed with SMTP id in4csp3458139pvb; Wed, 11 Jan 2023 12:47:13 -0800 (PST) X-Google-Smtp-Source: AMrXdXvJOOuJjApkYkPZ/4KILFTzRgxDF+aUKv/tryz+eHBIdLIYJ+aeSwJdnSKocDWSdeNnH345 X-Received: by 2002:a17:907:c242:b0:7c4:fc02:46a3 with SMTP id tj2-20020a170907c24200b007c4fc0246a3mr74697449ejc.30.1673470033322; Wed, 11 Jan 2023 12:47:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673470033; cv=none; d=google.com; s=arc-20160816; b=BNfEC33qC3urmyCwgO0w7JThZZJNnD7HD/YoeRLMl+FVgNyH/Wtxpn6FBI+hrsPGfQ 4AibijX12JzRIkxugIKiCZFU00mnOO+37JegAKs8u3t3pSgfqp52J4BMHf72eT6CuJV/ 8PIbNFDsf+chi2UNj5SO6gvo0gAnawZ8WlRd02QvHlIWYjdUfAFFMmyNUbwMOji5W2Kt 54o+yZVQ7d6rY+kaka/OU2cUabWOT2UAoDHdSIVfZXheU1XG3iV5fLbDexD4VQ+QgUDw VjLyhlcb4rRGYd5JuI/0wDacgt1zqOpVsyK9qniH4w00R7+YmhB+1MMmOugdPJrnJC/J sXzQ== 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=PddHxuIFSkRHPD25pU6F1VW7PwC2Yf02K6BCpa6PwPc=; b=x0oopBxC8jBBm84Yzypq7n2ehjvOpDkN6P44hBRTokA+O6DStW8WhJI9XciAHX6gRd ccGeFYv1C0I+XIfXBcnC8XuPGdm++jONVjpQ8+R9Fzl49y7PYuqaH5R4mVUrgOvZPpIt 45N2ogIV8crcdf6pxC1YAFOcebNeiiB4T93s6DAJQ8YiqWMrmcz8zBOJMzVt1P6MnsAd XRaELPCP29I+vRD5kOeBjXlh/FBpSkCjliIlWr6+3lSz6sk5ZoSAa9zW4D9JeXsz7pWM xIqg7dG/2bN5p5aLcDoO/hiL/4OEvyZ6/rFIYyTe1H9ia4S4wpu3prrtWuix8I/pAtIb BflQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=UDXrrlkK; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id xb6-20020a170907070600b007af041e40b4si16174144ejb.129.2023.01.11.12.47.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Jan 2023 12:47:13 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=UDXrrlkK; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 F3CFF3881D07 for ; Wed, 11 Jan 2023 20:47:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F3CFF3881D07 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673470032; 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=UDXrrlkKQqmqCQgOudbP+Y2XwXvZVF7XE+Q7fqA3SeBDLHJV4zn3P8Fl5Hk9zWdXW U8q9k/gMJq4ir/a0olKs5+JBE/4kZECjhCK/67KaMQy4FgHVONfDJ3xBEjMPWnZs98 edSZ1HDmBF7qjKBnsG9bUhWZ8wYjpmZCdZFhFQco= 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-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 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. - 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 */