Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F143398784
D40120.id126102.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
D40120.id126102.diff
View Options
diff --git a/sys/compat/linuxkpi/common/include/linux/math64.h b/sys/compat/linuxkpi/common/include/linux/math64.h
--- a/sys/compat/linuxkpi/common/include/linux/math64.h
+++ b/sys/compat/linuxkpi/common/include/linux/math64.h
@@ -108,6 +108,54 @@
return ((x / div) * y + (rem * y) / div);
}
+static inline uint64_t
+mul_u64_u64_div_u64(uint64_t x, uint64_t y, uint64_t z)
+{
+ uint64_t res, rem;
+ uint64_t x1, y1, y1z;
+
+ res = rem = 0;
+ x1 = x;
+ y1z = y / z;
+ y1 = y - y1z * z;
+
+ /*
+ * INVARIANT: x * y = res * z + rem + (y1 + y1z * z) * x1
+ * INVARIANT: y1 < z
+ * INVARIANT: rem < z
+ */
+ while (x1 > 0) {
+ /* Handle low bit. */
+ if (x1 & 1) {
+ x1 &= ~1;
+ res += y1z;
+ rem += y1;
+ if ((rem < y1) || (rem >= z)) {
+ res += 1;
+ rem -= z;
+ }
+ }
+
+ /* Shift x1 right and (y1 + y1z * z) left */
+ x1 >>= 1;
+ if ((y1 * 2 < y1) || (y1 * 2 >= z)) {
+ y1z = y1z * 2 + 1;
+ y1 = y1 * 2 - z;
+ } else {
+ y1z *= 2;
+ y1 *= 2;
+ }
+ }
+
+ KASSERT(res * z + rem == x * y, ("%s: res %ju * z %ju + rem %ju != "
+ "x %ju * y %ju", __func__, (uintmax_t)res, (uintmax_t)z,
+ (uintmax_t)rem, (uintmax_t)x, (uintmax_t)y));
+ KASSERT(rem < z, ("%s: rem %ju >= z %ju\n", __func__,
+ (uintmax_t)rem, (uintmax_t)z);
+
+ return (res);
+}
+
static inline uint64_t
mul_u64_u32_shr(uint64_t x, uint32_t y, unsigned int shift)
{
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Jan 30, 11:24 PM (11 h, 44 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
28105367
Default Alt Text
D40120.id126102.diff (1 KB)
Attached To
Mode
D40120: LinuxKPI: implement mul_u64_u64_div_u64()
Attached
Detach File
Event Timeline
Log In to Comment