From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BF00BEB64DC for ; Tue, 11 Jul 2023 14:43:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=jB27PO5r/NlX4+IyaJ2cppVKY3Gngd1MTrjHmLxBOvI=; b=hYXmYMU0ImtbmS7dABz5e2RG3k gRLkoTELXhR55pFEhQDkBbnLv9OJXwB0xQd23DOebBncd4UtMXh7hSoi00JxGQBHJaazOGFCSgGDV s3TMy//PjAt655c7rhhVvvx7FEiPrZ7RAv6ctY1wBxxFvpCaQEgOD1viRbhC/JLh6pvJD0Joqs6+A ZzBubaWFDnD7Kp4YutAJUdnzr+BLkQvUZxx2H6qFMJ8G1ohby9LOZwNimMpeSIAHjJFFeLfql9p5T +Ry5HjyPtcKcfYy88i7fy2NDuWOUvVaImBLf3jtGeEizhdcbe4QRVNgM4eZvVDafga5Fye7kNirAT pIYnvXOw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qJEZh-00FACt-2c; Tue, 11 Jul 2023 14:42:45 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qJEZe-00FABA-0j for linux-arm-kernel@lists.infradead.org; Tue, 11 Jul 2023 14:42:43 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-57704af0e64so63304407b3.0 for ; Tue, 11 Jul 2023 07:42:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689086560; x=1691678560; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dufumGN0X5SXjxYpittS7pKflaiJdPliu9At6Sap++c=; b=dLRkccixklYYXR2WU6Ln/pjVdN3Qd9Ec+IRQSqKyylEED1pH7A8wD4Qv2myKG/RfLD Jmz7qFSWO5vQZdgvSSTVF0jfpbFG+iIWCuI73GkGjsLdmmFtHeaEXarEarzKRxLo3Bbu Oa7iNom8QaPEKsXkrFwKbZwche58tepyDYMn2uuFlCkTDLEMqryEOghFXXF/Xr3QtaQA O9v+M21PEGqtdr36HSBveJlshivuqQkleC6reypNk8f7QOeE59/oCbcDig3awR0KcPbJ GOVy7GA+6yaCuScu0UfAVfJfq/ycqEaCCA0f2JkINq5xfGn+SvqCQktRwaRosZt61X7s 6FDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689086560; x=1691678560; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dufumGN0X5SXjxYpittS7pKflaiJdPliu9At6Sap++c=; b=NM5EZ1PSQe5Fre04L3oDOKTpMKfQjLTmyXTMZ7oIrEFD3DdTu5JS4IKWFh+P9paEf+ ZaGURsCMqqcM1ChHrHPy6F0fOqOlLSr+EDfnaswAdpmz6nLbcRrNGa+TJQegsDo3Su+X Tk2M970EbWtizJwp1PIp4VQkQFgnCwaLb/zwTUfA6z48TaJV6Ym2x52oHvrMRSfjzpau dJcLZ0Weq7cOEjfMJzEaKMew0LNnacpfAsgmC6RcyEZfMrV1IpeKnm57FuMex2dc5lF4 RTGwmQzZFmOn817IC5HDGLsilDd2dgRKuBaKG19diqhRdunnn6SEL33cFcBKQ6oCeb+3 qmlg== X-Gm-Message-State: ABy/qLYWD33y9ezNbvG2ThKGqoUbCqvS6o5mvHavrMhFV7l8ZitUY+NR mXv5RBRtFwtiLymkSDi9Glk2suQsJ0g= X-Google-Smtp-Source: APBJJlFIZz3CqK9sHFiNkuXlFYUZxhErF6A+cY/vC/nVCEfDRQwrtiXHgvzlHG6TlEhbEb3LNYtF3wh57Bs= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:564d:3aaa:6b5f:4419]) (user=glider job=sendgmr) by 2002:a05:690c:705:b0:565:a42c:79fe with SMTP id bs5-20020a05690c070500b00565a42c79femr126547ywb.1.1689086560587; Tue, 11 Jul 2023 07:42:40 -0700 (PDT) Date: Tue, 11 Jul 2023 16:42:29 +0200 In-Reply-To: <20230711144233.3129207-1-glider@google.com> Mime-Version: 1.0 References: <20230711144233.3129207-1-glider@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230711144233.3129207-2-glider@google.com> Subject: [Resend v1 1/5] linux/bitqueue.h: add the bit queue implementation From: Alexander Potapenko To: glider@google.com, catalin.marinas@arm.com, will@kernel.org, pcc@google.com, andreyknvl@gmail.com Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, eugenis@google.com, yury.norov@gmail.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230711_074242_273074_9B905471 X-CRM114-Status: GOOD ( 21.60 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org struct bitq represents a bit queue with external storage. Its purpose is to easily pack sub-byte values, which can be used, for example, to implement RLE algorithms. Signed-off-by: Alexander Potapenko --- include/linux/bitqueue.h | 144 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 include/linux/bitqueue.h diff --git a/include/linux/bitqueue.h b/include/linux/bitqueue.h new file mode 100644 index 0000000000000..c4393f703c697 --- /dev/null +++ b/include/linux/bitqueue.h @@ -0,0 +1,144 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * A simple bit queue which supports enqueueing/dequeueing of sub-byte values. + * + * This can be used to pack complex bitfields into byte arrays. + */ +#ifndef _LINUX_BITQUEUE_H +#define _LINUX_BITQUEUE_H + +#include +#include + +/** + * struct bitq - represents a bit queue with external storage. + * @data: data buffer used by the queue. + * @size: size of @data in bytes. + * @bit_pos: current bit position. + */ +struct bitq { + u8 *data; + int size, bit_pos; +}; + +/** + * bitq_init - initialize an empty bit queue. + * @q: struct bitq to be initialized. + * @data: external data buffer to use. + * @size: capacity in bytes. + * + * Return: 0 in the case of success, -1 if either of the pointers is NULL. + */ +static inline int bitq_init(struct bitq *q, u8 *data, int size) +{ + if (!q || !data) + return -1; + q->data = data; + q->size = size; + memset(data, 0, size); + q->bit_pos = 0; + return 0; +} + +/** + * bitq_init_full - make a bit queue from an initialized byte array. + * @q: struct bitq to be initialized. + * @data: external data buffer to use. + * @size: capacity in bytes. + * + * Return: 0 in the case of success, -1 if either of the pointers is NULL. + */ +static inline int bitq_init_full(struct bitq *q, u8 *data, int size) +{ + if (!q || !data) + return -1; + q->data = data; + q->size = size; + q->bit_pos = q->size * 8; + return 0; +} + +/** + * bitq_enqueue - push up to 8 bits to the end of the queue. + * @q: struct bitq. + * @value: byte containing the value to be pushed. + * @bits: number of bits (1 to 8) to push. + * + * Return: number of bits pushed, or -1 in the case of an error. + */ +static inline int bitq_enqueue(struct bitq *q, u8 value, int bits) +{ + int byte_pos, left_in_byte, max_pos; + u8 hi, lo; + + if (!q || (bits < 1) || (bits > 8)) + return -1; + + max_pos = q->size * 8; + if ((max_pos - q->bit_pos) < bits) + return -1; + + left_in_byte = 8 - (q->bit_pos % 8); + byte_pos = q->bit_pos / 8; + /* Clamp @value. */ + value %= (1 << bits); + if (left_in_byte >= bits) { + /* @value fits into the current byte. */ + value <<= (left_in_byte - bits); + q->data[byte_pos] |= value; + } else { + /* + * @value needs to be split between the current and the + * following bytes. + */ + hi = value >> (bits - left_in_byte); + q->data[byte_pos] |= hi; + byte_pos++; + lo = value << (8 - (bits - left_in_byte)); + q->data[byte_pos] |= lo; + } + q->bit_pos += bits; + return bits; +} + +/** + * bitq_dequeue - pop up to 8 bits from the beginning of the queue. + * @q: struct bitq. + * @value: u8* to store the popped value (can be NULL). + * @bits: number of bits (1 to 8) to pop. + * + * Return: number of bits popped, or -1 in the case of an error. + */ + +#include +static inline int bitq_dequeue(struct bitq *q, u8 *value, int bits) +{ + int rem_bits = 8 - bits, i; + u8 output; + + /* Invalid arguments. */ + if (!q || (bits < 1) || (bits > 8)) + return -1; + /* Not enough space to insert @bits. */ + if (q->bit_pos < bits) + return -1; + /* Take the first @bits bits from the first byte. */ + output = q->data[0]; + output >>= rem_bits; + if (value) + *value = output; + + /* + * Shift every byte in the queue to the left by @bits, carrying over to + * the previous byte. + */ + for (i = 0; i < q->size - 1; i++) { + q->data[i] = (q->data[i] << bits) | + (q->data[i + 1] >> rem_bits); + } + q->data[q->size - 1] <<= bits; + q->bit_pos -= bits; + return bits; +} + +#endif // _LINUX_BITQUEUE_H -- 2.41.0.255.g8b1d071c50-goog _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel