[m-rev.] for review: teach const_prop.m about <<u/>>u

Peter Wang novalazy at gmail.com
Thu Dec 8 17:03:20 AEDT 2022


On Thu, 08 Dec 2022 08:11:09 +1100 "Zoltan Somogyi" <zoltan.somogyi at runbox.com> wrote:
> Teach const_prop.m about <<u and >>u.
> 
> compiler/const_prop.m:
>     As above.
> 
>     Instead of checking the safety of static evaluation
>     - in const_prop.m for some operations, and
>     - in {int,uint}_emu.m for other operations,
>     do all safety checks in {int,uint}_emu.m.
> 
>     Statically evaluate both quotient/remainder and shift operations,
>     which all have both checked and unchecked versions, only when the
>     checks of the checked version would succeed. Document the reason why.
> 
> compiler/int_emu.m:
> compiler/uint_emu.m:
>     Emulate the static evaluation of checked ops only if the checks succeed.
> 
>     Since the emulations of the checked and unchecked versions would be
>     identical, keep only one version, the one not named "unchecked"
>     (since we *do* checks.)

> diff --git a/compiler/const_prop.m b/compiler/const_prop.m
> index 95a591d94..67c6ebc96 100644
> --- a/compiler/const_prop.m
> +++ b/compiler/const_prop.m
> @@ -604,24 +604,12 @@ evaluate_det_call_uint_3_mode_0(Globals, ProcName, X, Y, Z,
>      ;
>          ConstY = int_const(YVal),
>          (
> -            ProcName = "unchecked_left_shift",
> -            globals.lookup_bool_option(Globals, pregenerated_dist, no),
> -            uint_emu.target_bits_per_uint(Globals, BitsPerUInt),
> -            uint_emu.unchecked_left_shift(BitsPerUInt, XVal, YVal,
> -                OutputArgVal)
> -        ;
> -            ProcName = "<<",
> +            ( ProcName = "<<" ; ProcName = "unchecked_left_shift" ),
>              globals.lookup_bool_option(Globals, pregenerated_dist, no),
>              uint_emu.target_bits_per_uint(Globals, BitsPerUInt),
>              uint_emu.left_shift(BitsPerUInt, XVal, YVal, OutputArgVal)
>          ;
> -            ProcName = "unchecked_right_shift",
> -            globals.lookup_bool_option(Globals, pregenerated_dist, no),
> -            uint_emu.target_bits_per_uint(Globals, BitsPerUInt),
> -            uint_emu.unchecked_right_shift(BitsPerUInt, XVal, YVal,
> -                OutputArgVal)
> -        ;
> -            ProcName = ">>",
> +            ( ProcName = ">>" ; ProcName = "unchecked_right_shift" ),
>              globals.lookup_bool_option(Globals, pregenerated_dist, no),
>              uint_emu.target_bits_per_uint(Globals, BitsPerUInt),
>              uint_emu.right_shift(BitsPerUInt, XVal, YVal, OutputArgVal)

Did you mean to implement the unsigned-shift-amount ops as well?

> diff --git a/compiler/int_emu.m b/compiler/int_emu.m
> index 8cbdbbe61..013008355 100644
> --- a/compiler/int_emu.m
> +++ b/compiler/int_emu.m
...
> +%----------------------------------------------------------------------------%
>  
>  left_shift(BitsPerInt, X, Y, Z) :-
>      BitsPerInt = bits_per_int(N),
> +    Y >= 0,
> +    Y < N,
>      to_int_in_range(BitsPerInt, integer(X) << min(Y, N), Z).

min(Y, N) is redundant now.

>  
> -unchecked_left_shift(BitsPerInt, X, Y, Z) :-
> +left_ushift(BitsPerInt, X, UY, Z) :-
>      BitsPerInt = bits_per_int(N),
> +    Y = uint.cast_to_int(UY),
>      Y >= 0,
>      Y < N,
> -    left_shift(BitsPerInt, X, Y, Z).
> +    to_int_in_range(BitsPerInt, integer(X) << min(Y, N), Z).
>  

And here.

Otherwise, that looks fine.

Peter


More information about the reviews mailing list