• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Community
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
    • Interfacing-tools
    • Hardware-verification
      • Gl
      • Esim
      • Vl2014
        • Warnings
        • Primitives
        • Use-set
        • Syntax
        • Getting-started
        • Utilities
        • Loader
        • Transforms
          • Expression-sizing
            • Expression-sizing-minutia
            • Expression-sizing-intro
            • Vl-keyvalue-pattern-collect-array-replacements
            • Vl-assignpattern-positional-replacement
            • Vl-plainarg-exprsize
            • Vl-assignpattern-keyvalue-replacement
            • Vl-keyvalue-pattern-collect-struct-replacements
            • Vl-modulelist-exprsize
            • Vl-expr-assignpattern-extend/truncate
            • Vl-structmemberlist->types
            • Vl-expr-size
              • Vl-expr-selfsize
              • Vl-expr-typedecide
              • Vl-expr-expandsizes
                • Vl-weirdint-atom-expandsizes
                • Vl-constint-atom-expandsizes
                • Vl-hidexpr-expandsizes
                • Vl-expandsizes-zeroextend
                • Vl-atom-expandsizes
                • Vl-string-atom-expandsizes
                • Vl-sign-extend-constint
                • Vl-exprlist-expandsizes
                • Vl-exprlist-size
              • Vl-expr-selfdetermine-type
              • Vl-assignpattern-multi-replacement
              • Vl-parse-keyval-pattern-struct
              • Vl-plainarglist-exprsize
              • Vl-parse-keyval-pattern-array
              • Vl-warn-about-implicit-extension
              • Vl-assigncontext-size
              • Vl-arguments-exprsize
              • Vl-assignpattern-replacement
              • Vl-packeddimensionlist-exprsize
              • Vl-namedparamvaluelist-exprsize
              • Vl-maybe-delayoreventcontrol-exprsize
              • Vl-repeateventcontrol-exprsize
              • Vl-paramvaluelist-exprsize
              • Vl-maybe-packeddimension-exprsize
              • Vl-delayoreventcontrol-exprsize
              • Vl-namedparamvalue-exprsize
              • Vl-namedarglist-exprsize
              • Vl-enumitemlist-exprsize
              • Vl-packeddimension-exprsize
              • Vl-maybe-paramvalue-exprsize
              • Vl-evatomlist-exprsize
              • Vl-atom-selfdetermine-type
              • Vl-rangelist-exprsize
              • Vl-maybe-gatedelay-exprsize
              • Vl-maybe-datatype-exprsize
              • Vl-gatedelay-exprsize
              • Vl-paramargs-exprsize
              • Vl-namedarg-exprsize
              • Vl-eventcontrol-exprsize
              • Vl-enumbasetype-exprsize
              • Vl-delaycontrol-exprsize
              • Vl-paramvalue-exprsize
              • Vl-maybe-range-exprsize
              • Vl-enumitem-exprsize
              • Vl-range-exprsize
              • Vl-paramdecl-exprsize
              • Vl-paramdecllist-exprsize
              • Vl-maybe-expr-size
              • Vl-evatom-exprsize
              • Vl-vardecllist-exprsize
              • Vl-portdecllist-exprsize
              • Vl-modinstlist-exprsize
              • Vl-initiallist-exprsize
              • Vl-gateinstlist-exprsize
              • Vl-fundecllist-exprsize
              • Vl-assign-exprsize
              • Vl-interfaceport-exprsize
              • Vl-assignlist-exprsize
              • Vl-alwayslist-exprsize
              • Vl-portlist-exprsize
              • Vl-modinst-exprsize
              • Vl-gateinst-exprsize
              • Vl-fundecl-exprsize
              • Vl-vardecl-exprsize
              • Vl-regularport-exprsize
              • Vl-portdecl-exprsize
              • Vl-initial-exprsize
              • Vl-always-exprsize
              • Vl-port-exprsize
              • Vl-lvalue-type
              • Vl-classify-extension-warning-hook
              • Welltyped
              • Vl-castexpr->datatype
              • Vl-expr-size-assigncontext
              • Vl-type-expr-pairs-sum-datatype-sizes
              • Vl-basictype->datatype
              • Vl-expr-replace-assignpatterns
              • Vl-design-exprsize
              • Vl-op-simple-vector-p
              • Vl-expr-val-alist-max-count
              • Vl-expr-has-patterns
              • Vl-exprlist-max-count
              • Vl-unsigned-when-size-zero-lst
              • Vl-type-expr-pairs
              • Append-n
              • Vl-expr-val-alist
              • Vl-datatypelist
            • Occform
            • Oprewrite
            • Expand-functions
            • Delayredux
            • Unparameterization
            • Caseelim
            • Split
            • Selresolve
            • Weirdint-elim
            • Vl-delta
            • Replicate-insts
            • Rangeresolve
            • Propagate
            • Clean-selects
            • Clean-params
            • Blankargs
            • Inline-mods
            • Expr-simp
            • Trunc
            • Always-top
            • Gatesplit
            • Gate-elim
            • Expression-optimization
            • Elim-supplies
            • Wildelim
            • Drop-blankports
            • Clean-warnings
            • Addinstnames
            • Custom-transform-hooks
            • Annotate
            • Latchcode
            • Elim-unused-vars
            • Problem-modules
          • Lint
          • Mlib
          • Server
          • Kit
          • Printer
          • Esim-vl
          • Well-formedness
        • Sv
        • Fgl
        • Vwsim
        • Vl
        • X86isa
        • Svl
        • Rtl
      • Software-verification
      • Math
      • Testing-utilities
    • Vl-expr-expandsizes

    Vl-sign-extend-constint

    (vl-sign-extend-constint value origwidth finalwidth) returns a new value, which is the sign extension of the origwidth-bit value to finalwidth bits.

    Signature
    (vl-sign-extend-constint value origwidth finalwidth) → *
    Arguments
    value — Guard (natp value).
    origwidth — Guard (posp origwidth).
    finalwidth — Guard (posp finalwidth).

    When the MSB is true we need to add the appropriate number of 1 bits. There are probably any number of ways to do this. My method is relatively simple:

                            |---- finalwidth -------------|
                                          |-- origwidth --|
                  value  == 0000...0000   1bb...bbbbbbbbbbb
      (logior)    mask   == 1111...1111   000...00000000000
    ----------------------------------------------------------
                  result == 1111...1111   1bb...bbbbbbbbbbb

    Definitions and Theorems

    Function: vl-sign-extend-constint

    (defun vl-sign-extend-constint (value origwidth finalwidth)
      (declare (xargs :guard (and (natp value)
                                  (posp origwidth)
                                  (posp finalwidth))))
      (declare (xargs :guard (and (< value (expt 2 origwidth))
                                  (< origwidth finalwidth))))
      (let ((__function__ 'vl-sign-extend-constint))
        (declare (ignorable __function__))
        (b* ((msb (logbitp (1- origwidth) value))
             ((unless msb) value)
             (finalwidth-many-ones (- (expt 2 finalwidth) 1))
             (origwidth-many-ones (- (expt 2 origwidth) 1))
             (mask (logxor finalwidth-many-ones
                           origwidth-many-ones))
             (result (logior mask value)))
          result)))

    Theorem: natp-of-vl-sign-extend-constint

    (defthm natp-of-vl-sign-extend-constint
      (implies
           (and (force (natp value))
                (force (posp origwidth))
                (syntaxp (or (not (quotep origwidth))
                             (< (unquote origwidth) 10000)))
                (force (< value (expt 2 origwidth)))
                (force (posp finalwidth))
                (force (< origwidth finalwidth)))
           (natp (vl-sign-extend-constint value origwidth finalwidth)))
      :rule-classes :type-prescription)

    Theorem: upper-bound-of-vl-sign-extend-constint

    (defthm upper-bound-of-vl-sign-extend-constint
      (implies (and (force (natp value))
                    (force (posp origwidth))
                    (syntaxp (or (not (quotep origwidth))
                                 (< (unquote origwidth) 10000)))
                    (force (< value (expt 2 origwidth)))
                    (force (posp finalwidth))
                    (force (< origwidth finalwidth)))
               (< (vl-sign-extend-constint value origwidth finalwidth)
                  (expt 2 finalwidth)))
      :rule-classes ((:rewrite) (:linear)))