diff mbox series

[3/5] bitwise: allow compares for bitwise types

Message ID 20220627190540.13358-4-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, bitwise types are restricted to bitwise operations
(&, |, ^ and ~) as well as equality comparisons.

This patch makes the others comparisons valid for bitwise types
too.

Warning: This change make sense in the context of [1] but
         doesn't make sense for the 'main' bitwise types:
         __be32 and friends.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 evaluate.c                       | 4 ++++
 validation/bitwise-cmp.c         | 1 -
 validation/linear/bitwise-cmps.c | 1 -
 3 files changed, 4 insertions(+), 2 deletions(-)

Comments

Linus Torvalds June 27, 2022, 7:20 p.m. UTC | #1
On Mon, Jun 27, 2022 at 12:05 PM Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
>
> Currently, bitwise types are restricted to bitwise operations
> (&, |, ^ and ~) as well as equality comparisons.
>
> This patch makes the others comparisons valid for bitwise types
> too.
>
> Warning: This change make sense in the context of [1] but
>          doesn't make sense for the 'main' bitwise types:
>          __be32 and friends.

Yeah, this is wrong.

It will literally break one of the use-cases, which is endianness comparisons.

You cannot compare values in the wrong endianness for greater-than or
less-than, because you will get the wrong answer - the ordering is
different in different byte-orders.

But comparing for equality (and inequality) is fine, and we actually
do that in the kernel (ie you can take a big-endian value, and compare
it with another big-endian value for being equal, without converting
it to the local CPU endianness).

Now, comparing the *constants* 0 and all-ones is fine. They are
smaller than (and larger than) all other values, regardless of any
byte/bit order issues.

So I think that really needs to check that one (or both) sides are the
magic constants.

             Linus
Ramsay Jones June 27, 2022, 11:34 p.m. UTC | #2
On 27/06/2022 20:05, Luc Van Oostenryck wrote:
> Currently, bitwise types are restricted to bitwise operations
> (&, |, ^ and ~) as well as equality comparisons.
> 
> This patch makes the others comparisons valid for bitwise types
> too.
> 
> Warning: This change make sense in the context of [1] but

The [1] reference seems to be missing :)

ATB,
Ramsay Jones

>          doesn't make sense for the 'main' bitwise types:
>          __be32 and friends.
> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
> ---
>  evaluate.c                       | 4 ++++
>  validation/bitwise-cmp.c         | 1 -
>  validation/linear/bitwise-cmps.c | 1 -
>  3 files changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/evaluate.c b/evaluate.c
> index bcbcdf1ef0cc..bb8c0caa905a 100644
> --- a/evaluate.c
> +++ b/evaluate.c
> @@ -435,6 +435,10 @@ static int restricted_binop(int op, struct symbol *type)
>  		case '^':
>  		case '?':
>  			return 2;	/* keep fouled */
> +		case '<':
> +		case '>':
> +		case SPECIAL_LTE:
> +		case SPECIAL_GTE:
>  		case SPECIAL_EQUAL:
>  		case SPECIAL_NOTEQUAL:
>  			return 3;	/* warn if fouled */
> diff --git a/validation/bitwise-cmp.c b/validation/bitwise-cmp.c
> index ca12b5e51e8e..8c3e6894072d 100644
> --- a/validation/bitwise-cmp.c
> +++ b/validation/bitwise-cmp.c
> @@ -28,5 +28,4 @@ static int gtx(b32 x, b32 y)  { return (x >  y); }
>  
>  /*
>   * check-name: bitwise-cmp
> - * check-known-to-fail
>   */
> diff --git a/validation/linear/bitwise-cmps.c b/validation/linear/bitwise-cmps.c
> index 6122944a42c6..f83ab7fe47db 100644
> --- a/validation/linear/bitwise-cmps.c
> +++ b/validation/linear/bitwise-cmps.c
> @@ -8,7 +8,6 @@ static int gtu(bs32 x, bs32 y)  { return (x >  y); }
>  /*
>   * check-name: bitwise-cmps
>   * check-command: test-linearize -Wno-decl $file
> - * check-known-to-fail
>   *
>   * check-output-ignore
>   * check-output-excludes: setb\\.
diff mbox series

Patch

diff --git a/evaluate.c b/evaluate.c
index bcbcdf1ef0cc..bb8c0caa905a 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -435,6 +435,10 @@  static int restricted_binop(int op, struct symbol *type)
 		case '^':
 		case '?':
 			return 2;	/* keep fouled */
+		case '<':
+		case '>':
+		case SPECIAL_LTE:
+		case SPECIAL_GTE:
 		case SPECIAL_EQUAL:
 		case SPECIAL_NOTEQUAL:
 			return 3;	/* warn if fouled */
diff --git a/validation/bitwise-cmp.c b/validation/bitwise-cmp.c
index ca12b5e51e8e..8c3e6894072d 100644
--- a/validation/bitwise-cmp.c
+++ b/validation/bitwise-cmp.c
@@ -28,5 +28,4 @@  static int gtx(b32 x, b32 y)  { return (x >  y); }
 
 /*
  * check-name: bitwise-cmp
- * check-known-to-fail
  */
diff --git a/validation/linear/bitwise-cmps.c b/validation/linear/bitwise-cmps.c
index 6122944a42c6..f83ab7fe47db 100644
--- a/validation/linear/bitwise-cmps.c
+++ b/validation/linear/bitwise-cmps.c
@@ -8,7 +8,6 @@  static int gtu(bs32 x, bs32 y)  { return (x >  y); }
 /*
  * check-name: bitwise-cmps
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-excludes: setb\\.