From ea8a108b1f23dc1794bb6a1b561dc995a46e36b2 Mon Sep 17 00:00:00 2001 From: hniksic Date: Mon, 15 Sep 2003 10:50:43 -0700 Subject: [PATCH] [svn] Implement xdigit_to_xchar more efficiently. --- src/ChangeLog | 12 ++++++++++++ src/utils.c | 30 ++++++++++++++--------------- src/utils.h | 4 ++-- src/wget.h | 53 ++++++++++++++++++++++++++++++++++----------------- 4 files changed, 65 insertions(+), 34 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index d5c61083..503f2759 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,15 @@ +2003-09-15 Hrvoje Niksic + + * wget.h (XDIGIT_TO_XCHAR): Implement as index into a literal + string. + (XDIGIT_TO_xchar): Ditto. + +2003-09-15 Hrvoje Niksic + + * utils.c: Change the type of timer-related functions from long to + double, for better precision. On machines supporting gettimeofday + the timers now work with granularity of less than one millisecond. + 2003-09-15 Hrvoje Niksic * cookies.c (parse_set_cookies): Fixed the parser to handle more diff --git a/src/utils.c b/src/utils.c index a67ec8de..72b95279 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1551,11 +1551,11 @@ struct wget_timer { /* The most recent elapsed time, calculated by wtimer_elapsed(). Measured in milliseconds. */ - long elapsed_last; + double elapsed_last; /* Approximately, the time elapsed between the true start of the measurement and the time represented by START. */ - long elapsed_pre_start; + double elapsed_pre_start; }; /* Allocate a timer. It is not legal to do anything with a freshly @@ -1624,12 +1624,12 @@ wtimer_reset (struct wget_timer *wt) wt->elapsed_pre_start = 0; } -static long +static double wtimer_sys_diff (wget_sys_time *wst1, wget_sys_time *wst2) { #ifdef TIMER_GETTIMEOFDAY - return ((wst1->tv_sec - wst2->tv_sec) * 1000 - + (wst1->tv_usec - wst2->tv_usec) / 1000); + return ((double)(wst1->tv_sec - wst2->tv_sec) * 1000 + + (double)(wst1->tv_usec - wst2->tv_usec) / 1000); #endif #ifdef TIMER_TIME @@ -1637,7 +1637,7 @@ wtimer_sys_diff (wget_sys_time *wst1, wget_sys_time *wst2) #endif #ifdef WINDOWS - return (long)(wst1->QuadPart - wst2->QuadPart) / 10000; + return (double)(wst1->QuadPart - wst2->QuadPart) / 10000; #endif } @@ -1645,11 +1645,11 @@ wtimer_sys_diff (wget_sys_time *wst1, wget_sys_time *wst2) reset. It is allowed to call this function more than once to get increasingly higher elapsed values. */ -long +double wtimer_elapsed (struct wget_timer *wt) { wget_sys_time now; - long elapsed; + double elapsed; wtimer_sys_set (&now); elapsed = wt->elapsed_pre_start + wtimer_sys_diff (&now, &wt->start); @@ -1678,18 +1678,18 @@ wtimer_elapsed (struct wget_timer *wt) return elapsed; } -/* Return the assessed granularity of the timer implementation. This - is important for certain code that tries to deal with "zero" time - intervals. */ +/* Return the assessed granularity of the timer implementation, in + milliseconds. This is important for certain code that tries to + deal with "zero" time intervals. */ -long +double wtimer_granularity (void) { #ifdef TIMER_GETTIMEOFDAY /* Granularity of gettimeofday is hugely architecture-dependent. However, it appears that on modern machines it is better than - 1ms. */ - return 1; + 1ms. Assume 100 usecs. */ + return 0.1; #endif #ifdef TIMER_TIME @@ -1698,7 +1698,7 @@ wtimer_granularity (void) #endif #ifdef TIMER_WINDOWS - /* ? */ + /* #### Fill this in! */ return 1; #endif } diff --git a/src/utils.h b/src/utils.h index e5b31698..3ae279cf 100644 --- a/src/utils.h +++ b/src/utils.h @@ -109,8 +109,8 @@ struct wget_timer *wtimer_allocate PARAMS ((void)); struct wget_timer *wtimer_new PARAMS ((void)); void wtimer_delete PARAMS ((struct wget_timer *)); void wtimer_reset PARAMS ((struct wget_timer *)); -long wtimer_elapsed PARAMS ((struct wget_timer *)); -long wtimer_granularity PARAMS ((void)); +double wtimer_elapsed PARAMS ((struct wget_timer *)); +double wtimer_granularity PARAMS ((void)); char *html_quote_string PARAMS ((const char *)); diff --git a/src/wget.h b/src/wget.h index 21b758bd..bcd72f28 100644 --- a/src/wget.h +++ b/src/wget.h @@ -157,23 +157,42 @@ char *xstrdup_debug PARAMS ((const char *, const char *, int)); /* The smaller value of the two. */ #define MINVAL(x, y) ((x) < (y) ? (x) : (y)) -/* Convert the ASCII character X to a hex-digit. X should be between - '0' and '9', or between 'A' and 'F', or between 'a' and 'f'. The - result is a number between 0 and 15. If X is not a hexadecimal - digit character, the result is undefined. */ -#define XCHAR_TO_XDIGIT(x) \ - (((x) >= '0' && (x) <= '9') ? \ - ((x) - '0') : (TOUPPER(x) - 'A' + 10)) - -/* The reverse of the above: convert a HEX digit in the [0, 15] range - to an ASCII character representing it. The A-F characters are - always in upper case. */ -#define XDIGIT_TO_XCHAR(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'A')) - -/* Like XDIGIT_TO_XCHAR, but produce a lower-case char. */ -#define XDIGIT_TO_xchar(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'a')) - -#define ARRAY_SIZE(array) (sizeof (array) / sizeof (*(array))) +/* Convert the ASCII character that represents a hexadecimal digit to + the number in range [0, 16) that corresponds to the digit. X + should be between '0' and '9', or between 'A' and 'F', or between + 'a' and 'f'. If X is not a hexadecimal digit character, the result + is undefined. */ +#define XCHAR_TO_XDIGIT(x) \ + (((x) >= '0' && (x) <= '9') ? ((x) - '0') : (TOUPPER(x) - 'A' + 10)) + +/* The reverse of the above: convert a digit number in the [0, 16) + range to an ASCII character. The A-F characters are in upper + case. */ +#define XDIGIT_TO_XCHAR(x) ("0123456789ABCDEF"[x]) + +/* Like XDIGIT_TO_XCHAR, but generates lower-case characters. */ +#define XDIGIT_TO_xchar(x) ("0123456789abcdef"[x]) + +/* Returns the number of elements in an array with fixed + initialization. For example: + + static char a[] = "foo"; -- countof(a) == 4 (for terminating \0) + + int a[5] = {1, 2}; -- countof(a) == 5 + + char *a[3] = { -- countof(a) == 3 + "foo", "bar", "baz" + }; + + And, most importantly, it works when the compiler counts the array + elements for you: + + char *a[] = { -- countof(a) == 4 + "foo", "bar", "baz", "qux" + } */ +#define countof(array) (sizeof (array) / sizeof (*(array))) + +#define ARRAY_SIZE(array) countof (array) /* Copy the data delimited with BEG and END to alloca-allocated storage, and zero-terminate it. BEG and END are evaluated only -- 2.39.2