From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x229.google.com (mail-oi1-x229.google.com [IPv6:2607:f8b0:4864:20::229]) by sourceware.org (Postfix) with ESMTPS id 48CE83858C55 for ; Mon, 19 Sep 2022 19:59:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 48CE83858C55 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oi1-x229.google.com with SMTP id o184so828459oif.13 for ; Mon, 19 Sep 2022 12:59:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date; bh=MjUwfipAakeRuPnRv27tWnVieMJSKSQYpSl9hnNay6M=; b=uGyHnbVrtIsNS4AATRelfXjJZeZE9Cz1nFHGNNVxsIwJXme4/Cov/hSLMLYFDHc80S 4TtSrpebagQcmzyg602H8k1XgxPVw7baMTM6Nuuhs7mUezfAGogRz6CmO+JUqDWacjXo kCnxfks3jEybvPg6g136IOynw5/41Bhn27WUJyFwgVfBxZ1CeWmiJlm8MY9J9Lc/eXHH wB4Yxjvrf/Bpy31A4E4SQZK3wFhcODkV54PW0nj1G2ueACJou9JT6DvHb7cHlldhFrcz BIE2WvCOXBPl3egvx+n4954Qc36q60pZSFekdtRnflvvL5BCZSFVT4TyZ4b6yugjhdWk yiiQ== 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:to:from:x-gm-message-state:from:to:cc :subject:date; bh=MjUwfipAakeRuPnRv27tWnVieMJSKSQYpSl9hnNay6M=; b=sLkagknhb/YEIcvAEups4cTTtV3QT1Qm5uR8tqyWQL5WNH3rxcp0BX2zHhz5NxpMqX ZoQus1h1DLEKPDLwSdxXGd8ynDDIKijRAcFO/DWEWvq70c9297OQigA02nTv01UCCGPg zQOiE7lAX4Uie+MW/VQng18qa0xLBrrsO/sK4Tq7a3u8ZlKsLN4km3NKied++vcqQGeQ NeQbbHiCUuvjslczE91bOY3UmI9bS+yvW9UAXiPbzOHf9AdR27rWXtJPvfP4Kp0EEnTA Ebx8i3q5EtpB3eYrBdWfYCxr2/Y1MFqrO/DAQkfYOFQk+9vctqakCPU9vx9XlHBxTqgf Neqg== X-Gm-Message-State: ACrzQf3ZpWPri52F/Za0f/zaSyH7VbaCebhYj4uGLERVR5WfwfGfKApe bb0aliwrTdG/9wzSOkAcUSCnt2+xrlylttPN X-Google-Smtp-Source: AMsMyM5W6qqSJmDS3u5XtcFkcg3Wxo91ge7Rgm1cYc0JJr8R8eBJBNOrWaZ3SzWZmDc4NgZ1MmPsvQ== X-Received: by 2002:aca:c287:0:b0:343:402d:950a with SMTP id s129-20020acac287000000b00343402d950amr8209055oif.245.1663617570318; Mon, 19 Sep 2022 12:59:30 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:c266:b3ad:a56d:1ca:d6ea]) by smtp.gmail.com with ESMTPSA id l7-20020a4ab0c7000000b00475dc6c6f31sm4532291oon.45.2022.09.19.12.59.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Sep 2022 12:59:30 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v5 03/17] Add string-maskoff.h generic header Date: Mon, 19 Sep 2022 16:59:06 -0300 Message-Id: <20220919195920.956393-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220919195920.956393-1-adhemerval.zanella@linaro.org> References: <20220919195920.956393-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.5 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 List-Id: 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)); +} + +#endif /* _STRING_MASKOFF_H */ -- 2.34.1