Count Leading Zeros For Efficient Logarithms

[Ihsan Kehribar] points out a clever trick you can use to quickly and efficiently compute the logarithm of a 32-bit integer. The technique relies on the CLZ instruction which counts the number of leading zeros in a machine word and is available in many modern processors. Typical algorithms used to compute logarithms are not quick and have a variable execution time depending on the input value. The technique [Ihsan] is using is both fast and has a constant run time.

The above equation summarized the math behind the algorithm. We get the first term easily using the CLZ instruction. Using the remainder and a pre-computed lookup table, it is possible to get the second term to various degrees of accuracy, depending on how big you make the table and whether or not you take the performance hit of interpolation or not — those of a certain age will no likely groan at the memory of doing interpolation by hand from logarithm tables in high school math class. [Ihsan] has posted an MIT-licensed implementation of this technique in his GitHub repository, which includes both the C-language algorithm and Python tools to generate the lookup table and evaluate the errors.

Why would you do this? Our first thought was real-time streaming DSP operations, where you want fast and deterministic calculations, and [Ihsan]’s specifically calls out embedded audio processing as one class of such applications. And he should know, after all, since he developed a MIDI capable polyphonic FM synthesizer on a Cortex M0 that we covered way back in 2015.

Rubber Band “Slide Rule” Doesn’t Slide, But Rotates

Around here we mostly enjoy slide rules. We even have our own collections including some cylindrical and circular ones. But [Mathologer] discusses a recent Reddit post that explains a circular slide rule-like device using a wheel and a stretchable rubber band. While it probably would be difficult to build the actual device using a rubber band, it can do wonders for your understanding of logarithms which still show up in our lives when, for example, you are calculating decibels. [Dimitri] did simulate the rubber band for you in software.

The idea is that a perfect rubber band has numbers from 0 to 10 evenly marked on it. As you rotate a wheel attached at the 10 mark, the rubber band stretches more and more. So the 10 and the 9 have relatively little space between them, but the 1 and the 2 are much further apart. The wheel’s circumference is set so that the 1 will exactly overlay the 10. What this means is that each spot on the wheel can represent any number that differs only by a decimal point. So you could have 3 mean 0.03, 300, or — of course — 3. Of course, you don’t need to build the wheel with a rubber band — you could just mark the wheel like a regular circular slide rule.

Continue reading “Rubber Band “Slide Rule” Doesn’t Slide, But Rotates”

Vintage Console Becomes The Calculator It Appears To Be

What’s sitting on [Bob Alexander]’s desk in the video below did not start out life as the desktop calculator it appears to be. Turning it into a standalone calculator with features the original designers couldn’t imagine turned out to be an interesting project, and a trip down the retrocomputing rabbit hole.

A little explanation is in order. Sure, with its Nixie display, calculator keypad, and chunky mid-century design, the Wang 360 desktop console looks like a retro calculator. But it’s actually only a dumb terminal for a much, MUCH bigger box, called the Electronic Package, that would fit under a desk. The foot-warming part that was once connected to [Bob]’s console by a thick cable that had been unceremoniously lopped off by a previous owner. [Bob] decided to remedy the situation with modern electronics. The console turned out to have enough room for a custom PCB carrying a PIC32, some level-shifting components, power supply modules that include the high-voltage supply for the Nixies, and a GPS module because Nixies and clocks just go together. The interesting bit is the programming; [Bob] chose to emulate the original Wang methods of doing math, which include multiplication by logarithmic addition. Doing so replicates the original look and feel of the calculator down to the rapid progression of numbers across the Nixies as the logarithms are calculated using the display registers.

We normally frown on vintage gear being given modern guts, but in this case [Bob] hit just the right balance of new and old, And given that the Electronic Packages these consoles were connected to go for $1500 or more on eBay, it was a better choice than letting the console go to scrap. A similarly respectful approach was taken with this TRS80 Model 100 revival.

Continue reading “Vintage Console Becomes The Calculator It Appears To Be”

Bone Up On Your Multiplication Skills

John Napier was a Scottish physicist, mathematician, and astronomer who usually gets the credit for inventing logarithms. But his contributions to simplifying mathematics and building shorthand solutions didn’t end there. In the course of performing the many calculations he needed to practice these subjects in the 1500s, Napier invented a kind of computing mechanism for multiplication. It’s a physical manifestation of an old system known as lattice multiplication or gelosia.

Lattice multiplication makes use of the multiplication table in order to multiply huge numbers together quickly and easily. It is thought to have originated in India and moved west into Europe. When the lattice method reached Italy, the Italians named it gelosia after the trellised window covering it resembled, which was commonly used to keep prying eyes away from one’s possessions and wife.

Continue reading “Bone Up On Your Multiplication Skills”