Alsa-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] alsa: add annotations to bitwise type snd_pcm_hw_param_t
@ 2008-06-18 20:45 Harvey Harrison
  2008-06-19  8:34 ` Jaroslav Kysela
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Harvey Harrison @ 2008-06-18 20:45 UTC (permalink / raw)
  To: alsa-devel; +Cc: Alexander Viro

Fully half of all alsa sparse warnings are from snd_pcm_hw_param_t degrading
to integer type, this goes a long way towards eliminating them.

Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
---
 include/sound/asound.h |   12 ++++++++----
 include/sound/pcm.h    |   32 ++++++++++++++++----------------
 2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/include/sound/asound.h b/include/sound/asound.h
index 3eaf155..0309da2 100644
--- a/include/sound/asound.h
+++ b/include/sound/asound.h
@@ -302,6 +302,8 @@ typedef int __bitwise snd_pcm_hw_param_t;
 #define	SNDRV_PCM_HW_PARAM_SUBFORMAT	((__force snd_pcm_hw_param_t) 2) /* Subformat */
 #define	SNDRV_PCM_HW_PARAM_FIRST_MASK	SNDRV_PCM_HW_PARAM_ACCESS
 #define	SNDRV_PCM_HW_PARAM_LAST_MASK	SNDRV_PCM_HW_PARAM_SUBFORMAT
+#define SNDRV_PCM_HW_PARAM_MASK_INDEX(var)	\
+	((__force int)(var) - (__force int)SNDRV_PCM_HW_PARAM_FIRST_MASK)
 
 #define	SNDRV_PCM_HW_PARAM_SAMPLE_BITS	((__force snd_pcm_hw_param_t) 8) /* Bits per sample */
 #define	SNDRV_PCM_HW_PARAM_FRAME_BITS	((__force snd_pcm_hw_param_t) 9) /* Bits per frame */
@@ -317,6 +319,8 @@ typedef int __bitwise snd_pcm_hw_param_t;
 #define	SNDRV_PCM_HW_PARAM_TICK_TIME	((__force snd_pcm_hw_param_t) 19) /* Approx tick duration in us */
 #define	SNDRV_PCM_HW_PARAM_FIRST_INTERVAL	SNDRV_PCM_HW_PARAM_SAMPLE_BITS
 #define	SNDRV_PCM_HW_PARAM_LAST_INTERVAL	SNDRV_PCM_HW_PARAM_TICK_TIME
+#define SNDRV_PCM_HW_PARAM_INTERVAL_INDEX(var)	\
+	((__force int)(var) - (__force int)SNDRV_PCM_HW_PARAM_FIRST_INTERVAL)
 
 #define SNDRV_PCM_HW_PARAMS_NORESAMPLE		(1<<0)	/* avoid rate resampling */
 
@@ -336,11 +340,11 @@ struct snd_mask {
 
 struct snd_pcm_hw_params {
 	unsigned int flags;
-	struct snd_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK - 
-			       SNDRV_PCM_HW_PARAM_FIRST_MASK + 1];
+	struct snd_mask masks[
+		SNDRV_PCM_HW_PARAM_MASK_INDEX(SNDRV_PCM_HW_PARAM_LAST_MASK) + 1];
 	struct snd_mask mres[5];	/* reserved masks */
-	struct snd_interval intervals[SNDRV_PCM_HW_PARAM_LAST_INTERVAL -
-				        SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1];
+	struct snd_interval intervals[
+		SNDRV_PCM_HW_PARAM_INTERVAL_INDEX(SNDRV_PCM_HW_PARAM_LAST_INTERVAL) + 1];
 	struct snd_interval ires[9];	/* reserved intervals */
 	unsigned int rmask;		/* W: requested masks */
 	unsigned int cmask;		/* R: changed masks */
diff --git a/include/sound/pcm.h b/include/sound/pcm.h
index 51d58cc..5315b53 100644
--- a/include/sound/pcm.h
+++ b/include/sound/pcm.h
@@ -209,10 +209,10 @@ struct snd_pcm_hw_rule {
 };
 
 struct snd_pcm_hw_constraints {
-	struct snd_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK - 
-			 SNDRV_PCM_HW_PARAM_FIRST_MASK + 1];
-	struct snd_interval intervals[SNDRV_PCM_HW_PARAM_LAST_INTERVAL -
-			     SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1];
+	struct snd_mask masks[
+		SNDRV_PCM_HW_PARAM_MASK_INDEX(SNDRV_PCM_HW_PARAM_LAST_MASK) + 1];
+	struct snd_interval intervals[
+		SNDRV_PCM_HW_PARAM_INTERVAL_INDEX(SNDRV_PCM_HW_PARAM_LAST_INTERVAL) + 1];
 	unsigned int rules_num;
 	unsigned int rules_all;
 	struct snd_pcm_hw_rule *rules;
@@ -221,13 +221,13 @@ struct snd_pcm_hw_constraints {
 static inline struct snd_mask *constrs_mask(struct snd_pcm_hw_constraints *constrs,
 					    snd_pcm_hw_param_t var)
 {
-	return &constrs->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
+	return &constrs->masks[SNDRV_PCM_HW_PARAM_MASK_INDEX(var)];
 }
 
 static inline struct snd_interval *constrs_interval(struct snd_pcm_hw_constraints *constrs,
 						    snd_pcm_hw_param_t var)
 {
-	return &constrs->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
+	return &constrs->intervals[SNDRV_PCM_HW_PARAM_INTERVAL_INDEX(var)];
 }
 
 struct snd_ratnum {
@@ -761,40 +761,40 @@ static inline void snd_pcm_trigger_done(struct snd_pcm_substream *substream,
 	substream->runtime->trigger_master = master;
 }
 
-static inline int hw_is_mask(int var)
+static inline int hw_is_mask(snd_pcm_hw_param_t var)
 {
-	return var >= SNDRV_PCM_HW_PARAM_FIRST_MASK &&
-		var <= SNDRV_PCM_HW_PARAM_LAST_MASK;
+	return (__force int)var >= (__force int)SNDRV_PCM_HW_PARAM_FIRST_MASK &&
+		(__force int)var <= (__force int)SNDRV_PCM_HW_PARAM_LAST_MASK;
 }
 
-static inline int hw_is_interval(int var)
+static inline int hw_is_interval(snd_pcm_hw_param_t var)
 {
-	return var >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL &&
-		var <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL;
+	return (__force int)var >= (__force int)SNDRV_PCM_HW_PARAM_FIRST_INTERVAL &&
+		(__force int)var <= (__force int)SNDRV_PCM_HW_PARAM_LAST_INTERVAL;
 }
 
 static inline struct snd_mask *hw_param_mask(struct snd_pcm_hw_params *params,
 				     snd_pcm_hw_param_t var)
 {
-	return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
+	return &params->masks[SNDRV_PCM_HW_PARAM_MASK_INDEX(var)];
 }
 
 static inline struct snd_interval *hw_param_interval(struct snd_pcm_hw_params *params,
 					     snd_pcm_hw_param_t var)
 {
-	return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
+	return &params->intervals[SNDRV_PCM_HW_PARAM_INTERVAL_INDEX(var)];
 }
 
 static inline const struct snd_mask *hw_param_mask_c(const struct snd_pcm_hw_params *params,
 					     snd_pcm_hw_param_t var)
 {
-	return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
+	return &params->masks[SNDRV_PCM_HW_PARAM_MASK_INDEX(var)];
 }
 
 static inline const struct snd_interval *hw_param_interval_c(const struct snd_pcm_hw_params *params,
 						     snd_pcm_hw_param_t var)
 {
-	return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
+	return &params->intervals[SNDRV_PCM_HW_PARAM_INTERVAL_INDEX(var)];
 }
 
 #define params_access(p) snd_mask_min(hw_param_mask((p), SNDRV_PCM_HW_PARAM_ACCESS))
-- 
1.5.6.rc3.295.gdca2e

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] alsa: add annotations to bitwise type snd_pcm_hw_param_t
  2008-06-18 20:45 [PATCH] alsa: add annotations to bitwise type snd_pcm_hw_param_t Harvey Harrison
@ 2008-06-19  8:34 ` Jaroslav Kysela
  2008-06-19  9:21 ` Takashi Iwai
  2008-06-22  7:50 ` Al Viro
  2 siblings, 0 replies; 4+ messages in thread
