import { int32 } from './int32' ; import { select_int32 } from './select' ; param { number } numerator param { number } denominator returns { number } export function intdiv ( numerator , denominator ) { return divide ( numerator , denominator ) [ 0 ] ; } param { number } numerator param { number } denominator returns { number } export function modulo ( numerator , denominator ) { return divide ( numerator , denominator ) [ 1 ] ; } function divide ( num , denom ) { if ( denom === 0 ) { throw new Error ( 'Division by zero' ) ; } let bits = Math . ceil ( Math . log2 ( num ) ) | 0 ; let N = int32 . fromInt ( num ) ; let D = int32 . fromInt ( denom ) ; let Q = int32 . zero ( ) ; let R = int32 . zero ( ) ; let Qprime = int32 . zero ( ) ; let Rprime = int32 . zero ( ) ; let i ; let compared = int32 . zero ( ) ; let swap ; for ( i = 0 ; i <= bits ; i ++ ) { R = R . lshift ( 1 ) . or ( N . rshift ( bits - i ) . lsb ( ) ) ; compared = R . compare ( D ) ; swap = ( 1 - compared . msb ( ) ) ; Rprime = R . sub ( D ) ; Qprime = Q . or ( 1 << ( bits - i ) ) ; R = select_int32 ( swap , Rprime , R ) ; Q = select_int32 ( swap , Qprime , Q ) ; } const ret = [ Q . toNumber ( ) , R . toNumber ( ) ] ; compared . wipe ( ) ; Qprime . wipe ( ) ; Rprime . wipe ( ) ; N . wipe ( ) ; D . wipe ( ) ; Q . wipe ( ) ; R . wipe ( ) ; return ret ; }