diff mbox series

[2/5] bitwise: accept all ones as non-restricted value

Message ID 20220627190540.13358-3-luc.vanoostenryck@gmail.com (mailing list archive)
State New, archived
Headers show
Series allow -1 and compares in bitwise types | expand

Commit Message

Luc Van Oostenryck June 27, 2022, 7:05 p.m. UTC
Currently, the only value bitwise types can act on is 0
because the this value is anyway invariant for all bitwise
operations and endianness conversions.

But, a bit-pattern of all ones has the same properties and
is also very often used.

So, accept all ones as a valid value for bitwise operations.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 evaluate.c                |  2 +-
 validation/bitwise-cast.c | 13 +++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

Comments

Ramsay Jones June 27, 2022, 11:32 p.m. UTC | #1
On 27/06/2022 20:05, Luc Van Oostenryck wrote:
> Currently, the only value bitwise types can act on is 0
> because the this value is anyway invariant for all bitwise

s/the this/this/
s/anyway//

ATB,
Ramsay Jones

> operations and endianness conversions.
> 
> But, a bit-pattern of all ones has the same properties and
> is also very often used.
> 
> So, accept all ones as a valid value for bitwise operations.
> 
> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
> ---
>  evaluate.c                |  2 +-
>  validation/bitwise-cast.c | 13 +++++++++++++
>  2 files changed, 14 insertions(+), 1 deletion(-)
> 
> diff --git a/evaluate.c b/evaluate.c
> index 61f59ee3908e..bcbcdf1ef0cc 100644
> --- a/evaluate.c
> +++ b/evaluate.c
> @@ -417,7 +417,7 @@ static int restricted_value(struct expression *v, struct symbol *type)
>  {
>  	if (v->type != EXPR_VALUE)
>  		return 1;
> -	if (v->value != 0)
> +	if (v->value != 0 && v->value != bits_mask(type->bit_size))
>  		return 1;
>  	return 0;
>  }
> diff --git a/validation/bitwise-cast.c b/validation/bitwise-cast.c
> index 0583461cb745..1075a3e9410c 100644
> --- a/validation/bitwise-cast.c
> +++ b/validation/bitwise-cast.c
> @@ -35,6 +35,19 @@ static __be32 quuy(void)
>  	return (__attribute__((force)) __be32) 1730;
>  }
>  
> +/* Implicit casts of all ones, legal */
> +static __be32 foo1(void)
> +{
> +	__be32 x = 0xffffffff;
> +	return x;
> +}
> +
> +/* Explicit cast of all ones, legal */
> +static __be32 bar1(void)
> +{
> +	return (__be32)0xffffffff;
> +}
> +
>  /*
>   * check-name: conversions to bitwise types
>   * check-command: sparse -Wbitwise $file
diff mbox series

Patch

diff --git a/evaluate.c b/evaluate.c
index 61f59ee3908e..bcbcdf1ef0cc 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -417,7 +417,7 @@  static int restricted_value(struct expression *v, struct symbol *type)
 {
 	if (v->type != EXPR_VALUE)
 		return 1;
-	if (v->value != 0)
+	if (v->value != 0 && v->value != bits_mask(type->bit_size))
 		return 1;
 	return 0;
 }
diff --git a/validation/bitwise-cast.c b/validation/bitwise-cast.c
index 0583461cb745..1075a3e9410c 100644
--- a/validation/bitwise-cast.c
+++ b/validation/bitwise-cast.c
@@ -35,6 +35,19 @@  static __be32 quuy(void)
 	return (__attribute__((force)) __be32) 1730;
 }
 
+/* Implicit casts of all ones, legal */
+static __be32 foo1(void)
+{
+	__be32 x = 0xffffffff;
+	return x;
+}
+
+/* Explicit cast of all ones, legal */
+static __be32 bar1(void)
+{
+	return (__be32)0xffffffff;
+}
+
 /*
  * check-name: conversions to bitwise types
  * check-command: sparse -Wbitwise $file