Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add saturating word conversions and word duplication #118

Merged
merged 2 commits into from
Nov 4, 2024

Conversation

jargh
Copy link
Contributor

@jargh jargh commented Nov 4, 2024

No description provided.

jargh and others added 2 commits November 4, 2024 12:09
Added both unsigned and signed *saturating* conversions from N and Z to
machine words, which rather than wrapping modulo 2^wordsize as the main
"word" and "iword" do, saturate at the extremal values of that size:

  word_saturate =
   |- word_saturate n =
      if n > val word_UINT_MAX then word_UINT_MAX else word n

  iword_saturate =
    |- iword_saturate x =
       if x < ival word_INT_MIN then word_INT_MIN
       else if x > ival word_INT_MAX then word_INT_MAX
       else iword x

Also added a natural "word_duplicate" operation, a kind of iterated
word_join inferring relative sizes and hence number of duplicates from
the types.

  word_duplicate =
    |- (word_duplicate:M word->N word) x =
       word_of_bits {i | bit (i MOD dimindex (:M)) x

There are also a few elementary lemmas about it:

  BIT_WORD_DUPLICATE =
    |- !x i.
           bit i (word_duplicate x) <=>
           i < dimindex(:N) /\ bit (i MOD dimindex(:M)) x

  VAL_WORD_DUPLICATE =
    |- !x k.
           dimindex(:N) <= k * dimindex(:M)
           ==> val (word_duplicate x) =
               ((2 EXP (k * dimindex(:M)) - 1) DIV (2 EXP dimindex(:M) - 1) *
                val x) MOD
               2 EXP dimindex(:N)

  WORD_DUPLICATE =
    |- !m k.
           dimindex(:N) <= k * dimindex(:M)
           ==> word_duplicate (word m) =
               word
               ((2 EXP (k * dimindex(:M)) - 1) DIV (2 EXP dimindex(:M) - 1) *
                m MOD 2 EXP dimindex(:M))

  WORD_DUPLICATE_DOUBLE =
    |- !x. word_duplicate (word_join x x) = word_duplicate x

  WORD_DUPLICATE_REFL =
    |- !x. word_duplicate x = x

  WORD_SUBWORD_DUPLICATE =
    |- !x pos len.
           pos MOD dimindex(:M) + len <= dimindex(:M) /\
           pos + len <= dimindex(:N)
           ==> word_subword (word_duplicate x) (pos,len) =
               word_subword x (pos MOD dimindex(:M),len)

The relevant conversions like BIT_WORD_CONV, SIMPLE_WORD_SUBWORD_CONV
and WORD_RED_CONV have all been updated to take these three new operations
into account.
@jrh13 jrh13 merged commit c39dbfd into jrh13:master Nov 4, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants