Skip to content

ByteEncoder

Overview

An appendable/settable buffer of bytes

@author Jonathan Olson <jonathan.olson@colorado.edu>

Class ByteEncoder

import { ByteEncoder } from 'scenerystack/alpenglow';

Constructor

new ByteEncoder( arrayBuffer? : ArrayBuffer )

Instance Methods

clear()

pushByteEncoder( byteBuffer : ByteEncoder )

pushF32( f32 : F32 )

pushU32( u32 : U32 )

pushI32( i32 : I32 )

pushReversedU32( u32 : U32 )

pushU8( u8 : U8 )

resize( byteLength )

NOTE: this MAY truncate

encodeValues( values : T[], encode : ( element: T, encoder: ByteEncoder ) => void ) : this

decodeValues( decode : ( encoder: ByteEncoder, offset: number ) => T, bytesPerElement : number ) : T[]

TODO: Note the stride of each value might be larger, based on the alignment of the type for WGSL

getDebug32String() : string

Static Methods

padLeft( input : string, padding : string, length : number ) : string

padRight( input : string, padding : string, length : number ) : string

toU32Hex( n : number ) : string

toU32Binary( n : number ) : string

rightShiftU32( n : number, shift : number ) : number

A safer right-shift that handles negative and out-of-range values

alignUp( len : number, alignment : number ) : number

nextMultipleOf( val : number, rhs : number ) : number

f32ToBytes( float : number ) : U8[]

Convert f32 to 4 bytes in little endian order

u32ToBytes( int : number ) : U8[]

Convert u32 to 4 bytes in little endian order

i32ToBytes( int : number ) : U8[]

Convert i32 to 4 bytes in little endian order

toStripedIndex( blockedIndex : number, workgroupSize : number, grainSize : number ) : number

Converts an index from a normal (blocked) order to a striped order (for improved memory coherence).

fromStripedIndex( stripedIndex : number, workgroupSize : number, grainSize : number ) : number

Converts an index from a striped order to a normal (blocked) order.

getConvergentIndex( index : number, size : number ) : number

Converts to/from convergent indices (and handles the modulo-size portion)

getCorank( k : number, m : number, n : number, compare : ( aIndex: number, bIndex: number ) => number ) : number

Co-rank function, that determines the indices into two arrays that would be at a given rank if they were sorted together (with a binary search).

It will return the index into the first array (A), and the index into the second array (B) would just be k - result.

For example, if we have two arrays:

const a = [ 0, 5, 7, 7, 10, 11, 15, 16, 16, 16, 17 ]; const b = [ 1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13 ];

and define our co-rank function: const get = k => phet.alpenglow.ByteEncoder.getCorank( k, a.length, b.length, ( i, j ) => a[ i ] - b[ j ] );

The following will return a sorted array of the values from both arrays:

_.range( 0, a.length + b.length ).map( k => { if ( get( k ) !== get( k + 1 ) ) { return a[ get( k ) ]; } else { return b[ k - get( k ) ] } } ); // [0, 1, 2, 5, 5, 6, 7, 7, 7, 8, 9, 10, 10, 11, 11, 12, 13, 15, 16, 16, 16, 17]

@param k @param m @param n @param compare

Type F32

import type { F32 } from 'scenerystack/alpenglow';

number

Type I32

import type { I32 } from 'scenerystack/alpenglow';

number

Type U32

import type { U32 } from 'scenerystack/alpenglow';

number

Type U8

import type { U8 } from 'scenerystack/alpenglow';

number

Source Code

See the source for ByteEncoder.ts in the alpenglow repository.