Warner Losh imp at FreeBSD.org wrote on Tue Nov 20 07:11:24 UTC 2018 : > Instead, used fixed constants because there's no way to say ceil(X) > for integer math. . . .
For a ratio of unsigned integers, with 0<y: What of: ceil_x_div_y(x,y) == (x/y) + (x%y==0 ? 0 : 1) Code illustration with the ratios that produced the magic constants . . . # more ceil_x_div_y.c #include <stdio.h> // ceil_x_div_y(x,y), given 0<y // post condition: x <= ceil_x_div_y(x,y)*y < x+y // note: x-y < (x/y)*y <= x, no ceil involved // splits into: x-y < (x/y)*y < x || (x/y)*y == x // so: x < (x/y+1)*y) < x+y || (x/y)*y == x // // sufficient for: x==2**63 && (y==1000000 || y==1000000000): unsigned long long ceil_x_div_y(unsigned long long x, unsigned long long y) { return (x/y) + (x%y==0 ? 0 : 1); } int main () { printf("ceil(%llu / %llu) == %llu\n", 1ULL<<63, 1000000000ULL, ceil_x_div_y(1ULL<<63, 1000000000ULL)); printf("ceil(%llu / %llu) == %llu\n", 1ULL<<63, 1000000ULL, ceil_x_div_y(1ULL<<63, 1000000ULL)); return 0; } # ./a.out ceil(9223372036854775808 / 1000000000) == 9223372037 ceil(9223372036854775808 / 1000000) == 9223372036855 I used unsigned long long for a simple illustration, not because I expect the FreeBSD code should use that type. === Mark Millard marklmi at yahoo.com ( dsl-only.net went away in early 2018-Mar) _______________________________________________ svn-src-head@freebsd.org mailing list https://lists.freebsd.org/mailman/listinfo/svn-src-head To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"