-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize: use
+
operator instead of ToNumber
function
- Loading branch information
1 parent
7dc9749
commit a98499e
Showing
5 changed files
with
12 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,20 @@ | ||
import { convertToNumber, roundToFloat16Bits } from "./_util/converter.mjs"; | ||
import { CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER } from "./_util/messages.mjs"; | ||
import { | ||
NativeNumber, | ||
NativeTypeError, | ||
NumberIsFinite, | ||
} from "./_util/primordials.mjs"; | ||
import { NumberIsFinite } from "./_util/primordials.mjs"; | ||
|
||
/** | ||
* returns the nearest half-precision float representation of a number | ||
* | ||
* @param {number} num | ||
* @param {number} x | ||
* @returns {number} | ||
*/ | ||
export function hfround(num) { | ||
if (typeof num === "bigint") { | ||
throw NativeTypeError(CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER); | ||
} | ||
|
||
num = NativeNumber(num); | ||
export function hfround(x) { | ||
const number = +x; | ||
|
||
// for optimization | ||
if (!NumberIsFinite(num) || num === 0) { | ||
return num; | ||
if (!NumberIsFinite(number) || number === 0) { | ||
return number; | ||
} | ||
|
||
const x16 = roundToFloat16Bits(num); | ||
const x16 = roundToFloat16Bits(number); | ||
return convertToNumber(x16); | ||
} |