Use faster clamping for Uint8ClampedArray (#422)

This commit is contained in:
Max Graey
2019-02-22 16:26:30 +02:00
committed by Daniel Wirtz
parent 6a1ffb8e2f
commit ed97672a8c
3 changed files with 293 additions and 289 deletions

View File

@ -15,6 +15,10 @@ import {
COMPARATOR
} from "./internal/sort";
function clampToByte(value: i32): i32 {
return ~(value >> 31) & (((255 - value) >> 31) | value); // & 255
}
export class Int8Array extends TypedArray<i8> {
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>();
@ -112,12 +116,12 @@ export class Uint8ClampedArray extends Uint8Array {
@inline @operator("[]=")
protected __set(index: i32, value: i32): void {
super.__set(index, max(min(value, 255), 0));
super.__set(index, clampToByte(value));
}
@inline @operator("{}=")
protected __unchecked_set(index: i32, value: i32): void {
super.__unchecked_set(index, max(min(value, 255), 0));
super.__unchecked_set(index, clampToByte(value));
}
fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8ClampedArray {