From: David Laight <David.Laight@ACULAB.COM> To: 'Takashi Iwai' <tiwai@suse.de> Cc: 'Arnd Bergmann' <arnd@arndb.de>, Jaroslav Kysela <perex@perex.cz>, "Takashi Iwai" <tiwai@suse.com>, "alsa-devel@alsa-project.org" <alsa-devel@alsa-project.org>, "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org> Subject: RE: [PATCH] ALSA: opti9xx: shut up gcc-10 range warning Date: Thu, 30 Apr 2020 09:52:09 +0000 [thread overview] Message-ID: <94cc90ff07364743809698fae2ef7138@AcuMS.aculab.com> (raw) In-Reply-To: <s5hk11xidob.wl-tiwai@suse.de> From: Takashi Iwai > Sent: 30 April 2020 09:25 > > On Thu, 30 Apr 2020 10:15:02 +0200, > David Laight wrote: > > > > From: Arnd Bergmann .... > > > +static inline void snd_miro_write_mask(struct snd_miro *chip, > > > + unsigned char reg, unsigned char value, unsigned char mask) > > > +{ > > > + unsigned char oldval = snd_miro_read(chip, reg); > > > > > > -#define snd_miro_write_mask(chip, reg, value, mask) \ > > > - snd_miro_write(chip, reg, \ > > > - (snd_miro_read(chip, reg) & ~(mask)) | ((value) & (mask))) > > > + snd_miro_write(chip, reg, (oldval & ~mask) | (value & mask)); > > > +} > > > > Isn't that likely to add additional masking with 0xff at the call sites? > > You will probably get better code if the arguments are 'unsigned int'. > > I don't think such a micro optimization is needed. > All registers, values and masks in the driver are 8bit, so keeping all > unsigned char is rather an improvement of readability. And every time you do any arithmetic they get extended to int. And if you pass them to a function (as char) the compiler has to mask the result of any arithmetic back to 8 bits. On x86 the compiler can use the 'as if' rule and do 8 bit arithmetic. But only if it can determine that the high bits aren't actually used. On almost every other architecture you are likely to get a lot of masking operations. Just because the domain of a variable of 0..255 doesn't mean that 'unsigned char' is an appropriate type for a variable. For x86-64 (and probably others) 'unsigned int' is usually best for anything that cannot be negative. In particular it saves the sign extension instruction that has to be inserted when an 'int' variable is used as an array index. FWIW I think that somewhere else the ~mask had to be replaced with (mask ^ 0xff) do avoid another spurious compiler warning. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
WARNING: multiple messages have this Message-ID (diff)
From: David Laight <David.Laight@ACULAB.COM> To: 'Takashi Iwai' <tiwai@suse.de> Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>, "alsa-devel@alsa-project.org" <alsa-devel@alsa-project.org>, Takashi Iwai <tiwai@suse.com>, 'Arnd Bergmann' <arnd@arndb.de> Subject: RE: [PATCH] ALSA: opti9xx: shut up gcc-10 range warning Date: Thu, 30 Apr 2020 09:52:09 +0000 [thread overview] Message-ID: <94cc90ff07364743809698fae2ef7138@AcuMS.aculab.com> (raw) In-Reply-To: <s5hk11xidob.wl-tiwai@suse.de> From: Takashi Iwai > Sent: 30 April 2020 09:25 > > On Thu, 30 Apr 2020 10:15:02 +0200, > David Laight wrote: > > > > From: Arnd Bergmann .... > > > +static inline void snd_miro_write_mask(struct snd_miro *chip, > > > + unsigned char reg, unsigned char value, unsigned char mask) > > > +{ > > > + unsigned char oldval = snd_miro_read(chip, reg); > > > > > > -#define snd_miro_write_mask(chip, reg, value, mask) \ > > > - snd_miro_write(chip, reg, \ > > > - (snd_miro_read(chip, reg) & ~(mask)) | ((value) & (mask))) > > > + snd_miro_write(chip, reg, (oldval & ~mask) | (value & mask)); > > > +} > > > > Isn't that likely to add additional masking with 0xff at the call sites? > > You will probably get better code if the arguments are 'unsigned int'. > > I don't think such a micro optimization is needed. > All registers, values and masks in the driver are 8bit, so keeping all > unsigned char is rather an improvement of readability. And every time you do any arithmetic they get extended to int. And if you pass them to a function (as char) the compiler has to mask the result of any arithmetic back to 8 bits. On x86 the compiler can use the 'as if' rule and do 8 bit arithmetic. But only if it can determine that the high bits aren't actually used. On almost every other architecture you are likely to get a lot of masking operations. Just because the domain of a variable of 0..255 doesn't mean that 'unsigned char' is an appropriate type for a variable. For x86-64 (and probably others) 'unsigned int' is usually best for anything that cannot be negative. In particular it saves the sign extension instruction that has to be inserted when an 'int' variable is used as an array index. FWIW I think that somewhere else the ~mask had to be replaced with (mask ^ 0xff) do avoid another spurious compiler warning. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2020-04-30 9:52 UTC|newest] Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-04-29 19:02 [PATCH] ALSA: opti9xx: shut up gcc-10 range warning Arnd Bergmann 2020-04-29 19:02 ` Arnd Bergmann 2020-04-30 6:12 ` Takashi Iwai 2020-04-30 6:12 ` Takashi Iwai 2020-04-30 8:22 ` Takashi Iwai 2020-04-30 8:22 ` Takashi Iwai 2020-04-30 8:15 ` David Laight 2020-04-30 8:24 ` Takashi Iwai 2020-04-30 8:24 ` Takashi Iwai 2020-04-30 9:52 ` David Laight [this message] 2020-04-30 9:52 ` David Laight
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=94cc90ff07364743809698fae2ef7138@AcuMS.aculab.com \ --to=david.laight@aculab.com \ --cc=alsa-devel@alsa-project.org \ --cc=arnd@arndb.de \ --cc=linux-kernel@vger.kernel.org \ --cc=perex@perex.cz \ --cc=tiwai@suse.com \ --cc=tiwai@suse.de \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.