Date: Tue, 20 Nov 2018 10:46:55 -0800 From: Mark Millard <marklmi@yahoo.com> To: Warner Losh <imp@FreeBSD.org>, svn-src-head@freebsd.org Subject: Re: svn commit: r340664 - head/sys/sys Message-ID: <A1A26E1F-EE34-4E1B-98E2-F2829A0BEE61@yahoo.com>
next in thread | raw e-mail | index | archive | help
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) =3D=3D (x/y) + (x%y=3D=3D0 ? 0 : 1) Code illustration with the ratios that produced the magic constants . . . # more ceil_x_div_y.c=20 #include <stdio.h> // ceil_x_div_y(x,y), given 0<y // post condition: x <=3D ceil_x_div_y(x,y)*y < x+y // note: x-y < (x/y)*y <=3D x, no ceil involved // splits into: x-y < (x/y)*y < x || (x/y)*y =3D=3D x // so: x < (x/y+1)*y) < x+y || (x/y)*y =3D=3D x // // sufficient for: x=3D=3D2**63 && (y=3D=3D1000000 || y=3D=3D1000000000): unsigned long long ceil_x_div_y(unsigned long long x, unsigned long long = y) { return (x/y) + (x%y=3D=3D0 ? 0 : 1); } int main () { printf("ceil(%llu / %llu) =3D=3D %llu\n", 1ULL<<63, 1000000000ULL, = ceil_x_div_y(1ULL<<63, 1000000000ULL)); printf("ceil(%llu / %llu) =3D=3D %llu\n", 1ULL<<63, 1000000ULL, = ceil_x_div_y(1ULL<<63, 1000000ULL)); return 0; } # ./a.out ceil(9223372036854775808 / 1000000000) =3D=3D 9223372037 ceil(9223372036854775808 / 1000000) =3D=3D 9223372036855 I used unsigned long long for a simple illustration, not because I expect the FreeBSD code should use that type. =3D=3D=3D Mark Millard marklmi at yahoo.com ( dsl-only.net went away in early 2018-Mar)
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?A1A26E1F-EE34-4E1B-98E2-F2829A0BEE61>