diff --git a/cmMath.c b/cmMath.c index f740b1b..72ce7fd 100644 --- a/cmMath.c +++ b/cmMath.c @@ -154,6 +154,20 @@ unsigned cmPrevOddU( unsigned v ) { return cmIsOddU(v) ? v : v-1; } unsigned cmNextEvenU( unsigned v ) { return cmIsEvenU(v) ? v : v+1; } unsigned cmPrevEvenU( unsigned v ) { return cmIsEvenU(v) ? v : v-1; } +unsigned cmModIncr(int idx, int delta, int maxN ) +{ + int sum = idx + delta; + + if( sum >= maxN ) + return sum - maxN; + + if( sum < 0 ) + return maxN + sum; + + return sum; +} + + // modified bessel function of first kind, order 0 // ref: orfandis appendix B io.m double cmBessel0( double x ) diff --git a/cmMath.h b/cmMath.h index 57d96ea..b80716e 100644 --- a/cmMath.h +++ b/cmMath.h @@ -15,6 +15,12 @@ unsigned cmPrevOddU( unsigned v ); unsigned cmNextEvenU( unsigned v ); unsigned cmPrevEvenU( unsigned v ); +/// Increment or decrement 'idx' by 'delta' always wrapping the result into the range +/// 0 to (maxN-1). +/// 'idx': initial value +/// 'delta': incremental amount +/// 'maxN' - 1 : maximum return value. +unsigned cmModIncr(int idx, int delta, int maxN ); // modified bessel function of first kind, order 0 // ref: orfandis appendix B io.m