Changeset 9f17654


Ignore:
Timestamp:
May 18, 2004 6:33:37 PM (17 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
7243963
Parents:
1a65d70
git-author:
jrandom <jrandom> (05/18/04 18:33:37)
git-committer:
zzz <zzz@…> (05/18/04 18:33:37)
Message:

tuned to avoid expensive biginteger operation (well, not "expensive", but a lot more expensive than a few shifts)
when we need to debug, essentially assert the validity of the new tuned op, losing the performance benefit by duplicating the effort (to verify)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/data/UnsignedInteger.java

    r1a65d70 r9f17654  
    7575            return 0;
    7676        } else if (data.length == 0) { return 0; }
    77         BigInteger bi = new BigInteger(1, data);
    78         return bi.longValue();
     77        long rv = 0;
     78        for (int i = 0; i < data.length; i++) {
     79            long cur = (long)(data[i]&0xFF);
     80            if (cur < 0) cur = cur+256;
     81            cur = (cur << (8*(data.length-i-1)));
     82            rv += cur;
     83        }
     84        // only fire up this expensive assert if we're worried about it
     85        if (_log.shouldLog(Log.DEBUG)) {
     86            BigInteger bi = new BigInteger(1, data);
     87            long biVal = bi.longValue();
     88            if (biVal != rv) {
     89                _log.log(Log.CRIT, "ERR: " + bi.toString(2) + " /\t" + bi.toString(16) + " /\t" + bi.toString()
     90                                   + " != \n     " + Long.toBinaryString(rv) + " /\t" + Long.toHexString(rv)
     91                                   + " /\t" + rv);
     92                for (int i = 0; i < data.length; i++) {
     93                    long cur = (long)(data[i]&0xFF);
     94                    if (cur < 0) cur = cur+256;
     95                    long shiftBy = (8*(data.length-i-1));
     96                    long old = cur;
     97                    cur = (cur << shiftBy);
     98                    _log.log(Log.CRIT, "cur["+ i+"]=" + Long.toHexString(cur) + " data = "
     99                                       + Long.toHexString((data[i]&0xFF)) + " shiftBy: " + shiftBy
     100                                       + " old: " + Long.toHexString(old));
     101                }
     102                throw new RuntimeException("b0rked on " + bi.toString() + " / " + rv);
     103            }
     104        }
     105        return rv;
     106       
    79107    }
    80108
     
    197225
    198226    public static void main(String args[]) {
     227        try {
    199228        _log.debug("Testing 1024");
    200229        testNum(1024L);
     
    211240        _log.debug("Testing MaxLong");
    212241        testNum(Long.MAX_VALUE);
     242        } catch (Throwable t) { t.printStackTrace(); }
    213243        try {
    214244            Thread.sleep(1000);
Note: See TracChangeset for help on using the changeset viewer.