From: Jaroslav Kysela @ 2008-06-19  8:34 UTC (permalink / raw)
  To: Harvey Harrison; +Cc: alsa-devel, Alexander Viro

On Wed, 18 Jun 2008, Harvey Harrison wrote:

> Fully half of all alsa sparse warnings are from snd_pcm_hw_param_t degrading
> to integer type, this goes a long way towards eliminating them.
> 
> Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>

Applied to ALSA repo. Thanks.

						Jaroslav

-----
Jaroslav Kysela <perex@perex.cz>
Linux Kernel Sound Maintainer
ALSA Project, Red Hat, Inc.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] alsa: add annotations to bitwise type snd_pcm_hw_param_t
  2008-06-18 20:45 [PATCH] alsa: add annotations to bitwise type snd_pcm_hw_param_t Harvey Harrison
  2008-06-19  8:34 ` Jaroslav Kysela
@ 2008-06-19  9:21 ` Takashi Iwai
  2008-06-22  7:50 ` Al Viro
  2 siblings, 0 replies; 4+ messages in thread
From: Takashi Iwai @ 2008-06-19  9:21 UTC (permalink / raw)
  To: Harvey Harrison; +Cc: alsa-devel, Alexander Viro

At Wed, 18 Jun 2008 13:45:13 -0700,
Harvey Harrison wrote:
> 
> Fully half of all alsa sparse warnings are from snd_pcm_hw_param_t degrading
> to integer type, this goes a long way towards eliminating them.

Oh what a cast hell...

> Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
> ---
>  include/sound/asound.h |   12 ++++++++----
>  include/sound/pcm.h    |   32 ++++++++++++++++----------------
>  2 files changed, 24 insertions(+), 20 deletions(-)
> 
> diff --git a/include/sound/asound.h b/include/sound/asound.h
> index 3eaf155..0309da2 100644
> --- a/include/sound/asound.h
> +++ b/include/sound/asound.h
> @@ -302,6 +302,8 @@ typedef int __bitwise snd_pcm_hw_param_t;
>  #define	SNDRV_PCM_HW_PARAM_SUBFORMAT	((__force snd_pcm_hw_param_t) 2) /* Subformat */
>  #define	SNDRV_PCM_HW_PARAM_FIRST_MASK	SNDRV_PCM_HW_PARAM_ACCESS
>  #define	SNDRV_PCM_HW_PARAM_LAST_MASK	SNDRV_PCM_HW_PARAM_SUBFORMAT
> +#define SNDRV_PCM_HW_PARAM_MASK_INDEX(var)	\
> +	((__force int)(var) - (__force int)SNDRV_PCM_HW_PARAM_FIRST_MASK)
>  
>  #define	SNDRV_PCM_HW_PARAM_SAMPLE_BITS	((__force snd_pcm_hw_param_t) 8) /* Bits per sample */
>  #define	SNDRV_PCM_HW_PARAM_FRAME_BITS	((__force snd_pcm_hw_param_t) 9) /* Bits per frame */
> @@ -317,6 +319,8 @@ typedef int __bitwise snd_pcm_hw_param_t;
>  #define	SNDRV_PCM_HW_PARAM_TICK_TIME	((__force snd_pcm_hw_param_t) 19) /* Approx tick duration in us */
>  #define	SNDRV_PCM_HW_PARAM_FIRST_INTERVAL	SNDRV_PCM_HW_PARAM_SAMPLE_BITS
>  #define	SNDRV_PCM_HW_PARAM_LAST_INTERVAL	SNDRV_PCM_HW_PARAM_TICK_TIME
> +#define SNDRV_PCM_HW_PARAM_INTERVAL_INDEX(var)	\
> +	((__force int)(var) - (__force int)SNDRV_PCM_HW_PARAM_FIRST_INTERVAL)
>  
>  #define SNDRV_PCM_HW_PARAMS_NORESAMPLE		(1<<0)	/* avoid rate resampling */
>  
> @@ -336,11 +340,11 @@ struct snd_mask {
>  
>  struct snd_pcm_hw_params {
>  	unsigned int flags;
> -	struct snd_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK - 
> -			       SNDRV_PCM_HW_PARAM_FIRST_MASK + 1];
> +	struct snd_mask masks[
> +		SNDRV_PCM_HW_PARAM_MASK_INDEX(SNDRV_PCM_HW_PARAM_LAST_MASK) + 1];

Maybe better to define this as SNDRV_PCM_HW_PARAM_MASKS or so
beforehand?  (Ditto for SNDRV_PCM_HW_PARAM_INTERVALS.)
Then, the below would be a bit simpler.

> @@ -761,40 +761,40 @@ static inline void snd_pcm_trigger_done(struct snd_pcm_substream *substream,
>  	substream->runtime->trigger_master = master;
>  }
>  
> -static inline int hw_is_mask(int var)
> +static inline int hw_is_mask(snd_pcm_hw_param_t var)
>  {
> -	return var >= SNDRV_PCM_HW_PARAM_FIRST_MASK &&
> -		var <= SNDRV_PCM_HW_PARAM_LAST_MASK;
> +	return (__force int)var >= (__force int)SNDRV_PCM_HW_PARAM_FIRST_MASK &&
> +		(__force int)var <= (__force int)SNDRV_PCM_HW_PARAM_LAST_MASK;

static inline int hw_is_mask(snd_pcm_hw_param_t var)
{
	unsigned int idx = SNDRV_PCM_HW_PARAM_MASK_INDEX(var);
	return idx < SNDRV_PCM_HW_PARAM_MASKS;
}


Or, we should git rid of __bitwise for snd_pcm_hw_param_t instead of
cast.  The bitwise check doesn't help debugging much for this type in
the end.


thanks,

Takashi

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] alsa: add annotations to bitwise type snd_pcm_hw_param_t
  2008-06-18 20:45 [PATCH] alsa: add annotations to bitwise type snd_pcm_hw_param_t Harvey Harrison
  2008-06-19  8:34 ` Jaroslav Kysela
  2008-06-19  9:21 ` Takashi Iwai
@ 2008-06-22  7:50 ` Al Viro
  2 siblings, 0 replies; 4+ messages in thread
