Menu

#10 Negative slicing for intbv

Development
wont-fix
None
5
2013-09-15
2013-08-25
No

I think it would be useful to be able to do negative slicing in the same way as python handles it in intbv. This could simply be implemented by asserting that intbv has a defined number of bits, and then converting into the positive values.

For example:
a = intbv(5)[10]
get the upper bit:
a[-1]
cut off the upper bit:
a[-1:]
get the top 5 bits:
a[:-5]

1 Attachments

Discussion

  • Benjamin Berg

    Benjamin Berg - 2013-08-25

    And a bit of code that tests all the cases

     
  • David

    David - 2013-09-11

    Oh, this would've been fantastic short-hand in my current design! (This is a simple "me too" shout out.)

    Here's an example of splitting off the top 3 bits such as for address decoding:

    addr_hi.next = addr[:-3]
    addr_lo.next = addr[-3:]
    

    Right now, I resort to:

    N = len(addr)
    ...
    addr_hi.next = addr[:N-3]
    addr_lo.next = addr[N-3:]
    

    (Yes, it is just syntactic sugar... But I like sweets if they don't impair my health too badly. Though given the current mailing list discussion on fixbv, that fixbv type might need a way to alter such negative indexing such as imposing limits. --- but hopefully, that would only be a limit of fixbv.)

    Hmm ---- Or at risk of appearing to be anti-pythonic, MATLAB and Octave employ a keyword "end" for their array-indexing purposes. If "end" isn't a reserved word, perhaps an implementation could be:

    from myhdl import end, ...
    ...
    addr_hi.next = addr[:end-3]
    addr_lo.next = addr[end-3:]
    

    If "end" behaves as a synonym for len(addr), with myhdl indexing this may imply implies that end-1 is the most significant bit (requiring that the # bits in the intbv is specified), and

    addr[end-1] == addr[end:end-1] == addr[:end-1]
    

    (or perhaps instead of "end", define "msb" and "lsb" which would be more general for use with types such as fixbv? -- though "msb" may need to be defined as "end-1" so that addr[msb] would indeed return the most significant bit of addr... and then things might feel "less pythonic" after that. hmm...)

    • just 2 cents. :)
      [enjoying the myhdl.]
     
  • Jan Decaluwe

    Jan Decaluwe - 2013-09-15
    • status: open --> wont-fix
    • assigned_to: Jan Decaluwe
     
  • Jan Decaluwe

    Jan Decaluwe - 2013-09-15

    Too much potential confusion when regarding intbv as a fixbv subtype, where negative indices presumably have a totally different meaning. Note that intbv is already quite different from Python sequences in that the direction is reversed.
    Moreover, the "workaround" is straightforward, and in my opinion not even a workaround but a clearer, more explicit way to express the intention.

     

    Last edit: Jan Decaluwe 2013-09-15

Log in to post a comment.