Skip to content

Commit

Permalink
Remove duplicate definitions of reinterpret* (#3095)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinEng authored Oct 25, 2023
1 parent 42e6b6d commit 2be0e90
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 192 deletions.
7 changes: 2 additions & 5 deletions src/unittests/floating_point.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import { makeTestGroup } from '../common/framework/test_group.js';
import { objectEquals, unreachable } from '../common/util/util.js';
import { kValue } from '../webgpu/util/constants.js';
import { FP, FPInterval, FPIntervalParam, IntervalBounds } from '../webgpu/util/floating_point.js';
import { map2DArray, oneULPF32, oneULPF16, oneULPF64 } from '../webgpu/util/math.js';
import {
reinterpretU16AsF16,
reinterpretU32AsF32,
reinterpretU64AsF64,
map2DArray,
oneULPF32,
oneULPF16,
oneULPF64,
} from '../webgpu/util/math.js';
} from '../webgpu/util/reinterpret.js';

import { UnitTest } from './unit_test.js';

Expand Down
8 changes: 5 additions & 3 deletions src/unittests/maths.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import {
fullF16Range,
fullF32Range,
fullI32Range,
reinterpretU16AsF16,
reinterpretU32AsF32,
reinterpretU64AsF64,
lerp,
linearRange,
nextAfterF16,
Expand All @@ -40,6 +37,11 @@ import {
lerpBigInt,
linearRangeBigInt,
} from '../webgpu/util/math.js';
import {
reinterpretU16AsF16,
reinterpretU32AsF32,
reinterpretU64AsF64,
} from '../webgpu/util/reinterpret.js';

import { UnitTest } from './unit_test.js';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ import { GPUTest } from '../../../../../gpu_test.js';
import { Comparator, alwaysPass, anyOf } from '../../../../../util/compare.js';
import { kBit, kValue } from '../../../../../util/constants.js';
import {
reinterpretI32AsF32,
reinterpretI32AsU32,
reinterpretF32AsI32,
reinterpretF32AsU32,
reinterpretU32AsF32,
reinterpretU32AsI32,
reinterpretU16AsF16,
reinterpretF16AsU16,
f32,
i32,
u32,
Expand All @@ -59,6 +51,16 @@ import {
isFiniteF32,
isFiniteF16,
} from '../../../../../util/math.js';
import {
reinterpretI32AsF32,
reinterpretI32AsU32,
reinterpretF32AsI32,
reinterpretF32AsU32,
reinterpretU32AsF32,
reinterpretU32AsI32,
reinterpretU16AsF16,
reinterpretF16AsU16,
} from '../../../../../util/reinterpret.js';
import { makeCaseCache } from '../../case_cache.js';
import { allInputSources, run, ShaderBuilder } from '../../expression.js';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ import { GPUTest } from '../../../../gpu_test.js';
import { kValue } from '../../../../util/constants.js';
import { abstractFloat, TypeAbstractFloat, TypeF16, TypeF32 } from '../../../../util/conversion.js';
import { FP } from '../../../../util/floating_point.js';
import {
filteredF64Range,
fullF64Range,
isSubnormalNumberF64,
reinterpretU64AsF64,
} from '../../../../util/math.js';
import { filteredF64Range, fullF64Range, isSubnormalNumberF64 } from '../../../../util/math.js';
import { reinterpretU64AsF64 } from '../../../../util/reinterpret.js';
import { makeCaseCache } from '../case_cache.js';
import {
abstractFloatShaderBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
f32,
f16,
i32,
reinterpretU32AsI32,
TypeBool,
TypeF32,
TypeF16,
Expand All @@ -26,6 +25,7 @@ import {
quantizeToF32,
quantizeToF16,
} from '../../../../util/math.js';
import { reinterpretU32AsI32 } from '../../../../util/reinterpret.js';
import { makeCaseCache } from '../case_cache.js';
import { allInputSources, run, ShaderBuilder } from '../expression.js';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
f32,
f16,
i32,
reinterpretI32AsU32,
TypeBool,
TypeF32,
TypeF16,
Expand All @@ -26,6 +25,7 @@ import {
quantizeToF32,
quantizeToF16,
} from '../../../../util/math.js';
import { reinterpretI32AsU32 } from '../../../../util/reinterpret.js';
import { makeCaseCache } from '../case_cache.js';
import { allInputSources, run, ShaderBuilder } from '../expression.js';

Expand Down
50 changes: 6 additions & 44 deletions src/webgpu/util/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Float16Array } from '../../external/petamoriken/float16/float16.js';
import {
reinterpretU64AsF64,
reinterpretF64AsU64,
reinterpretU32AsF32,
reinterpretU16AsF16,
} from './reinterpret.js';

export const kBit = {
// Limits of int32
Expand Down Expand Up @@ -236,49 +241,6 @@ export const kBit = {
},
} as const;

/**
* @returns a 64-bit float value via interpreting the input as the bit
* representation as a 64-bit integer
*
* Using a locally defined function here to avoid compile time dependency
* issues.
*/
function reinterpretU64AsF64(input: bigint): number {
return new Float64Array(new BigUint64Array([input]).buffer)[0];
}

/**
* @returns the 64-bit integer bit representation of 64-bit float value
*
* Using a locally defined function here to avoid compile time dependency
* issues.
*/
function reinterpretF64AsU64(input: number): bigint {
return new BigUint64Array(new Float64Array([input]).buffer)[0];
}

/**
* @returns a 32-bit float value via interpreting the input as the bit
* representation as a 32-bit integer
*
* Using a locally defined function here to avoid compile time dependency
* issues.
*/
function reinterpretU32AsF32(input: number): number {
return new Float32Array(new Uint32Array([input]).buffer)[0];
}

/**
* @returns a 16-bit float value via interpreting the input as the bit
* representation as a 64-bit integer
*
* Using a locally defined function here to avoid compile time dependency
* issues.
*/
function reinterpretU16AsF16(input: number): number {
return new Float16Array(new Uint16Array([input]).buffer)[0];
}

export const kValue = {
// Limits of i32
i32: {
Expand Down
85 changes: 0 additions & 85 deletions src/webgpu/util/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1094,91 +1094,6 @@ export const True = bool(true);
/** A 'false' literal value */
export const False = bool(false);

// Encoding to u32s, instead of BigInt, for serialization
export function reinterpretF64AsU32s(f64: number): [number, number] {
workingDataF64[0] = f64;
return [workingDataU32[0], workingDataU32[1]];
}

// De-encoding from u32s, instead of BigInt, for serialization
export function reinterpretU32sAsF64(u32s: [number, number]): number {
workingDataU32[0] = u32s[0];
workingDataU32[1] = u32s[1];
return workingDataF64[0];
}

/**
* @returns a number representing the u32 interpretation
* of the bits of a number assumed to be an f32 value.
*/
export function reinterpretF32AsU32(f32: number): number {
workingDataF32[0] = f32;
return workingDataU32[0];
}

/**
* @returns a number representing the i32 interpretation
* of the bits of a number assumed to be an f32 value.
*/
export function reinterpretF32AsI32(f32: number): number {
workingDataF32[0] = f32;
return workingDataI32[0];
}

/**
* @returns a number representing the f32 interpretation
* of the bits of a number assumed to be an u32 value.
*/
export function reinterpretU32AsF32(u32: number): number {
workingDataU32[0] = u32;
return workingDataF32[0];
}

/**
* @returns a number representing the i32 interpretation
* of the bits of a number assumed to be an u32 value.
*/
export function reinterpretU32AsI32(u32: number): number {
workingDataU32[0] = u32;
return workingDataI32[0];
}

/**
* @returns a number representing the u32 interpretation
* of the bits of a number assumed to be an i32 value.
*/
export function reinterpretI32AsU32(i32: number): number {
workingDataI32[0] = i32;
return workingDataU32[0];
}

/**
* @returns a number representing the f32 interpretation
* of the bits of a number assumed to be an i32 value.
*/
export function reinterpretI32AsF32(i32: number): number {
workingDataI32[0] = i32;
return workingDataF32[0];
}

/**
* @returns a number representing the u16 interpretation
* of the bits of a number assumed to be an f16 value.
*/
export function reinterpretF16AsU16(f16: number): number {
workingDataF16[0] = f16;
return workingDataU16[0];
}

/**
* @returns a number representing the f16 interpretation
* of the bits of a number assumed to be an u16 value.
*/
export function reinterpretU16AsF16(u16: number): number {
workingDataU16[0] = u16;
return workingDataF16[0];
}

/**
* Class that encapsulates a vector value.
*/
Expand Down
14 changes: 8 additions & 6 deletions src/webgpu/util/floating_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import {
f16,
f32,
isFloatType,
reinterpretF16AsU16,
reinterpretF32AsU32,
reinterpretF64AsU32s,
reinterpretU16AsF16,
reinterpretU32AsF32,
reinterpretU32sAsF64,
Scalar,
ScalarType,
toMatrix,
Expand Down Expand Up @@ -45,6 +39,14 @@ import {
unflatten2DArray,
every2DArray,
} from './math.js';
import {
reinterpretF16AsU16,
reinterpretF32AsU32,
reinterpretF64AsU32s,
reinterpretU16AsF16,
reinterpretU32AsF32,
reinterpretU32sAsF64,
} from './reinterpret.js';

/** Indicate the kind of WGSL floating point numbers being operated on */
export type FPKind = 'f32' | 'f16' | 'abstract';
Expand Down
40 changes: 7 additions & 33 deletions src/webgpu/util/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {
} from '../../external/petamoriken/float16/float16.js';

import { kBit, kValue } from './constants.js';
import { floatBitsToNumber, i32, kFloat16Format, kFloat32Format, u32 } from './conversion.js';
import { i32, u32 } from './conversion.js';
import {
reinterpretF64AsU64,
reinterpretU64AsF64,
reinterpretU32AsF32,
reinterpretU16AsF16,
} from './reinterpret.js';

/**
* A multiple of 8 guaranteed to be way too large to allocate (just under 8 pebibytes).
Expand Down Expand Up @@ -2061,38 +2067,6 @@ export function lcm(a: number, b: number): number {
return (a * b) / gcd(a, b);
}

/**
* @returns the bit representation as a 64-integer, via interpreting the input
* as a 64-bit float value
*/
export function reinterpretF64AsU64(input: number): bigint {
return new BigUint64Array(new Float64Array([input]).buffer)[0];
}

/**
* @returns a 64-bit float value via interpreting the input as the bit
* representation as a 64-bit integer
*/
export function reinterpretU64AsF64(input: bigint): number {
return new Float64Array(new BigUint64Array([input]).buffer)[0];
}

/**
* @returns a 32-bit float value via interpreting the input as the bit
* representation as a 32-bit integer
*/
export function reinterpretU32AsF32(input: number): number {
return floatBitsToNumber(input, kFloat32Format);
}

/**
* @returns a 16-bit float value via interpreting the input as the bit
* representation as a 16-bit integer
*/
export function reinterpretU16AsF16(hex: number): number {
return floatBitsToNumber(hex, kFloat16Format);
}

/** @returns the cross of an array with the intermediate result of cartesianProduct
*
* @param elements array of values to cross with the intermediate result of
Expand Down
Loading

0 comments on commit 2be0e90

Please sign in to comment.