static int
fbound(double val, double minval, double maxval)
{
- if (val > maxval)
+ if (val > maxval) {
val = maxval;
- else if (val < minval + 1)
+ }
+ else if (val < minval + 1.0) {
val = minval;
+ }
+
+ /* Round towards minus infinity (-inf) */
+ val = floor(val);
+
+ /* Cast double to integer: round towards zero */
return (int)val;
}
for (i = 0; i < fragment->len; i += width) {
double val = GETRAWSAMPLE(width, fragment->buf, i);
- val *= factor;
- val = floor(fbound(val, minval, maxval));
- SETRAWSAMPLE(width, ncp, i, (int)val);
+ int ival = fbound(val * factor, minval, maxval);
+ SETRAWSAMPLE(width, ncp, i, ival);
}
return rv;
}
for (i = 0; i < len; i += width*2) {
double val1 = GETRAWSAMPLE(width, cp, i);
double val2 = GETRAWSAMPLE(width, cp, i + width);
- double val = val1*lfactor + val2*rfactor;
- val = floor(fbound(val, minval, maxval));
- SETRAWSAMPLE(width, ncp, i/2, val);
+ double val = val1 * lfactor + val2 * rfactor;
+ int ival = fbound(val, minval, maxval);
+ SETRAWSAMPLE(width, ncp, i/2, ival);
}
return rv;
}
for (i = 0; i < fragment->len; i += width) {
double val = GETRAWSAMPLE(width, fragment->buf, i);
- int val1 = (int)floor(fbound(val*lfactor, minval, maxval));
- int val2 = (int)floor(fbound(val*rfactor, minval, maxval));
+ int val1 = fbound(val * lfactor, minval, maxval);
+ int val2 = fbound(val * rfactor, minval, maxval);
SETRAWSAMPLE(width, ncp, i*2, val1);
SETRAWSAMPLE(width, ncp, i*2 + width, val2);
}
else {
double fval = (double)val1 + (double)val2;
/* truncate in case of overflow */
- newval = (int)floor(fbound(fval, minval, maxval));
+ newval = fbound(fval, minval, maxval);
}
SETRAWSAMPLE(width, ncp, i, newval);