Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp2179068imd; Sun, 28 Oct 2018 04:50:05 -0700 (PDT) X-Google-Smtp-Source: AJdET5e+Ox0NLU2Rehed1aCdkLZ2tojBHItP5ga9PsZP6OFuKPY/VE5IglkBAOaQ+4gqplt+ML+y X-Received: by 2002:a17:902:758f:: with SMTP id j15-v6mr10507735pll.160.1540727405145; Sun, 28 Oct 2018 04:50:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540727405; cv=none; d=google.com; s=arc-20160816; b=z8qoBQbVEii1kv0DQD5833lwWO5Ee/6zYWXATnKz/UfoubLG4ujJJiq1MT4UwXMFyd pVitkZWAmTLzmOu5qG5mRHomUg6SnA/HB1G6/12YhYHBuGCXBOs4obI+LcIqCS0YPwve v6dDx2aUH5YyX8q+1zWGYyIwnMo0i8HIyrhLnsKaKq3jD67xbxYmpj1NviU6GXpdwVhg 1meIX/zh4a3qn4Q6v+Rq2ZosgqWeXWc+nQR64IT+4Q/ZlcCXPyGDE5g6XzlRbO3aEpwz BeKS1An9vVDtMy6kVmikUwe9nS9AL8YFviRe2KiGI9HDUf0fwWNc12x11A/AOuXu9Io6 Ov5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=THW8ZDxCUih461bbwxroSxqP/PoK7NAB867x2oJAU0U=; b=O4anulavlIO2a1lX9tlEgkTTPTz6JTgLQZygWeg7DpV6Rd88TamZS/4Y47FqX7WLz2 EsBrs/hXUZdP5Dvxe6vt6Ye8FFnk33a8biEcKiywphg/82XKPwnGByhwIGx6WvrE7H53 vb1MGQl1+4J9y4E7X5G8V3GLUbBZaSc0MPZomHtn13YIgSQzTdJhoqx+KKYc+GYklHXZ 5+nzMNDGTyUtxhyPCN00v+enhFc5nDIwEYaKYc3sGsOXIRUOjHYxMP3wFvHD/UIjef0v i+D2bGT9HLmCSb2hfghvWbxL1YnUebuH6J6GulA8Leu3lm3m3aPBXLflLIPlBXJsk102 owVA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l30-v6si3869689pgb.404.2018.10.28.04.49.49; Sun, 28 Oct 2018 04:50:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727420AbeJ1UbK (ORCPT + 99 others); Sun, 28 Oct 2018 16:31:10 -0400 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:25385 "EHLO mail3-relais-sop.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726354AbeJ1UbK (ORCPT ); Sun, 28 Oct 2018 16:31:10 -0400 X-IronPort-AV: E=Sophos;i="5.54,436,1534802400"; d="scan'208";a="283549915" Received: from 89-157-201-244.rev.numericable.fr (HELO hadrien) ([89.157.201.244]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Oct 2018 12:46:42 +0100 Date: Sun, 28 Oct 2018 12:46:42 +0100 (CET) From: Julia Lawall X-X-Sender: jll@hadrien To: Himanshu Jha cc: Sasha Levin , Shayenne da Luz Moura , Greg Kroah-Hartman , Hans de Goede , Michael Thayer , devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, outreachy-kernel@googlegroups.com Subject: Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool In-Reply-To: <20181028112011.GA5157@himanshu-Vostro-3559> Message-ID: References: <211701e4ae42acd95afb24713314bce5a4c58ecf.1540580493.git.shayenneluzmoura@gmail.com> <20181026204225.GH2015@sasha-vm> <20181028075209.GA1938@himanshu-Vostro-3559> <20181028112011.GA5157@himanshu-Vostro-3559> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 28 Oct 2018, Himanshu Jha wrote: > On Sun, Oct 28, 2018 at 09:47:15AM +0100, Julia Lawall wrote: > > > The "possible alignement issues" in CHECK report is difficult to figure > > > out by just doing a glance analysis. :) > > > > > > Linus also suggested to use bool as the base type i.e., `bool x:1` but > > > again sizeof(_Bool) is implementation defined ranging from 1-4 bytes. > > > > If bool x:1 has the size of bool, then wouldn't int x:1 have the size of > > int? But my little experiments suggest that the size is the smallest that > > fits the requested bits and alignment chosen by the compiler, regardless of > > the type. > > Yes, correct! > And we can't use sizeof on bitfields *directly*, nor reference it using a > pointer. > > It can be applied only when these bitfields are wrapped in a structure. > > Testing: > > #include > #include > > struct S { > bool a:1; > bool b:1; > bool c:1; > bool d:1; > }; > > int main(void) > { > printf("%zu\n", sizeof(struct S)); > } > > Output: 1 > > If I change all bool to unsigned int, output is: *4*. > > So, conclusion is compiler doesn't squeeze the size less than > native size of the datatype i.e., if we changed all members to > unsigned int:1, > total width = 4 bits > padding = 4 bits > > Therefore, total size should have been = 1 byte! > But since sizeof(unsigned int) == 4, it can't be squeezed to > less than it. This conclusion does not seem to be correct, if you try the following program. I get 4 for everything, meaning that the four unsigned int bits are getting squeezed into one byte when it is convenient. #include #include struct S1 { bool a:1; bool b:1; bool c:1; bool d:1; char a1; char a2; char a3; }; struct S2 { unsigned int a:1; unsigned int b:1; unsigned int c:1; unsigned int d:1; char a1; char a2; char a3; }; int main(void) { printf("%zu\n", sizeof(struct S1)); printf("%zu\n", sizeof(struct S2)); printf("%zu\n", sizeof(unsigned int)); } > Well, int x:1 can either have 0..1 or -1..0 range due implementation > defined behavior as I said in the previous reply. > > If you really want to consider negative values, then make it explicit > using `signed int x:1` which make range guaranteed to be -1..0 The code wants booleans, not negative values. julia