#17526: Bitset doctest failures on OS X
-------------------------------------+-------------------------------------
       Reporter:  jdemeyer           |        Owner:
           Type:  defect             |       Status:  needs_review
       Priority:  blocker            |    Milestone:  sage-6.5
      Component:  misc               |   Resolution:
       Keywords:                     |    Merged in:
        Authors:  Jeroen Demeyer     |    Reviewers:
Report Upstream:  N/A                |  Work issues:
         Branch:                     |       Commit:
  u/jdemeyer/ticket/17526            |  0d1e1497735026f12f1cb06c5ef919f462d0bd23
   Dependencies:                     |     Stopgaps:
-------------------------------------+-------------------------------------

Comment (by SimonKing):

 Let me try to put some semantic on the existing code (i.e., without your
 patch).

 We shift by `n` bits. The variable `nlimbs` denotes the number of limbs
 that are fully discarded by the shift.
 Expl: If a limb comprises 32 bit and we shift by 37 bits, then `nlimbs` is
 1.

 The variable `shifted_limbs` denotes the number of limbs of `a` that carry
 data to-be-put into `r` (provided that `r` is large enough).
 Expl: If `a` comprises 5 limbs in the example above, then we have
 `shifted_limbs=4`.

 The variable `nbits` is the remainder of `n` by the limb size, hence,
 `nbits=5` in our example.

 If `r` is large enough to contain `shifted_limbs` limbs, then we can just
 shift (if `nbits>0`) resp. copy (if `nbits==0`). Otherwise, we can only
 shift `r.limbs` limbs.

 If in our example `r.limbs==3<shifted_limbs==4`, then we would like to
 omit limb number 0 of `a`, and then take limbs 1, 2, 3 of `a`, shift it by
 5 bits, and put the result into limbs 0,1 and 2 of `r`. In addition, we
 want that the first 5 bits of limb 4 of `a` shall appear as the 5 highest
 bits of limb number 2 of `r`.

 However, if `r.limbs>=4==shifted_limbs`, then it is enough to shift limbs
 1,2,3,4 of `a`, shift by 5 bits, and put the result into the limbs 0,1,2,3
 of `r`. There are no additional 5 bits to be put somewhere. However, it is
 needed to clear the top bits of `r`: It could be that `r` does not use all
 bits of its limb number 3, and hence there might be some bits obtained
 from `a` that should be discarded.

 Hence, I believe the following solution would be easier:
 {{{
 #!python
     if shifted_limbs <= r.limbs: # here I changed "<" to "<="
         if nbits:
             mpn_rshift(r.bits, a.bits + nlimbs, shifted_limbs, nbits)
         else:
             mpn_copyi(r.bits, a.bits + nlimbs, shifted_limbs)

         # Clear top limbs (note that r.limbs - shifted_limbs >= 1)
         mpn_zero(r.bits + (r.limbs - nlimbs), r.limbs - shifted_limbs)
     else:
         # Number of limbs to shift is r.limbs
         if nbits:
             mpn_rshift(r.bits, a.bits + nlimbs, r.limbs, nbits)
             # Add the additional bits from top limb of a
             r.bits[r.limbs-1] |= a.bits[r.limbs+nlimbs] << (GMP_LIMB_BITS
 - nbits)
         else:
             mpn_copyi(r.bits, a.bits + nlimbs, r.limbs)

         # Clear bits outside bitset in top limb
         bitset_fix(r)
 }}}

--
Ticket URL: <http://trac.sagemath.org/ticket/17526#comment:5>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.

Reply via email to