I didn't find any way to improve the actual overflow check,
although if you entirely replace the "fast path" check with
checks involving unsigned masking, you get some performance
improvement. For a wide variety of input patterns, I get
about an 18% speedup versus the core long multiply code,
when modified as shown below:
#define UL_LO_HI_BIT (((unsigned long)1) << (sizeof(unsigned long) * 4U))
#define UL_LO_MASK ((UL_LO_HI_BIT) - 1)
#define UL_HI_MASK (~(UL_LO_MASK))
#define UL_HI_LO_BIT (((unsigned long)1) << ((sizeof(unsigned long) * 4U)-1))
#define UL_OVERFLOW_IMPOSSIBLE_MASK ((UL_HI_LO_BIT) - 1)
#define UL_OVERFLOW_POSSIBLE_MASK (~(UL_OVERFLOW_IMPOSSIBLE_MASK))
core_int_mul(long a, long b)
long longprod = a * b;
unsigned long ma = a & UL_HI_MASK;
if (ma == ((a < 0) ? UL_HI_MASK : 0))
unsigned long mb = b & UL_OVERFLOW_POSSIBLE_MASK;
if (mb == ((b < 0) ? UL_OVERFLOW_POSSIBLE_MASK : 0))
double doubleprod = (double)a * (double)b;
double doubled_longprod = (double)longprod;
double diff = doubled_longprod - doubleprod;
double absdiff = (diff >= 0.0) ? diff : -diff;
double absprod = (doubleprod >= 0.0) ? doubleprod : -doubleprod;
/* absdiff/absprod <= 1/32 iff
32 * absdiff <= absprod -- 5 good bits is "close enough" */
if (32.0 * absdiff <= absprod)
This version suffers no apparent degradation versus the
existing implementation when fed sets of multiplicands
evenly distributed over range(-sys.maxint, sys.maxint),
and almost always shows an improvement.
Shall I submit a patch?
This issue came up on the c++-sig. I'm not qualified to answer any of
the questions at the bottom, and hoped that this forum could be of
From: "Paul F. Kunz" <Paul_Kunz(a)SLAC.Stanford.EDU>
Subject: Re: [C++-sig] Boost V1 build on Solaris
Date: Wed, 23 Oct 2002 17:36:58 -0700
One final question, which is not specific to boost::python but
general question on building Python modules. What my Solaris
administrators have done is to configure Python with
They also did a build fron Linux with the appropriate `exec-prefix'.
What apparently this has done is to put all but one of the Python
include files in the directory `common/2.0/include'. But Python.h
includes `config.h' or `pyconfig.h' (depending on the version) which
gets installed in `sun4x_55/2.0/include'. This is appropriate since
this file is machine/OS dependent.
Now the problem is that boost::python, and other packages, gives you
one variable, like PYTHON_INCLUDES, for its build. When it is set to find
`Python.h' it will not find `pyconfig.h'.
What is the best way to handle this situation? Is it a bug in
Python's configure script? Bug in boost::python and others? Or am
I missing something obvious?
dave(a)boost-consulting.com * http://www.boost-consulting.com
Building C/C++ Extensions for Python: Dec 9-11, Austin, TX
Python2.3 contains, inside the src/Mac/Tools/IDE/ directory a
perfectly portable module called "PyFontify". Can this be added to the
Python standard library somewhere under site-packages/? Twisted Matrix
Labs distributes it with Twisted manually (with the copyright intact,
of course), but this isn't very nice.
<< radix(a)twistedmatrix.com >>
> Update of /cvsroot/python/python/dist/src/Objects
> In directory usw-pr-cvs1:/tmp/cvs-serv10274/Objects
> Modified Files:
> unicodectype.c unicodetype_db.h
> Log Message:
> Update to Unicode 3.2 database.
I haven't seen any messages about this on python-dev. Did I miss
The switch from Unicode 3.0 is a big one since 3.2 introduces
non-BMP character points for the first time.
I also don't think that it is a good idea to ship the Unicode
3.2 database while the code behaves as defined in Unicode 3.0.
And last not least, I'd like to be asked before you make such
CEO eGenix.com Software GmbH
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting: http://www.egenix.com/
Python Software: http://www.egenix.com/files/python/
I recently finished an article for IBM's developerWorks website on Python
Persistence Management. I basically cover quite a bit of material on
pickling/unpickling. I'm looking for a couple of pickling experts to critique
the article before it gets published. If you'd like to do so, please contact
me offlist at pobrien(a)orbtech.com. Thanks.
Patrick K. O'Brien
"Your source for Python programming expertise."
here's a question for y'all:
I've written a simple http proxy that logs the domains visited. The idea
is that it keeps a list of the unique domains for each day. No problem.
The problem is that I want it to be obvious if someone has deleted
something from the logs.
This is a little bit o' software meant to run on the user's box. It does
not have to be impossible to tamper with it, nor do I need to be able to
say what was removed; just "hey, someone futzed with the log!"
Any ideas would be greatly appreciated.
Well, my ignorance of Unicode has finally bitten me -- someone filed a
bug (#622831) against textwrap.py because it crashes when it attempts to
wrap a Unicode string.
Here are the problems that I am aware of:
* textwrap assumes "whitespace" means "the characters in
* textwrap assumes "lowercase letter" means "the characters in
string.lowercase" (heck, this only works in English)
Can someone tell me what the proper way to do this is? Or just point me
at the relevant documentation? I've scoured the online docs and *Python
Essential Reference*, and I know more about the codes and unicodedata
modules than I did before. But I still don't know how to replace all
whitespace with space, or detect words that end with a lowercase letter.
Greg Ward <gward(a)python.net> http://www.gerg.ca/
I brought my BOWLING BALL -- and some DRUGS!!
Gcc on MacOSX has the ability to use precompiled headers, and I
was toying with the idea of giving this a try for Python.h. Has
anyone tried this, and is it worth the hassle?
Also, are precompiled headers an Apple addition to gcc, or are
they available on all platforms?
I'm interested in trying this because when I started using
precompiled headers for MacPython with the CodeWarrior compiler
the build time for a complete Python tree want down by about
70%, so that's definitely worth a little effort...
- Jack Jansen <Jack.Jansen(a)oratrix.com>
- If I can't dance I don't want to be part of your revolution --
Emma Goldman -
Guido van Rossum wrote:
> I.e. a macro callable as
> SAFE_MULTIPLY(destination, src1, src2, on_overflow);
> meaning roughly
> destination = src1 * src2;
> if (<overflow detected>)
> There are also additions...These are easier to test: if you add a small
> positive constant to a size_t, and the result is smaller than the
> original size_t, an overflow occurred.
Why not use the same trick for multiplication? For src1,src2 > 0,
dest should always be >= MAX(src1,src2). SAFE_MULTIPLY could be
implemented something like this:
#define HALF_BITS (sizeof(size_t) * 4U)
#define LO_MASK ((((size_t)1) << (HALF_BITS))-1)
#define HI_MASK (~(LO_MASK))
#define MAX(a,b) (((a) >= (b)) ? (a) : (b))
#define SAFE_MULTIPLY(dest,src1,src2,on_error) \
size_t _x = src1; \
size_t _y = src2; \
size_t _dest = _x * _y; \
if (_x && _y && ((_x|_y) & HI_MASK)) \
if (_dest < MAX(_x,_y)) \
dest = _dest; \