Changeset 2996


Ignore:
Timestamp:
Apr 3, 2013, 6:38:32 PM (5 years ago)
Author:
cameron
Message:

Process bits in little-endian order; add bit_pattern_expr

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/charsetcompiler/charset_compiler.py

    r2994 r2996  
    246246    return options.basis_pattern % n
    247247def make_bitv(n):
    248     return Var(bit_var(n))
     248    return Var(bit_var(7-n))
    249249
    250250
     
    255255    for i in range(0, bit_count):
    256256        if (pattern & test_bit) == 0:
    257             bit_terms.append(make_not(make_bitv(i)))
    258         else: bit_terms.append(make_bitv(i))
     257            bit_terms.append(make_not(make_bitv(7-i)))
     258        else: bit_terms.append(make_bitv(7-i))
    259259        test_bit >>= 1
    260260    while len(bit_terms) > 1:
     
    267267    return bit_terms[0]
    268268
     269def bit_pattern_expr(pattern, selected_bits):
     270    if selected_bits == 0: return TrueLiteral()
     271    bit_terms = []
     272    bit_no = 0
     273    while selected_bits:
     274      test_bit = 1 << bit_no
     275      if selected_bits & test_bit:
     276        if (pattern & test_bit) == 0:
     277            bit_terms = [make_not(make_bitv(bit_no))] + bit_terms
     278        else: bit_terms = [make_bitv(bit_no)] + bit_terms
     279      else: bit_terms = [TrueLiteral()] + bit_terms
     280      # Appending TrueLiteral() for nonselected bits is intended
     281      # to keep consistent grouping of variables in the next loop.
     282      selected_bits &= ~test_bit
     283      bit_no += 1
     284    while len(bit_terms) > 1:
     285        new_terms = []
     286        for i in range(0, len(bit_terms)/ 2):
     287            new_terms.append(make_and(bit_terms[2*i], bit_terms[2*i+1]))
     288        if len(bit_terms) % 2 == 1:
     289            new_terms.append(bit_terms[-1])
     290        bit_terms = new_terms
     291    return bit_terms[0]
     292   
    269293
    270294def char_test_expr(ch):
    271     return make_bit_test(ord(ch), 8)
    272     #return make_8bit_test(ord(ch))
     295    #return make_bit_test(ord(ch), 8)
     296    return bit_pattern_expr(ord(ch), 0xFF)
    273297
    274298def GE_Range(N, n):
    275299    if N == 0: return TrueLiteral()
    276300    elif N % 2 == 0 and (n >> (N - 2)) == 0:
    277         return make_or(make_or(make_bitv(8-N), make_bitv(9-N)),
     301        return make_or(make_or(make_bitv(N-1), make_bitv(N-2)),
    278302                        GE_Range(N - 2, n))
    279303    elif N % 2 == 0 and (n >> (N - 2)) == 3:   # >= 11xxxx
    280         return make_and(make_and(make_bitv(8-N), make_bitv(9-N)),
     304        return make_and(make_and(make_bitv(N-1), make_bitv(N-2)),
    281305                        GE_Range(N - 2, n - (3 << (N-2))))
    282306    elif N >= 1:
     
    288312            # is set in the target, the target will certainly be >=.  Otherwise,
    289313            # the value of GE_range(N-1, lo_bits) is required.
    290             return make_or(make_bitv(8-N), lo_range)
     314            return make_or(make_bitv(N-1), lo_range)
    291315        else:
    292316            # If the hi_bit of n is set, then the corresponding bit must be set
    293317            # in the target for >= and GE_range(N-1, lo_bits) must also be true.
    294             return make_and(make_bitv(8-N), lo_range)
     318            return make_and(make_bitv(N-1), lo_range)
    295319
    296320def LE_Range(N, n):
     
    314338        diff_bits >>= 1
    315339    if n2 < n1 or diff_count > 8: raise BadRange()
    316     mask = 2**(diff_count-1) - 1
    317     common = make_bit_test(n1 >> diff_count, 8 - diff_count)
     340    mask = 2**(diff_count) - 1
     341    #common = make_bit_test(n1 >> diff_count, 8 - diff_count)
     342    common = bit_pattern_expr(n1 & ~mask, 0xFF^mask)
    318343    if diff_count == 0: return common
    319344    mask = 2**(diff_count-1) - 1
    320345    lo_test = GE_Range(diff_count-1, n1 & mask)
    321346    hi_test = LE_Range(diff_count-1, n2 & mask)
    322     return make_and(common, make_sel(make_bitv(8-diff_count), hi_test, lo_test))
     347    return make_and(common, make_sel(make_bitv(diff_count-1), hi_test, lo_test))
    323348
    324349BadCharSetItem = Exception()
Note: See TracChangeset for help on using the changeset viewer.