aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@webassemblyjs/floating-point-hex-parser
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/@webassemblyjs/floating-point-hex-parser')
-rw-r--r--node_modules/@webassemblyjs/floating-point-hex-parser/LICENSE21
-rw-r--r--node_modules/@webassemblyjs/floating-point-hex-parser/README.md34
-rw-r--r--node_modules/@webassemblyjs/floating-point-hex-parser/esm/index.js42
-rw-r--r--node_modules/@webassemblyjs/floating-point-hex-parser/lib/index.js49
-rw-r--r--node_modules/@webassemblyjs/floating-point-hex-parser/package.json24
5 files changed, 170 insertions, 0 deletions
diff --git a/node_modules/@webassemblyjs/floating-point-hex-parser/LICENSE b/node_modules/@webassemblyjs/floating-point-hex-parser/LICENSE
new file mode 100644
index 00000000..d5471f86
--- /dev/null
+++ b/node_modules/@webassemblyjs/floating-point-hex-parser/LICENSE
@@ -0,0 +1,21 @@
+MIT 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. \ No newline at end of file
diff --git a/node_modules/@webassemblyjs/floating-point-hex-parser/README.md b/node_modules/@webassemblyjs/floating-point-hex-parser/README.md
new file mode 100644
index 00000000..648e09bc
--- /dev/null
+++ b/node_modules/@webassemblyjs/floating-point-hex-parser/README.md
@@ -0,0 +1,34 @@
+# 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)
diff --git a/node_modules/@webassemblyjs/floating-point-hex-parser/esm/index.js b/node_modules/@webassemblyjs/floating-point-hex-parser/esm/index.js
new file mode 100644
index 00000000..d8d858d3
--- /dev/null
+++ b/node_modules/@webassemblyjs/floating-point-hex-parser/esm/index.js
@@ -0,0 +1,42 @@
+export 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);
+} \ No newline at end of file
diff --git a/node_modules/@webassemblyjs/floating-point-hex-parser/lib/index.js b/node_modules/@webassemblyjs/floating-point-hex-parser/lib/index.js
new file mode 100644
index 00000000..a8676992
--- /dev/null
+++ b/node_modules/@webassemblyjs/floating-point-hex-parser/lib/index.js
@@ -0,0 +1,49 @@
+"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);
+} \ No newline at end of file
diff --git a/node_modules/@webassemblyjs/floating-point-hex-parser/package.json b/node_modules/@webassemblyjs/floating-point-hex-parser/package.json
new file mode 100644
index 00000000..2f2ee38f
--- /dev/null
+++ b/node_modules/@webassemblyjs/floating-point-hex-parser/package.json
@@ -0,0 +1,24 @@
+{
+ "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.7.11",
+ "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": "4291990bfc4648bc6676091a955d12dc3c7e5909"
+}