Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #198036
| Subject | Re: myrkraverk.c |
|---|---|
| Newsgroups | comp.lang.python, comp.lang.c |
| References | (3 earlier) <118mo9b$2i0j0$1@dont-email.me> <WjCrS.369387$G71.22768@fx17.ams4> <extension-20260919213739@ram.dialup.fu-berlin.de> <w9OrS.493051$a02.195416@fx11.ams4> <bits-20260920132258@ram.dialup.fu-berlin.de> |
| From | "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> |
| Organization | Watcom Pro Ltd. |
| Message-ID | <BgRrS.204631$Nn1.110223@fx18.ams4> (permalink) |
| Date | 2026-09-20 21:33 +0800 |
Cross-posted to 2 groups.
On 9/20/2026 8:25 PM, Stefan Ram wrote:
> "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> wrote or quoted:
>> _BitScanForward( &index, *digits ) ;
>
> From the documentation:
>
> |If no bit is found, the function returns 0 and the value
> |written to the address in the first parameter is undefined.
>
> . However, if there are 32 0s before a 1 is found, this
> might be intended to add "32" to the total zero count.
>
> (GCC's and Clang's "__builtin_ctz" can also count bits and
> might be more portable.)
>
> Newsgroups: comp.lang.python,comp.lang.c,sci.math
> Followup-To: comp.lang.python,comp.lang.c
Dear Stefan,
You don't need to worry about _BitScanForward(). By the time that
function call happens, we are fairly certain there is a bit to find.
This is because we have already looped past all the zero digits [1],
and assume a real integer given to us from the outer Python runtime en-
vironment is not zero. Otherwise, we simply assume the CPython runtime
to be buggy, and don't care about any of the results.
Perhaps I should have made that clearer in the code comments?
A bit more worrying is that I do not off hand know how to time my cre-
ation. This is because /timeit/ can't find it. I do not know why, and
hopefully the wizards of comp.lang.python can help benchmark this func-
tion against ( n & -n ).bit_length() - 1 # for some really long int-
egers.
See for instance this session in my recent history.
Python 3.13.15 (tags/v3.13.15:4061bc4, Aug 5 2026, 13:05:39) [MSC
v.1944 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from myrkraverk import count_lsb
>>> print( count_lsb( -( 1 << 125 ) ) )
125
>>> import timeit
## From the above, we can see that my function works, and exists. This
## also demonstrates that it totally ignores the sign bit. What follows
## is confounding.
>>> timeit.timeit( "count_lsb( 1 << 125 )" )
Traceback (most recent call last):
File "<python-input-7>", line 1, in <module>
timeit.timeit( "count_lsb( 1 << 125 )" )
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Opt\Python\3.13\Lib\timeit.py", line 237, in timeit
return Timer(stmt, setup, timer, globals).timeit(number)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
File "C:\Opt\Python\3.13\Lib\timeit.py", line 180, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
NameError: name 'count_lsb' is not defined
Why doesn't /timeit/ see my C extension function?
Best wishes, and happy Python benchmarking!
[1] Here, /digit/ is the 30bit word the internal CPython interpreter
uses to represent multiprecision integers.
--
Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
I'm not from the Internet, I just work there. | via Easynews.com
https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
( n & -n ).bit_length() - 1 ## Really? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 01:56 +0800
Re: ( n & -n ).bit_length() - 1 ## Really? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-19 18:23 +0000
Re: ( n & -n ).bit_length() - 1 ## Really? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 03:15 +0800
Re: ( n & -n ).bit_length() - 1 ## Really? Lane W <cactus_DAC@yahoo.com> - 2026-09-19 13:37 -0600
Re: ( n & -n ).bit_length() - 1 ## Really? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 04:32 +0800
Re: ( n & -n ).bit_length() - 1 ## Really? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-19 20:39 +0000
Re: ( n & -n ).bit_length() - 1 ## Really? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 04:56 +0800
myrkraverk.c (was: Re: ( n & -n ).bit_length() - 1 ## Really?) "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 18:00 +0800
Re: myrkraverk.c ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-20 12:25 +0000
Re: myrkraverk.c "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 21:33 +0800
Re: myrkraverk.c ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-20 13:48 +0000
Re: myrkraverk.c "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 22:09 +0800
jacobi_symbol.py (was: Re: myrkraverk.c) Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-21 20:34 +0800
Re: jacobi_symbol.py Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-22 15:20 +0800
Re: jacobi_symbol.py Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-25 23:32 +0800
Re: ( n & -n ).bit_length() - 1 ## Really? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 04:52 +0800
Re: ( n & -n ).bit_length() - 1 ## Really? Paul Rubin <no.email@nospam.invalid> - 2026-09-19 14:50 -0700
csiph-web