From patchwork Tue Apr 16 14:24:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13631963 Received: from mail-ot1-f43.google.com (mail-ot1-f43.google.com [209.85.210.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0CEC5132470 for ; Tue, 16 Apr 2024 14:25:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713277503; cv=none; b=DnkM8ImwzLnUTY7d1R1xm1av6ZqLj0w/CSMfberrDQJXehk1d8vRIGdWTlVpYsPR8oNROxJQIlGY9EgV+z9nZKnyeAX69CgQ8Id2cbo/191WPdVCpKpDUK9mQRnQ6+N2IJRw0ggvcg6Tn6Rdnb9G5FTPx803bkNFURn+imPeL+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713277503; c=relaxed/simple; bh=Qg0sWfUZ1Y5AVACzSwVOzRzIU+95KW5HJ95s5ihX/OA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=spk4qoYJ8m6+lxaTm+YCJwuwHMNo18HFsPDlz6Wmm95VCKjdPMx+V1kPkdmCxckqQIHK0PV6SKzIQgzvPASdUFiSVTgJOf9FQC4pDqKbWQUAnLx3fUdKOexGfYoxsQ7k5oyDuvYJQLD8xI8ApdafCcSfcqTy/MVRMR4b/pxAUpY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cz889umz; arc=none smtp.client-ip=209.85.210.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cz889umz" Received: by mail-ot1-f43.google.com with SMTP id 46e09a7af769-6eb7500abe3so1495376a34.2 for ; Tue, 16 Apr 2024 07:25:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713277501; x=1713882301; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=0qCR6qBAYBIAganQSNYamaCuGyGHoQ+XWEWbUy/9fgI=; b=cz889umz5htv9bSLoCb/72PbiXP1K1C0ymAhxX/AUhqtja/AHTQIkUKzTkfcA3PSZ8 XsHD3b/XC4G8MIE9i3Yu4yrcgzaRbdTgekG1fmtQI0UtTQSik1M3xB64SQn4l1i54kxL uB1IOmYsWOPZ12EPCy1e9xos8mzlY4ef9AGxDDN1N7L1y/sh3DqDMf8r9Pi+MJCjNPi9 rVPWMMyTNmHmq3Twumqh2kKCawPx8tibJkGLQ6zE0a7PlsjpqQfsdXo2rq79A+Qj4T1p s1kj6G2dN9Tg29CnTxaq7jhYG1dvmmQbAxqs+JIDQI3hzrro4qrElYvvax4BokbuWXp/ bcYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713277501; x=1713882301; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0qCR6qBAYBIAganQSNYamaCuGyGHoQ+XWEWbUy/9fgI=; b=Vr5QgbkgvI5YCnXFSW/D978wtz2z0VOC2H+hoE9rw3xLaxU8trAD+lTJxT5WwHkWsa u70NbFXmmooXRbXaZ7W5cXLJf1cCIHsN+7RxELzCVQhHC9cFivfZ7KgqrN0YO7vY+dRe 11wepcuuKvItgNIYeokS6mJo/32ixJphY/JHOUBar/NdIewYYMSZwpQskSkIwrqIZoJk 6xMcdHWv/vhQuLr5lozXDQY+aqLIz2Sr9lyIUOnJftIhPlZRHZ3Zxj3SSz+Q0IQQ08yq yUSrmScu2rS2/+HtLCZwt/aogtGqKElUCLa7yHskqhiUc2BShxHr7o6b1wkRR5HOEXOx vAlQ== X-Gm-Message-State: AOJu0YyT+ez3MissEbCPzgboAx4ZgIRq05IZnZLNF6BafxM/pmc/1kgE RyibfuldQCQTC3s8QRTelxjFlCOw8y8HMH0aNQyMyZQYBfFoDPzEgPuiIQ== X-Google-Smtp-Source: AGHT+IGiVH5ZwRsa9tZD+KtfyRGkoIoFiYR65vYfIuEYnDNz4zvQwONcpATxf12P+1ycv+BksdrVvQ== X-Received: by 2002:a05:6830:2059:b0:6eb:7c4e:70c1 with SMTP id f25-20020a056830205900b006eb7c4e70c1mr6060355otp.37.1713277500948; Tue, 16 Apr 2024 07:25:00 -0700 (PDT) Received: from localhost.localdomain ([70.114.247.242]) by smtp.gmail.com with ESMTPSA id r33-20020a05683044a100b006eb892647dasm526999otv.79.2024.04.16.07.25.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 07:25:00 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH v2 1/4] util: Remove confusing static keyword use Date: Tue, 16 Apr 2024 09:24:52 -0500 Message-ID: <20240416142458.3353383-1-denkenz@gmail.com> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 '__n' is declared as static const which is confusing since it would imply that __n is only initialized once. This works out in the end since the macro used in a separate anonymous block, but some compilers (i.e. clang) will generate a symbol table entry for '__n'. This is not needed since the expression is always constant. Declare '__n' as a const size_t instead since that is the type produced by the sizeof() operation used by L_ARRAY_SIZE. While here, also declare '__i' as size_t to match '__n'. Fixes: 4a9f6388dd7f ("util: Add L_IN_SET macros") --- ell/util.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ell/util.h b/ell/util.h index 80b29b7bb6e3..237ea4824b06 100644 --- a/ell/util.h +++ b/ell/util.h @@ -349,8 +349,8 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p) #define _L_IN_SET_CMP(val, type, cmp, ...) __extension__ ({ \ const type __v = (val); \ const typeof(__v) __elems[] = {__VA_ARGS__}; \ - unsigned int __i; \ - static const unsigned int __n = L_ARRAY_SIZE(__elems); \ + size_t __i; \ + const size_t __n = L_ARRAY_SIZE(__elems); \ bool __r = false; \ for (__i = 0; __i < __n && !__r; __i++) \ __r = (cmp); \ From patchwork Tue Apr 16 14:24:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13631964 Received: from mail-ot1-f53.google.com (mail-ot1-f53.google.com [209.85.210.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5B6512D1FA for ; Tue, 16 Apr 2024 14:25:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713277504; cv=none; b=pOXwqI8jvHY/xRdKRy5R3/rU5lva9cAPEULHBQP37NNrRyWuKNPBE5MvDT5nh5nTpOQihwFliFzyS2+k5B+XlosGXeMryO3H3UXRYE8QerTqc09BAoUCBRpyEeRnKQzdZYSOCHtZKi97gpT7P3P9cEtgEQFdjYVMLkgSyUwy9xM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713277504; c=relaxed/simple; bh=j10vhC0Mr9hA/ntVVrTff+CXbRTx/mxSlPtqo/yafyQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gJs3fCvxU1+szeTEB70A/BkZ4OzJaKhwAVYIJc+ZQFU3/IRA7CJ0xwOSG7vxG60Xu61C4mUqYij+aI6z86ywRf22ivrxn4bLeFR/Hst968ACBjnY+Dt0QrvTM3JHTtWvayxPPURZCRr3OTkkWcS8HnPE6EyLSBu8ojp1VjKTURc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HPMODYs+; arc=none smtp.client-ip=209.85.210.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HPMODYs+" Received: by mail-ot1-f53.google.com with SMTP id 46e09a7af769-6eb8ae9b14eso870527a34.0 for ; Tue, 16 Apr 2024 07:25:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713277501; x=1713882301; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+rWQ066RNWOEY5oDq5u+p7aNXhCkyXRm5K2HwNWwDo8=; b=HPMODYs+oQA1+JazPqUCMmgmlMvdSDSLnJYFAjk7Fq92qFfEbOzsYhxdBBeQToMJjs 8f5RvAWlu+4qpKaOYGJYe1MfoVxDhgejLonvDeAD2K0fOTi8l9GZsabi1EpozVBywbbw vyOW0zv+AvAfc8VLZ2m8bC/tUYsbqDB5MWFPBgw3oXEBr869lW0QhZ9vOIf756ntg8w4 xBeayWomY7tlYoFFA4WnP0H+Xnc1XkqphyFKByKMGUbgOVIGc8JDfzczEst6FIH5V02X y4uzJ6KRatOqgxn6ehyPbFoxh0Mdf3VDD/rG2fEbR66PL4eChFV1oMtJQIOOp01NcZU8 Uywg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713277501; x=1713882301; 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=+rWQ066RNWOEY5oDq5u+p7aNXhCkyXRm5K2HwNWwDo8=; b=FpWzPVlVCkjLD50364dN4NaFxKccBRV4NRi9lEA1FgmyFBSKMZn3udgwzKPXQtscHd hifMbOIQM3TJ2B6GMXVz1ZD6cXgXifDUawQUbaZnX/5gy5qnsLLWoSeJH2LFvZ0U0hia znpI53f6FshXJc+LkETbJo8rG9fsAUM3PG+R3+73s4znca6QlHsyWXkwajZaldI35S0L zgO3w3XnU+IvSl+i8SH0x2fmafwx1tmlnpeThitSHqOVTn98+VRnop8SgFW09IroBTZC 4aitf/taD2NcDcOI/hxO8hV7lo45qonuOjCFJV8c4L105HsBCRk18TtGBrn0BxxkeloP +cUg== X-Gm-Message-State: AOJu0YzwIG766UbCmjNM2v34a7/4lHOrCg0w3+Zq/k+kqWQzb0vkL4GM 4o9toQ6K+waIEUgiJbYlPCkZ904gin+ljRC8S4eX5N+ya1yIa0Oo8HERTQ== X-Google-Smtp-Source: AGHT+IEnhjSmSbx4Xu+vbnetCyn/VfyhKK+EcWlXxRZcslVXqtbNYg9LAFBCPNpPckSzk6KAuLro5g== X-Received: by 2002:a05:6830:ecb:b0:6eb:5a72:3e57 with SMTP id dq11-20020a0568300ecb00b006eb5a723e57mr11399430otb.10.1713277501608; Tue, 16 Apr 2024 07:25:01 -0700 (PDT) Received: from localhost.localdomain ([70.114.247.242]) by smtp.gmail.com with ESMTPSA id r33-20020a05683044a100b006eb892647dasm526999otv.79.2024.04.16.07.25.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 07:25:01 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH v2 2/4] util: Add L_BIT_{SET|CLEAR|TEST} macros Date: Tue, 16 Apr 2024 09:24:53 -0500 Message-ID: <20240416142458.3353383-2-denkenz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240416142458.3353383-1-denkenz@gmail.com> References: <20240416142458.3353383-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 These macros support arbitrarily sized bitmaps using any integer data type. It is thus possible to write code like this: uint64_t bitmap = 0; uint8_t array[4] = {}; bool r; L_BIT_SET(&bitmap, 63); r = L_BIT_TEST(&bitmap, 0); L_BIT_SET(array, 25); r = L_BIT_TEST(array, 31); --- ell/util.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/ell/util.h b/ell/util.h index 237ea4824b06..ed0bbe845656 100644 --- a/ell/util.h +++ b/ell/util.h @@ -366,6 +366,36 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p) (__v && __elems[__i] && \ !strcmp(__v, __elems[__i])), ##__VA_ARGS__) +#define _L_BIT_TO_MASK(bits, nr) __extension__ ({ \ + typeof(*(bits)) _one = 1U; \ + const unsigned int _shift = (nr) % (sizeof(_one) * 8); \ + _one << _shift; \ +}) + +#define _L_BIT_TO_OFFSET(bits, nr) __extension__ ({ \ + __auto_type _bits = (bits); \ + const size_t _offset = (nr) / (sizeof(*_bits) * 8); \ + _bits + _offset; \ +}) + +#define L_BIT_SET(bits, nr) __extension__ ({ \ + size_t _nr = (nr); \ + __auto_type _offset = _L_BIT_TO_OFFSET(bits, _nr); \ + *_offset |= _L_BIT_TO_MASK(_offset, _nr); \ +}) + +#define L_BIT_CLEAR(bits, nr) __extension__ ({ \ + size_t _nr = (nr); \ + __auto_type _offset = _L_BIT_TO_OFFSET(bits, _nr); \ + *_offset &= ~_L_BIT_TO_MASK(_offset, _nr); \ +}) + +#define L_BIT_TEST(bits, nr) __extension__ ({ \ + size_t _nr = (nr); \ + __auto_type _offset = _L_BIT_TO_OFFSET(bits, _nr); \ + (*_offset & _L_BIT_TO_MASK(_offset, _nr)) != 0; \ +}) + /* * Taken from https://github.com/chmike/cst_time_memcmp, adding a volatile to * ensure the compiler does not try to optimize the constant time behavior. From patchwork Tue Apr 16 14:24:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13631965 Received: from mail-ot1-f46.google.com (mail-ot1-f46.google.com [209.85.210.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F21512D1FD for ; Tue, 16 Apr 2024 14:25:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713277504; cv=none; b=YnkV5RXtfVMHLbtReGNwbnEGYrTstoyNGj4aZL1Jj78yaZWVa08aQOc+Rgkw1hoJW6PJltMHOCRwWgZ723tzOlSQMkFh2mlcNbXdCBYtnYo9IVTDqSGYT4xDTvJ3QKeUJPK+OSlCVGdJHV+oO6eNM94A/aDVrDLkb6erxyHCQPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713277504; c=relaxed/simple; bh=nwdqHoOXWcez6n0clJODZMaJMJz18GyICBeKue7pfn8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kOdA0CLZFCTQOJ2cZLZfAH5PkJowLlmhxgsX9D3vYalhqa+7pnsfLY2Dg/Uw0+eOA4hRUSCy68tTnD+yP8Dc6EeVmF9O+1qKy/wB7umxx3ijd+okj3ckWvRUXjxwEFaReDdHysYbQ5yTJfd+OHwsjGcz0X6XbLpJE+KtHuXdsVQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QuEL8l4Q; arc=none smtp.client-ip=209.85.210.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QuEL8l4Q" Received: by mail-ot1-f46.google.com with SMTP id 46e09a7af769-6eb86b69e65so1029615a34.3 for ; Tue, 16 Apr 2024 07:25:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713277502; x=1713882302; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Uy2zvEZvdwnelhW4o8wq3mRw4YITkcPAT3PVo/a/+UA=; b=QuEL8l4Q2PHIFlhkppIJlpvVbQJnIXGkl757OF8cR7xvragxjC+wFOVhAgpARjlKan qUxzNTvyDTI5FdEM8reGAcsMng0ePy7X38qRQqKl/rYVK17tYkWNbvFQhDdOZ83XSJk9 Gm/yCsQdEm+uEx232qLWGysEHhTFkEsHYcenVcdTMH8n6PlrSVXTA50pXGQWXbZ9ixii bOOAX025fgbaWA3/OC3DInc1qo8JmNW10UThxbGtEWgKWTeMtHIQbDW31LBKmPYcb3h3 wHxM3YlXON7g/STbzjGGdjIUFQNTWp6YFs1KAjXOfUuVgKo5P3OAKhMGj4LlxBiId0Bd SG7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713277502; x=1713882302; 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=Uy2zvEZvdwnelhW4o8wq3mRw4YITkcPAT3PVo/a/+UA=; b=B0awLr+gHUY5O63bNe+fFhYXJFnIpQpKyvlhEHhCg5rOrjyIPVIZa+cty7Rio7HD2B YiDZcCpOIM9FFatFX4jRuRZbk2PElEVgE0PHb0gas3t5NSxmoLfXlMB9cM+1Ihe2LfMk JwSPLjJLnmZ6RDIpBKniPgv/OQuLHFfRwMEx5y+Pc6JscscVHTZQFzm929GtI9wSH09v fPro6KeXTqou7NxC36nz/JalO2Im+WYH3zXS8qJDM5F35QiMuy3cWJMZAeaHSIjhTdYz iWZuZwigaDrifQ3gZ4Gz7zXm76hvjNJQ6lOXaZnt/wwTnBmn9ce3xmcVfgt5yO/EhjlN KtQg== X-Gm-Message-State: AOJu0YzG4Xt98brvCqxprbUh5NyoEjWqowGIWYGQWb8c8Z5499v4uVIb qZihMPdV4d7BEhUaLNv2xWaVnR+603YABv/bd1DessNZoJawblmb1jXxBw== X-Google-Smtp-Source: AGHT+IF1X2ZqInNf9q2OJ5n+29O1nFmknk6SI3vA/m7Xw/MEAl+f4QV2eILiVZIgiSeHoBpjPNUM3A== X-Received: by 2002:a05:6830:650d:b0:6eb:8612:64d3 with SMTP id cm13-20020a056830650d00b006eb861264d3mr6231939otb.23.1713277502359; Tue, 16 Apr 2024 07:25:02 -0700 (PDT) Received: from localhost.localdomain ([70.114.247.242]) by smtp.gmail.com with ESMTPSA id r33-20020a05683044a100b006eb892647dasm526999otv.79.2024.04.16.07.25.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 07:25:02 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH v2 3/4] util: Add L_BITS_{SET|CLEAR} Date: Tue, 16 Apr 2024 09:24:54 -0500 Message-ID: <20240416142458.3353383-3-denkenz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240416142458.3353383-1-denkenz@gmail.com> References: <20240416142458.3353383-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 These macros can set or clear an arbitrary number of bits in a single call, as follows: uint64_t bitmap = 0; L_BITS_CLEAR(&bitmap, 0, 1, 5, 15, 32, 63); --- ell/util.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ell/util.h b/ell/util.h index ed0bbe845656..346197771514 100644 --- a/ell/util.h +++ b/ell/util.h @@ -396,6 +396,20 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p) (*_offset & _L_BIT_TO_MASK(_offset, _nr)) != 0; \ }) +#define L_BITS_SET(bits, ...) __extension__ ({ \ + const unsigned int __elems[] = {__VA_ARGS__}; \ + size_t __i; \ + for (__i = 0; __i < L_ARRAY_SIZE(__elems); __i++) \ + L_BIT_SET(bits, __elems[__i]); \ +}) + +#define L_BITS_CLEAR(bits, ...) __extension__ ({ \ + const unsigned int __elems[] = {__VA_ARGS__}; \ + size_t __i; \ + for (__i = 0; __i < L_ARRAY_SIZE(__elems); __i++) \ + L_BIT_CLEAR(bits, __elems[__i]); \ +}) + /* * Taken from https://github.com/chmike/cst_time_memcmp, adding a volatile to * ensure the compiler does not try to optimize the constant time behavior. From patchwork Tue Apr 16 14:24:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13631966 Received: from mail-oi1-f169.google.com (mail-oi1-f169.google.com [209.85.167.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D84F132489 for ; Tue, 16 Apr 2024 14:25:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713277505; cv=none; b=TCviZ0Zhu8aJ9ijXbH/0WZKCnJZvO5XvOs1r/3R2zdYk0NPwGUg3bJk5GImUcOud+OUU8fmltAEMNAqEK44YSAPizHuNKe6KkqsRLvvlxyeTa5squ8EJcZnd1p2nO2r/AxXFG4+BIyIUlcVm4RJuIUOkVcA6m3GD5HpT5jcuQQA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713277505; c=relaxed/simple; bh=Ryn63rGfHAomvx3udqLVIkIHeXBulDtGuOGCDufvJGE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GNVlw0CHPrAN23NJCaxc8Jxlvf+SGHBSfu4uIcMTO47P51yiCZtfr4+Dr8T1H5wtFYE6DWXVtaG80VUCe9d/8zhI+DOwUyVnZF/x9R3miQTO2p4T6MsRUz9irgXLOFzZb/6X5Ebbhei0K9RJIzTVCmwmT1thd3BIVVCKq7wcENA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fiLA13Dt; arc=none smtp.client-ip=209.85.167.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fiLA13Dt" Received: by mail-oi1-f169.google.com with SMTP id 5614622812f47-3c5ed7fdbb1so2958966b6e.0 for ; Tue, 16 Apr 2024 07:25:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713277503; x=1713882303; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lFmR3yMaN22EiDkIkEHWYWMk2s1k0kUvlvYOigLAYJo=; b=fiLA13Dtt5alfcmvVBA654DG1lRU75NZPaEX74nf4m/rGCxiXdqSpCxUrsmSD9TYf/ uxW1MRKWCHp8bBH/g/6XogZZ0Hy1AYT1f+bSbzLGFV23n87YVWApmx4IpphkKUESGF9E OTdKLaR8/0Mfv/MTvPehS+S7jfjjjKfHfIe4MjKtiIOchNatefeOppDs1yIO9tjsKTXG pYpRN5YdYIQ76P2IUg+LYng8N+SjCjDrZ85a6EccuCcUGMP8HHQHBUrmR6sGZLmvOPIx cowBahfr6rlwDAfYBiSJgc2ad4aPa6Ts1ZM63LAlbPoWRlAIyBAP16MsDQ5MYV+aVVr9 cEdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713277503; x=1713882303; 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=lFmR3yMaN22EiDkIkEHWYWMk2s1k0kUvlvYOigLAYJo=; b=hMEj7Ia22hcgsEqY3Oid3Qu59Q59z2jUtHhCjbE6hnUDS1CCvpKYCq+1myokAi2vnv 9af62mzko1rCnk3oMz2HRvG9wjXO7SLuUzCW9OzpVH3hZV165Rhbhf7dARzoT4LGhDlC QqmPWosqcECYlVl6kR9IBRdH8iWYSXjhXN8kVrxbcWzCahDeVy3+K77lpjO6OZSuCMYO kjd9S44oYekjI6bSn3oxU0zi95w7+/uk1K4nbjQCqEhQmhihOSXErfileHnVGrjRTQfG 3em6aiyuGvx//OoJdFGGu5aE6h7CrGnaK2UmiHY5Iel5xTOG76MZhEEteTvZ8IuskC/l x+3w== X-Gm-Message-State: AOJu0YwH6yzu7VGJCTgtenXQ7tZDQVCSon3cW3n8zYUDY2eCU5pa0xie kNVNkiy20VgeOAntc3uv97Z2v4LwMotNqjlWdtfv1/AXElVm9TlsOPkgcQ== X-Google-Smtp-Source: AGHT+IHYwl/wWSIgQBiKF6LhklpCMKyt7yJpW0BPnFQOvxl9nk9zcuizFncNV6hE8/C13DcyhbWeIw== X-Received: by 2002:a05:6808:3086:b0:3c7:2066:8f8c with SMTP id bl6-20020a056808308600b003c720668f8cmr1039022oib.2.1713277503048; Tue, 16 Apr 2024 07:25:03 -0700 (PDT) Received: from localhost.localdomain ([70.114.247.242]) by smtp.gmail.com with ESMTPSA id r33-20020a05683044a100b006eb892647dasm526999otv.79.2024.04.16.07.25.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 07:25:02 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH v2 4/4] unit: Add tests for the new bitmap utilities Date: Tue, 16 Apr 2024 09:24:55 -0500 Message-ID: <20240416142458.3353383-4-denkenz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240416142458.3353383-1-denkenz@gmail.com> References: <20240416142458.3353383-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 --- unit/test-util.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/unit/test-util.c b/unit/test-util.c index f7a895f911d5..b178b86cf489 100644 --- a/unit/test-util.c +++ b/unit/test-util.c @@ -195,6 +195,81 @@ static void test_safe_atoux(const void *test_data) assert(l_safe_atox8("0xffff", &c) == -ERANGE); } +static void test_set_bit(const void *test_data) +{ + uint32_t bitmap[2] = { }; + int one = 0; + + L_BIT_SET(&bitmap[0], 0); + L_BIT_SET(bitmap, 1); + L_BIT_SET(bitmap, 2); + L_BIT_SET(bitmap, 3); + + assert(bitmap[0] == 0x0f); + assert(bitmap[1] == 0); + + L_BIT_SET(bitmap, 63); + L_BIT_SET(bitmap, 62); + L_BIT_SET(bitmap, 61); + L_BIT_SET(bitmap, 60); + + assert(bitmap[0] == 0x0fU); + assert(bitmap[1] == 0xf0000000U); + + L_BIT_SET(&one, 0); + assert(one == 1); +} + +static void test_clear_bit(const void *test_data) +{ + uint32_t bitmap[2] = { 0xfU, 0xf0000000U }; + + L_BIT_CLEAR(&bitmap[0], 3); + L_BIT_CLEAR(bitmap, 63); + + assert(bitmap[0] == 0x07U); + assert(bitmap[1] == 0x70000000U); +} + +static void test_is_bit_set(const void *test_data) +{ + uint32_t bitmap[2] = { 0xfU, 0xf0000000U }; + uint8_t one = 1; + + assert(L_BIT_TEST(&bitmap[0], 0) == true); + assert(L_BIT_TEST(bitmap, 1) == true); + assert(L_BIT_TEST(bitmap, 2) == true); + assert(L_BIT_TEST(bitmap, 3) == true); + assert(L_BIT_TEST(bitmap, 4) == false); + + assert(L_BIT_TEST(bitmap, 63) == true); + assert(L_BIT_TEST(bitmap, 55) == false); + + assert(L_BIT_TEST(&one, 0) == true); + assert(L_BIT_TEST(&one, 1) == false); +} + +static void test_set_bits(const void *test_data) +{ + uint16_t bitmap[4] = {}; + + L_BITS_SET(bitmap, 0, 1, 16, 32, 48); + + assert(bitmap[0] == 0x3); + assert(bitmap[1] == 0x1); + assert(bitmap[2] == 0x1); + assert(bitmap[3] == 0x1); +} + +static void test_clear_bits(const void *test_data) +{ + uint16_t bitmap[4] = { 0x3, 0x1, 0x1, 0x1 }; + + L_BITS_CLEAR(bitmap, 0, 1, 16, 32, 48); + + assert(l_memeqzero(bitmap, sizeof(bitmap))); +} + int main(int argc, char *argv[]) { l_test_init(&argc, &argv); @@ -212,5 +287,11 @@ int main(int argc, char *argv[]) l_test_add("l_safe_atoux", test_safe_atoux, NULL); + l_test_add("L_BIT_SET", test_set_bit, NULL); + l_test_add("L_BIT_CLEAR", test_clear_bit, NULL); + l_test_add("L_BIT_TEST", test_is_bit_set, NULL); + l_test_add("L_BITS_SET", test_set_bits, NULL); + l_test_add("L_BITS_CLEAR", test_clear_bits, NULL); + return l_test_run(); }