From 79fc377e16fe1fb9074418c7fa5a4e06b9da28f4 Mon Sep 17 00:00:00 2001 From: hobbs Date: Tue, 30 Nov 2010 18:17:26 +0000 Subject: * generic/tclInt.decls, generic/tclInt.h, generic/tclIntDecls.h: * generic/tclStubInit.c: TclFormatInt restored at slot 24 * generic/tclUtil.c (TclFormatInt): restore TclFormatInt func from 2005-07-05 macro-ization. Benchmarks indicate it is faster, as a key int->string routine (e.g. int-indexed arrays). --- ChangeLog | 8 +++++ generic/tclInt.decls | 10 +++--- generic/tclInt.h | 14 +------- generic/tclIntDecls.h | 10 +++--- generic/tclStubInit.c | 4 +-- generic/tclUtil.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 109 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8279967..4fb2e64 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2010-11-30 Jeff Hobbs + + * generic/tclInt.decls, generic/tclInt.h, generic/tclIntDecls.h: + * generic/tclStubInit.c: TclFormatInt restored at slot 24 + * generic/tclUtil.c (TclFormatInt): restore TclFormatInt func from + 2005-07-05 macro-ization. Benchmarks indicate it is faster, as a + key int->string routine (e.g. int-indexed arrays). + 2010-11-29 Alexandre Ferrieux * generic/tclBasic.c: Patch by Miguel, providing a diff --git a/generic/tclInt.decls b/generic/tclInt.decls index 75780d5..eb18d99 100644 --- a/generic/tclInt.decls +++ b/generic/tclInt.decls @@ -13,7 +13,7 @@ # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # -# RCS: @(#) $Id: tclInt.decls,v 1.151 2010/11/28 23:20:10 kennykb Exp $ +# RCS: @(#) $Id: tclInt.decls,v 1.152 2010/11/30 18:17:26 hobbs Exp $ library tcl @@ -116,10 +116,10 @@ declare 22 { declare 23 { Proc *TclFindProc(Interp *iPtr, const char *procName) } -# Replaced with macro (see tclInt.h) in Tcl 8.5 -#declare 24 { -# int TclFormatInt(char *buffer, long n) -#} +# Replaced with macro (see tclInt.h) in Tcl 8.5.0, restored in 8.5.10 +declare 24 { + int TclFormatInt(char *buffer, long n) +} declare 25 { void TclFreePackageInfo(Interp *iPtr) } diff --git a/generic/tclInt.h b/generic/tclInt.h index 8c5d863..2d36154 100644 --- a/generic/tclInt.h +++ b/generic/tclInt.h @@ -15,7 +15,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclInt.h,v 1.487 2010/11/28 23:20:11 kennykb Exp $ + * RCS: @(#) $Id: tclInt.h,v 1.488 2010/11/30 18:17:26 hobbs Exp $ */ #ifndef _TCLINT @@ -4221,18 +4221,6 @@ MODULE_SCOPE Tcl_PackageInitProc Procbodytest_SafeInit; /* *---------------------------------------------------------------- - * Macro used by the Tcl core to write the string rep of a long integer to a - * character buffer. The ANSI C "prototype" for this macro is: - * - * MODULE_SCOPE int TclFormatInt(char *buf, long n); - *---------------------------------------------------------------- - */ - -#define TclFormatInt(buf, n) \ - sprintf((buf), "%ld", (long)(n)) - -/* - *---------------------------------------------------------------- * Macros used by the Tcl core to set a Tcl_Obj's numeric representation * avoiding the corresponding function calls in time critical parts of the * core. They should only be called on unshared objects. The ANSI C diff --git a/generic/tclIntDecls.h b/generic/tclIntDecls.h index ae4046d..4af7895 100644 --- a/generic/tclIntDecls.h +++ b/generic/tclIntDecls.h @@ -11,7 +11,7 @@ * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclIntDecls.h,v 1.145 2010/11/28 23:20:11 kennykb Exp $ + * RCS: @(#) $Id: tclIntDecls.h,v 1.146 2010/11/30 18:17:26 hobbs Exp $ */ #ifndef _TCLINTDECLS @@ -109,7 +109,8 @@ EXTERN int TclFindElement(Tcl_Interp *interp, int *bracePtr); /* 23 */ EXTERN Proc * TclFindProc(Interp *iPtr, const char *procName); -/* Slot 24 is reserved */ +/* 24 */ +EXTERN int TclFormatInt(char *buffer, long n); /* 25 */ EXTERN void TclFreePackageInfo(Interp *iPtr); /* Slot 26 is reserved */ @@ -628,7 +629,7 @@ typedef struct TclIntStubs { void (*reserved21)(void); int (*tclFindElement) (Tcl_Interp *interp, const char *listStr, int listLength, const char **elementPtr, const char **nextPtr, int *sizePtr, int *bracePtr); /* 22 */ Proc * (*tclFindProc) (Interp *iPtr, const char *procName); /* 23 */ - void (*reserved24)(void); + int (*tclFormatInt) (char *buffer, long n); /* 24 */ void (*tclFreePackageInfo) (Interp *iPtr); /* 25 */ void (*reserved26)(void); void (*reserved27)(void); @@ -907,7 +908,8 @@ extern const TclIntStubs *tclIntStubsPtr; (tclIntStubsPtr->tclFindElement) /* 22 */ #define TclFindProc \ (tclIntStubsPtr->tclFindProc) /* 23 */ -/* Slot 24 is reserved */ +#define TclFormatInt \ + (tclIntStubsPtr->tclFormatInt) /* 24 */ #define TclFreePackageInfo \ (tclIntStubsPtr->tclFreePackageInfo) /* 25 */ /* Slot 26 is reserved */ diff --git a/generic/tclStubInit.c b/generic/tclStubInit.c index d308f34..98ef9b7 100644 --- a/generic/tclStubInit.c +++ b/generic/tclStubInit.c @@ -8,7 +8,7 @@ * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclStubInit.c,v 1.198 2010/11/28 23:20:11 kennykb Exp $ + * RCS: @(#) $Id: tclStubInit.c,v 1.199 2010/11/30 18:17:26 hobbs Exp $ */ #include "tclInt.h" @@ -80,7 +80,7 @@ static const TclIntStubs tclIntStubs = { 0, /* 21 */ TclFindElement, /* 22 */ TclFindProc, /* 23 */ - 0, /* 24 */ + TclFormatInt, /* 24 */ TclFreePackageInfo, /* 25 */ 0, /* 26 */ 0, /* 27 */ diff --git a/generic/tclUtil.c b/generic/tclUtil.c index 74cbff1..fd16f88 100644 --- a/generic/tclUtil.c +++ b/generic/tclUtil.c @@ -11,7 +11,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclUtil.c,v 1.120 2010/11/29 02:27:12 kennykb Exp $ + * RCS: @(#) $Id: tclUtil.c,v 1.121 2010/11/30 18:17:26 hobbs Exp $ */ #include "tclInt.h" @@ -2496,6 +2496,92 @@ TclNeedSpace( /* *---------------------------------------------------------------------- * + * TclFormatInt -- + * + * This procedure formats an integer into a sequence of decimal digit + * characters in a buffer. If the integer is negative, a minus sign is + * inserted at the start of the buffer. A null character is inserted at + * the end of the formatted characters. It is the caller's + * responsibility to ensure that enough storage is available. This + * procedure has the effect of sprintf(buffer, "%ld", n) but is faster + * as proven in benchmarks. This is key to UpdateStringOfInt, which + * is a common path for a lot of code (e.g. int-indexed arrays). + * + * Results: + * An integer representing the number of characters formatted, not + * including the terminating \0. + * + * Side effects: + * The formatted characters are written into the storage pointer to + * by the "buffer" argument. + * + *---------------------------------------------------------------------- + */ + +int +TclFormatInt(buffer, n) + char *buffer; /* Points to the storage into which the + * formatted characters are written. */ + long n; /* The integer to format. */ +{ + long intVal; + int i; + int numFormatted, j; + char *digits = "0123456789"; + + /* + * Check first whether "n" is zero. + */ + + if (n == 0) { + buffer[0] = '0'; + buffer[1] = 0; + return 1; + } + + /* + * Check whether "n" is the maximum negative value. This is + * -2^(m-1) for an m-bit word, and has no positive equivalent; + * negating it produces the same value. + */ + + if (n == -n) { + return sprintf(buffer, "%ld", n); + } + + /* + * Generate the characters of the result backwards in the buffer. + */ + + intVal = (n < 0? -n : n); + i = 0; + buffer[0] = '\0'; + do { + i++; + buffer[i] = digits[intVal % 10]; + intVal = intVal/10; + } while (intVal > 0); + if (n < 0) { + i++; + buffer[i] = '-'; + } + numFormatted = i; + + /* + * Now reverse the characters. + */ + + for (j = 0; j < i; j++, i--) { + char tmp = buffer[i]; + buffer[i] = buffer[j]; + buffer[j] = tmp; + } + return numFormatted; +} + +/* + *---------------------------------------------------------------------- + * * TclGetIntForIndex -- * * This function returns an integer corresponding to the list index held -- cgit v0.12