From: Al Viro @ 2008-06-22  7:50 UTC (permalink / raw)
  To: Harvey Harrison; +Cc: alsa-devel, Alexander Viro

On Wed, Jun 18, 2008 at 01:45:13PM -0700, Harvey Harrison wrote:
> Fully half of all alsa sparse warnings are from snd_pcm_hw_param_t degrading
> to integer type, this goes a long way towards eliminating them.

FWIW, *all* this stuff is not bitwise at all.  For crying out loud, half
of these types are routinely used as array indices and loop variables...

If anything, we want a different set of allowed operations - subtraction
between elements of type (yielding integer), addition/subtraction of
integer types not bigger than ours (yielding our type), comparisons,
assignments (=, +=, -=, passing to function as argument, return from
function, initializers) and second/third arguments in ?:.  With 0 *not*
being allowed as a constant of such type.

It's not bitwise; we may use the same infrastructure in sparse, but it
should be a separate class of types (__attribute__((affine))).

dma_addr_t is another candidate for the same treatment, but there we'll
need helpers for conversions to hw-acceptable form (dma_to_le32(), etc.)
and gradual conversion of drivers.

ALSA ones and pm mess are absolutely straightforward cases, though.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-06-22  7:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-18 20:45 [PATCH] alsa: add annotations to bitwise type snd_pcm_hw_param_t Harvey Harrison
2008-06-19  8:34 ` Jaroslav Kysela
2008-06-19  9:21 ` Takashi Iwai
2008-06-22  7:50 ` Al Viro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox