Permalink
Browse files

add back sjcl.bitArray.extract - needed for bn

  • Loading branch information...
sqs committed Apr 21, 2011
1 parent 8615cb3 commit 70598ac8d55de4ebf90e9f75e41e60e8cba9f0e2
Showing with 21 additions and 0 deletions.
  1. +21 −0 core/bitArray.js
View
@@ -42,6 +42,27 @@ sjcl.bitArray = {
return (bend === undefined) ? a : sjcl.bitArray.clamp(a, bend-bstart);
},
+ /**
+ * Extract a number packed into a bit array.
+ * @param {bitArray} a The array to slice.
+ * @param {Number} bstart The offset to the start of the slice, in bits.
+ * @param {Number} length The length of the number to extract.
+ * @return {Number} The requested slice.
+ */
+ extract: function(a, bstart, blength) {
+ // FIXME: this Math.floor is not necessary at all, but for some reason
+ // seems to suppress a bug in the Chromium JIT.
+ var x, sh = Math.floor((-bstart-blength) & 31);
+ if ((bstart + blength - 1 ^ bstart) & -32) {
+ // it crosses a boundary
+ x = (a[bstart/32|0] << (32 - sh)) ^ (a[bstart/32+1|0] >>> sh);
+ } else {
+ // within a single word
+ x = a[bstart/32|0] >>> sh;
+ }
+ return x & ((1<<blength) - 1);
+ },
+
/**
* Concatenate two bit arrays.
* @param {bitArray} a1 The first array.

0 comments on commit 70598ac

Please sign in to comment.