From: Colin Plumb <colin@nyx.net>
To: chip@perlsupport.com
Cc: linux-kernel@vger.rutgers.edu, linux-mm@kvack.org
Subject: Re: testing/pre-7 and do_poll()
Date: Mon, 11 Jan 1999 05:54:24 -0700 (MST) [thread overview]
Message-ID: <199901111254.FAA06857@nyx10.nyx.net> (raw)
Chip Salzenberg wrote:
> Well, I forgot the (unsigned long) cast, as someone else noted:
> timeout = ROUND_UP((unsigned long) timeout, 1000/HZ);
>
> Otherwise, the code is Just Right.
Um, this works perfectly when HZ == 100, but consider what happens when
HZ == 1024. 1000/HZ == 0, and then computing (x+0-1)/0 doesn't work so well.
If you want accuracy with no danger of overflow, try the following trick:
ticks = (msec/1000)*HZ + (msec%1000)*HZ/1000.
To make this more efficient, use that only on large values of msec,
and the simpler (msec*HZ)/1000. In thhe HZ > 1000 case, you also lose
the guarantee that every legal msec value corresponds to a
(C experts will note that none of the parens are necessary, but I though
it was clearer to include them.)
So, for perfection, you want:
unsigned long msec_to_ticks(unsigned long msec)
{
if (msec <= ULONG_MAX/HZ)
return msec*HZ/1000;
#if HZ > 1000
/* Wups, can overflow - saturate return value */
if (msec > (ULONG_MAX/HZ)*1000 + (ULONG_MAX % HZ)*1000/HZ)
return ULONG_MAX
#endif
return (msec/1000)*HZ + (msec%1000)*HZ/1000;
}
Um... this is the rounding-down case, and also saturates at ULONG_MAX
instead of MAX_SCHEDULE_TIMEOUT (LONG_MAX). Let me adjust the boundary
cases a bit...
#if MAX_SCHEDULE_TIMEOUT != LONG_MAX
#error Adjust this code - it assumes identical input and output ranges
#endif
unsigned long msec_to_ticks(unsigned long msec)
{
if (msec < ULONG_MAX/HZ - 999)
return (msec+999)*HZ/1000;
msec--; /* Following code rounds up and adds one */
#if HZ > 1000
/* Wups, can overflow - saturate return value */
if (msec >= (ULONG_MAX/HZ)*1000 + (ULONG_MAX % HZ)*1000/HZ)
return MAX_SCHEDULE_TIMEOUT;
#endif
return (msec/1000)*HZ + (msec%1000)*HZ/1000 + 1;
}
--
-Colin
--
This is a majordomo managed list. To unsubscribe, send a message with
the body 'unsubscribe linux-mm me@address' to: majordomo@kvack.org
next reply other threads:[~1999-01-11 12:54 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-01-11 12:54 Colin Plumb [this message]
-- strict thread matches above, loose matches on Subject: below --
1999-01-10 20:29 Results: pre6 vs pre6+zlatko's_patch vs pre5 vs arcavm13 Steve Bergman
1999-01-10 21:41 ` Linus Torvalds
1999-01-10 23:33 ` testing/pre-7 and do_poll() Chip Salzenberg
1999-01-11 6:02 ` Linus Torvalds
1999-01-11 6:26 ` Chip Salzenberg
1999-01-11 6:46 ` Linus Torvalds
1999-01-11 6:59 ` Chip Salzenberg
1999-01-11 7:02 ` Linus Torvalds
1999-01-11 22:08 ` Shawn Leas
1999-01-11 22:13 ` Linus Torvalds
1999-01-12 0:25 ` estafford
1999-01-12 8:25 ` Shawn Leas
1999-01-12 7:06 ` Gregory Maxwell
1999-01-11 20:20 ` Adam Heath
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=199901111254.FAA06857@nyx10.nyx.net \
--to=colin@nyx.net \
--cc=chip@perlsupport.com \
--cc=linux-kernel@vger.rutgers.edu \
--cc=linux-mm@kvack.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox