@@ -2023,22 +2023,32 @@ export function quantizeToF16(num: number): number {
2023
2023
return hfround ( num ) ;
2024
2024
}
2025
2025
2026
- /** Statically allocate working data, so it doesn't need per-call creation */
2027
- const quantizeToI32Data = new Int32Array ( new ArrayBuffer ( 4 ) ) ;
2028
-
2029
- /** @returns the closest 32-bit signed integer value to the input */
2026
+ /**
2027
+ * @returns the closest 32-bit signed integer value to the input, rounding
2028
+ * towards 0, if not already an integer
2029
+ */
2030
2030
export function quantizeToI32 ( num : number ) : number {
2031
- quantizeToI32Data [ 0 ] = num ;
2032
- return quantizeToI32Data [ 0 ] ;
2031
+ if ( num >= kValue . i32 . positive . max ) {
2032
+ return kValue . i32 . positive . max ;
2033
+ }
2034
+ if ( num <= kValue . i32 . negative . min ) {
2035
+ return kValue . i32 . negative . min ;
2036
+ }
2037
+ return Math . trunc ( num ) ;
2033
2038
}
2034
2039
2035
- /** Statically allocate working data, so it doesn't need per-call creation */
2036
- const quantizeToU32Data = new Uint32Array ( new ArrayBuffer ( 4 ) ) ;
2037
-
2038
- /** @returns the closest 32-bit signed integer value to the input */
2040
+ /**
2041
+ * @returns the closest 32-bit unsigned integer value to the input, rounding
2042
+ * towards 0, if not already an integer
2043
+ */
2039
2044
export function quantizeToU32 ( num : number ) : number {
2040
- quantizeToU32Data [ 0 ] = num ;
2041
- return quantizeToU32Data [ 0 ] ;
2045
+ if ( num >= kValue . u32 . max ) {
2046
+ return kValue . u32 . max ;
2047
+ }
2048
+ if ( num <= 0 ) {
2049
+ return 0 ;
2050
+ }
2051
+ return Math . trunc ( num ) ;
2042
2052
}
2043
2053
2044
2054
/** @returns whether the number is an integer and a power of two */
0 commit comments