Permalink
Browse files

Merge pull request #2 from sokra/master

add buffer_ieee754
  • Loading branch information...
2 parents 27ce0ba + a6abbc7 commit 42c328d168a99614b0b195a5f6327dbb9993f1cc @toots committed Jun 29, 2012
Showing with 88 additions and 4 deletions.
  1. +84 −0 buffer_ieee754.js
  2. +4 −4 index.js
View
@@ -0,0 +1,84 @@
+exports.readIEEE754 = function(buffer, offset, isBE, mLen, nBytes) {
+ var e, m,
+ eLen = nBytes * 8 - mLen - 1,
+ eMax = (1 << eLen) - 1,
+ eBias = eMax >> 1,
+ nBits = -7,
+ i = isBE ? 0 : (nBytes - 1),
+ d = isBE ? 1 : -1,
+ s = buffer[offset + i];
+
+ i += d;
+
+ e = s & ((1 << (-nBits)) - 1);
+ s >>= (-nBits);
+ nBits += eLen;
+ for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
+
+ m = e & ((1 << (-nBits)) - 1);
+ e >>= (-nBits);
+ nBits += mLen;
+ for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
+
+ if (e === 0) {
+ e = 1 - eBias;
+ } else if (e === eMax) {
+ return m ? NaN : ((s ? -1 : 1) * Infinity);
+ } else {
+ m = m + Math.pow(2, mLen);
+ e = e - eBias;
+ }
+ return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
+};
+
+exports.writeIEEE754 = function(buffer, value, offset, isBE, mLen, nBytes) {
+ var e, m, c,
+ eLen = nBytes * 8 - mLen - 1,
+ eMax = (1 << eLen) - 1,
+ eBias = eMax >> 1,
+ rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0),
+ i = isBE ? (nBytes - 1) : 0,
+ d = isBE ? -1 : 1,
+ s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
+
+ value = Math.abs(value);
+
+ if (isNaN(value) || value === Infinity) {
+ m = isNaN(value) ? 1 : 0;
+ e = eMax;
+ } else {
+ e = Math.floor(Math.log(value) / Math.LN2);
+ if (value * (c = Math.pow(2, -e)) < 1) {
+ e--;
+ c *= 2;
+ }
+ if (e + eBias >= 1) {
+ value += rt / c;
+ } else {
+ value += rt * Math.pow(2, 1 - eBias);
+ }
+ if (value * c >= 2) {
+ e++;
+ c /= 2;
+ }
+
+ if (e + eBias >= eMax) {
+ m = 0;
+ e = eMax;
+ } else if (e + eBias >= 1) {
+ m = (value * c - 1) * Math.pow(2, mLen);
+ e = e + eBias;
+ } else {
+ m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
+ e = 0;
+ }
+ }
+
+ for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8);
+
+ e = (e << mLen) | m;
+ eLen += mLen;
+ for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8);
+
+ buffer[offset + i - d] |= s * 128;
+};
View
@@ -860,7 +860,7 @@ function readFloat(buffer, offset, isBigEndian, noAssert) {
'Trying to read beyond buffer length');
}
- return require('buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
+ return require('./buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
23, 4);
}
@@ -881,7 +881,7 @@ function readDouble(buffer, offset, isBigEndian, noAssert) {
'Trying to read beyond buffer length');
}
- return require('buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
+ return require('./buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
52, 8);
}
@@ -1171,7 +1171,7 @@ function writeFloat(buffer, value, offset, isBigEndian, noAssert) {
verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38);
}
- require('buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
+ require('./buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
23, 4);
}
@@ -1200,7 +1200,7 @@ function writeDouble(buffer, value, offset, isBigEndian, noAssert) {
verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308);
}
- require('buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
+ require('./buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
52, 8);
}

0 comments on commit 42c328d

Please sign in to comment.