+ struct timerel res;
+ uint64_t rem, ns;
+
+ /* Dividing seconds is simple. */
+ res.ts.tv_sec = TIMEREL_CHECK(t).ts.tv_sec / div;
+ rem = t.ts.tv_sec % div;
+
+ /* If we can't fit remainder * 1,000,000,000 in 64 bits? */
+#if 0 /* ilog is great, but we use fp for multiply anyway. */
+ bits = ilog64(rem);
+ if (bits + 30 >= 64) {
+ /* Reduce accuracy slightly */
+ rem >>= (bits - (64 - 30));
+ div >>= (bits - (64 - 30));
+ }
+#endif
+ if (rem & ~(((uint64_t)1 << 30) - 1)) {
+ /* FIXME: fp is cheating! */
+ double nsec = rem * 1000000000.0 + t.ts.tv_nsec;
+ res.ts.tv_nsec = nsec / div;
+ } else {
+ ns = rem * 1000000000 + t.ts.tv_nsec;
+ res.ts.tv_nsec = ns / div;
+ }
+ return TIMEREL_CHECK(res);