Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp5190756imd; Tue, 30 Oct 2018 13:26:15 -0700 (PDT) X-Google-Smtp-Source: AJdET5c7fBTRUfnkHrSdYDWW/54kSGFaLiyV3R+17KvPhGLnlfll9DWCCULmbmbk7nkfUQ0cGaA0 X-Received: by 2002:a17:902:5e3:: with SMTP id f90-v6mr209928plf.286.1540931175188; Tue, 30 Oct 2018 13:26:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540931175; cv=none; d=google.com; s=arc-20160816; b=g8nVEYiHTQl6TqtvNxzyc/18Z0L0WXXx3AlGN/AjAPUJAUPGiVLrhjWpbh2suSA2JV 6d/XTH+HPRsdowbHhPH1AMItf0rcjmPCRU47kA1uXNThIyFONAMpjQ8L1WJjYJsjGRYX B8MVLbV3oC7q0XAWCuDDK37CmuDl3tgpmNgUnEdNrZH7e5CAQqmpzAQ15BQU7x/K0jBQ eVzkm+tl4QCUurci7Bhd6/L5YWvjwxyTShKrxoSPD/stLWS9zAYyjnH3dyetfXw4ovO4 jqU0X/X6J3WPuxgsj6pdw4RY/rbUVs1v25GgabMsqgOEmxnn1FfSCmJ8W1kap0MHMdvH 4nmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=jEHvye6C0RTiHbryQD6FLvfoQR2BpbybsT0rnRdO/jM=; b=zXUYMb0rORT/w9ISTIhiE8QWO8nWa2EtZLALlwc0cqzGOnCZ98qvzuvVDnF8k9Ffoe BPxR3h51rVc51oOZufYFMjoKLVTnKIsBqVgnUgGhCpCYZYb/NQwJn5BiYLCvqNYmPX3P la/UbB9mJoCb6sFuJVVip2b99iWKqihp7dAvdp8zKPVX/ziRVl5sHStgVPtyYfFK6q7t G94aBbKAPozNFKjD6w9cOI5jUh++lPnjYozc78YybibimhYMhmRcL4sJyOzhpOhmxPG1 IJnvmaGFI3npu57UbJRoEV9OwjBJ+bgU4iben0iVgBqZGXQE179ai1mmhC3ESG8kfjdE tJKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=b35BsBf7; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y5-v6si16047860pgo.310.2018.10.30.13.25.59; Tue, 30 Oct 2018 13:26:15 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=b35BsBf7; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726799AbeJaFNT (ORCPT + 99 others); Wed, 31 Oct 2018 01:13:19 -0400 Received: from mail-qk1-f195.google.com ([209.85.222.195]:39479 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725962AbeJaFNT (ORCPT ); Wed, 31 Oct 2018 01:13:19 -0400 Received: by mail-qk1-f195.google.com with SMTP id e4so8126272qkh.6 for ; Tue, 30 Oct 2018 13:18:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=jEHvye6C0RTiHbryQD6FLvfoQR2BpbybsT0rnRdO/jM=; b=b35BsBf7QYcwUTqaMT9LMSOOPxSr62pLCQL1fgsrq1bmi74yT59TpajhLCVfbdutlc FXo4hJcrfd/kqgaUSJPoUn87CsYNxFETedtNcg8useYeuzbqIMw1nUJEaMPTjALhcq/0 7TruBvGgE0vq2hUbSGYgJUeSSIKCVK1YZYeK5ER8xYEAnQlajRawt/fgwY7K4HBA3S9a RMKPPapFdxMkqVQ5KQQfadSVo56StFWr2ZVMKoDIQqM3mxx6CP0WM2kEZeSt/3e1wELe sblFM7v0ZzFSWAjHMBUaGE4Oldca7rujrGAHPfNtEmPtekPrTrsM/HN37Nus+6lVOW0m XvRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=jEHvye6C0RTiHbryQD6FLvfoQR2BpbybsT0rnRdO/jM=; b=kOqYDGadpTXqF0XJZVaGlNnUXJDcBWWjyA5zfkC5Mr1Du0moktDoqrmUF4+xzl0yFa 4/sN1YjBqSN4/iSS2kLVRbfJnACUJxQwf/o6Pjto7Wm3YakK2lAdAbQMXy+eUuYtv22q ZXQ1IA9dT/WZhpkjl1gTprXdcNVHEX39Hk1VW8dsdSDFfBYvN+AkmlAxbP9Lh+mV36oo +kU7Kgbuyp4ei+/gboJWdiKVKAjxrNWYGQvOPfmkZtBwSuZ6DVhPyuOVaJQVgklaXTp2 LEeDT9VLQPF6yXOMyvOqk7XhShzmmVpT+3DWlpkCQR3b0tuoUNeUlfWSy9mfZe8c1xuV EOyg== X-Gm-Message-State: AGRZ1gJmraXTDSKIfI5pvQ5xQUDf4f6r67puZedTEICSzUj5ww3VADUq zDfcORulEB8IqW2fyvuDsYI= X-Received: by 2002:a37:7983:: with SMTP id u125-v6mr140855qkc.219.1540930701615; Tue, 30 Oct 2018 13:18:21 -0700 (PDT) Received: from smtp.gmail.com ([143.107.45.1]) by smtp.gmail.com with ESMTPSA id v3-v6sm16832346qth.74.2018.10.30.13.18.19 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 30 Oct 2018 13:18:21 -0700 (PDT) Date: Tue, 30 Oct 2018 17:18:16 -0300 From: Shayenne Moura To: Julia Lawall Cc: Himanshu Jha , Sasha Levin , 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 Message-ID: <20181030201816.joihhwd7htgixo5i@smtp.gmail.com> References: <211701e4ae42acd95afb24713314bce5a4c58ecf.1540580493.git.shayenneluzmoura@gmail.com> <20181026204225.GH2015@sasha-vm> <20181028075209.GA1938@himanshu-Vostro-3559> <20181028112011.GA5157@himanshu-Vostro-3559> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20171215 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, > 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 Thank you all for the discussion! However, I think I do not understand the conclusion. It means that the best way is to use only boolean instead of use unsigned int with bitfield? I mean specifically in the case of my patch, where there are some boolean variables are mixed with other variables types. Best, Shayenne