1 // Copyright (c) 2008, Fair Oaks Labs, Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
14 // * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors
15 // may be used to endorse or promote products derived from this software
16 // without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
31 // Modifications to writeIEEE754 to support negative zeroes made by Brian White
33 var readIEEE754 = function(buffer, offset, endian, mLen, nBytes) {
35 bBE = (endian === 'big'),
36 eLen = nBytes * 8 - mLen - 1,
37 eMax = (1 << eLen) - 1,
40 i = bBE ? 0 : (nBytes - 1),
42 s = buffer[offset + i];
46 e = s & ((1 << (-nBits)) - 1);
49 for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
51 m = e & ((1 << (-nBits)) - 1);
54 for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
58 } else if (e === eMax) {
59 return m ? NaN : ((s ? -1 : 1) * Infinity);
61 m = m + Math.pow(2, mLen);
64 return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
67 var writeIEEE754 = function(buffer, value, offset, endian, mLen, nBytes) {
69 bBE = (endian === 'big'),
70 eLen = nBytes * 8 - mLen - 1,
71 eMax = (1 << eLen) - 1,
73 rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0),
74 i = bBE ? (nBytes-1) : 0,
76 s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
78 value = Math.abs(value);
80 if (isNaN(value) || value === Infinity) {
81 m = isNaN(value) ? 1 : 0;
84 e = Math.floor(Math.log(value) / Math.LN2);
85 if (value * (c = Math.pow(2, -e)) < 1) {
92 value += rt * Math.pow(2, 1 - eBias);
99 if (e + eBias >= eMax) {
102 } else if (e + eBias >= 1) {
103 m = (value * c - 1) * Math.pow(2, mLen);
106 m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
111 for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8);
115 for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8);
117 buffer[offset + i - d] |= s * 128;
120 exports.readIEEE754 = readIEEE754;
121 exports.writeIEEE754 = writeIEEE754;