8889841cREADME.md000066600000004122150515305110006022 0ustar00# Parser function for floating point hexadecimals [![license](https://img.shields.io/github/license/maurobringolf/@webassemblyjs/floating-point-hex-parser.svg)]() [![GitHub last commit](https://img.shields.io/github/last-commit/maurobringolf/@webassemblyjs/floating-point-hex-parser.svg)]() [![npm](https://img.shields.io/npm/v/@webassemblyjs/floating-point-hex-parser.svg)]() > A JavaScript function to parse floating point hexadecimals as defined by the [WebAssembly specification](https://webassembly.github.io/spec/core/text/values.html#text-hexfloat). ## Usage ```javascript import parseHexFloat from '@webassemblyjs/floating-point-hex-parser' parseHexFloat('0x1p-1') // 0.5 parseHexFloat('0x1.921fb54442d18p+2') // 6.283185307179586 ``` ## Tests This module is tested in two ways. The first one is through a small set of test cases that can be found in [test/regular.test.js](https://github.com/maurobringolf/@webassemblyjs/floating-point-hex-parser/blob/master/test/regular.test.js). The second one is non-deterministic (sometimes called *fuzzing*): 1. Generate a random IEEE754 double precision value `x`. 1. Compute its representation `y` in floating point hexadecimal format using the C standard library function `printf` since C supports this format. 1. Give both values to JS testcase and see if `parseHexFloat(y) === x`. By default one `npm test` run tests 100 random samples. If you want to do more, you can set the environment variable `FUZZ_AMOUNT` to whatever number of runs you'd like. Because it uses one child process for each sample, it is really slow though. For more details about the randomized tests see [the source](https://github.com/maurobringolf/@webassemblyjs/floating-point-hex-parser/tree/master/test/fuzzing). ## Links * [maurobringolf.ch/2017/12/hexadecimal-floating-point-notation/](https://maurobringolf.ch/2017/12/hexadecimal-floating-point-notation/) * [github.com/xtuc/js-webassembly-interpreter/issues/32](https://github.com/xtuc/js-webassembly-interpreter/issues/32) * [github.com/WebAssembly/design/issues/292](https://github.com/WebAssembly/design/issues/292) esm/index.js000066600000002227150515305110007000 0ustar00export default function parse(input) { input = input.toUpperCase(); var splitIndex = input.indexOf("P"); var mantissa, exponent; if (splitIndex !== -1) { mantissa = input.substring(0, splitIndex); exponent = parseInt(input.substring(splitIndex + 1)); } else { mantissa = input; exponent = 0; } var dotIndex = mantissa.indexOf("."); if (dotIndex !== -1) { var integerPart = parseInt(mantissa.substring(0, dotIndex), 16); var sign = Math.sign(integerPart); integerPart = sign * integerPart; var fractionLength = mantissa.length - dotIndex - 1; var fractionalPart = parseInt(mantissa.substring(dotIndex + 1), 16); var fraction = fractionLength > 0 ? fractionalPart / Math.pow(16, fractionLength) : 0; if (sign === 0) { if (fraction === 0) { mantissa = sign; } else { if (Object.is(sign, -0)) { mantissa = -fraction; } else { mantissa = fraction; } } } else { mantissa = sign * (integerPart + fraction); } } else { mantissa = parseInt(mantissa, 16); } return mantissa * (splitIndex !== -1 ? Math.pow(2, exponent) : 1); }lib/index.js000066600000002362150515305110006762 0ustar00"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = parse; function parse(input) { input = input.toUpperCase(); var splitIndex = input.indexOf("P"); var mantissa, exponent; if (splitIndex !== -1) { mantissa = input.substring(0, splitIndex); exponent = parseInt(input.substring(splitIndex + 1)); } else { mantissa = input; exponent = 0; } var dotIndex = mantissa.indexOf("."); if (dotIndex !== -1) { var integerPart = parseInt(mantissa.substring(0, dotIndex), 16); var sign = Math.sign(integerPart); integerPart = sign * integerPart; var fractionLength = mantissa.length - dotIndex - 1; var fractionalPart = parseInt(mantissa.substring(dotIndex + 1), 16); var fraction = fractionLength > 0 ? fractionalPart / Math.pow(16, fractionLength) : 0; if (sign === 0) { if (fraction === 0) { mantissa = sign; } else { if (Object.is(sign, -0)) { mantissa = -fraction; } else { mantissa = fraction; } } } else { mantissa = sign * (integerPart + fraction); } } else { mantissa = parseInt(mantissa, 16); } return mantissa * (splitIndex !== -1 ? Math.pow(2, exponent) : 1); }LICENSE000066600000002102150515305110005544 0ustar00MIT License Copyright (c) 2017 Mauro Bringolf Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.package.json000066600000001305150515305110007031 0ustar00{ "name": "@webassemblyjs/floating-point-hex-parser", "scripts": { "build-fuzzer": "[ -f ./test/fuzzing/parse.out ] || gcc ./test/fuzzing/parse.c -o ./test/fuzzing/parse.out -lm -Wall" }, "repository": { "type": "git", "url": "https://github.com/xtuc/webassemblyjs.git" }, "publishConfig": { "access": "public" }, "version": "1.11.1", "description": "A function to parse floating point hexadecimal strings as defined by the WebAssembly specification", "main": "lib/index.js", "module": "esm/index.js", "keywords": [ "webassembly", "floating-point" ], "author": "Mauro Bringolf", "license": "MIT", "gitHead": "3f07e2db2031afe0ce686630418c542938c1674b" }