News for hackers. Stuff that matters.
Re:Mandelbrot in x86 Assembly by Claude by ur-whale on Wednesday July 02, 2025 @04:04AM (#44441217)
The code seem to be doing calculations with integers instead of floats.

If so, why?

Re:Mandelbrot in x86 Assembly by Claude by danbruc on Wednesday July 02, 2025 @05:34AM (#44441695)
Floating point calculations used to be a lot slower than integer calculations so it was very common to use fixed point numbers. Also for good performance, you would usually not do what this code does, calculate the coordinates for each pixel explicitly. Instead you would calculate the coordinates of the starting corner and the delta between adjacent pixels and then just add the delta each time you move to the next pixel. That is also generally easier to do with fixed point numbers as adding 0.1 with floating point numbers a 1000 times will not yield 100 because 0.1 is not exactly representable with base 2 floating point numbers. For this visualization this probably does not matter too much, but if you care that you are not slightly off and you want to calculate stuff incrementally, then doing it in fixed point might make things easier. I have no clue about the first point on current hardware, if floating point calculations are still notably less performant, if there is a relevant difference in the number of execution units and so on.
Re:Mandelbrot in x86 Assembly by Claude by adrian_b on Wednesday July 02, 2025 @11:11AM (#44444728)
In very old CPUs and in many microcontrollers, floating-point operations are slower than integer operations.

However, in many old but not very old CPUs, e.g. in many from the last decade of the 20th century and from the first decade of this century, floating-point multiplication and division were much faster than integer multiplication and division.

So for those CPUs, which include models like Pentium 4, or even the first Intel Core models (those older than Nehalem, which got a dedicated integer multiplier), there were many cases when converting an integer computation into a floating-point computation could increase the speed many times.

Re:Mandelbrot in x86 Assembly by Claude by egil on Wednesday July 02, 2025 @05:41AM (#44441744)
Maybe it would have generated with floats if it was prompted for a generator in x87 assembly? It did originate as an extension to x86 on a separate chip, so it could explain the AI sticking to integers.
Re:Mandelbrot in x86 Assembly by Claude by zczc on Wednesday July 02, 2025 @05:53AM (#44441797)
Re:Mandelbrot in x86 Assembly by Claude by dspillett on Wednesday July 02, 2025 @07:17AM (#44442324)
This is _very_ common, so I'm not surprised an LLM would use the method.

Integers are much faster to process than floating point, so if you have a fixed acceptable lower level of precision it is usually a good optimisation to scale your values so they exist in integer space, so 12345678 instead of 1.2345678, perform the mass calculations on those integers, and scale back down for subsequent display.

As well as speed, this also (assuming your scaling to ints gives enough precision for your use) removes the rounding issues inherent with floating point (see https://floating-point-gui.de/), which can balloon over many iterations. This is less important to fractal calculations then the speed issue, though can be visible at very high magnifications, and a bigger issue for things like monetary work. Monetary values are often stored as pennies (or other local minimum subdivision) rather than the nominal currency, so £123.45 is stored and processed as 12345, or for thing like calcs where factions of pennies may be significant, thousandths of pennies (£122.45 -> 12345000).

Re:Mandelbrot in x86 Assembly by Claude by ur-whale on Wednesday July 02, 2025 @07:48AM (#44442552)
> Integers are much faster to process than floating point

This was common and accepted knowledge circa 1990-2005.

Is that still the case?

In 2025, I'm not so sure, and if speed is indeed what you're after, what you typically want is to pump calculations into both the floating point units and the integer units in parallel using SIMD instructions, something that should be easy on an embarrassingly parallel problem like Mandelbrot.

And regarding precision ... I was under the impression that proving with 100% certainty that a point (x,y) actually belongs to the Mandelbrot set is basically impossible unless you prove some sort of theorem about that specific point.

The numerical method is always something along the lines of "as long as the iteration hasn't diverged after N iterations we consider the point to be inside".

And both N and the definition of "diverged" are usually completely arbitrary ... so precision, meh, unless you're going to try draw something with a zoom of 1e+20 on the frontier.

Re:Mandelbrot in x86 Assembly by Claude by pixelpoet on Wednesday July 02, 2025 @08:23AM (#44442875)
It's possible to render exact Mandelbrot set images using interval arithmetic, see https://mathr.co.uk/web/m-trustworthy.html

About performance: floating point is definitely much faster than integer arithmetic these days, for most comparable operations.

One more point about precision: integer arithmetic is exact, and so if you can work with a reasonably small bound (e.g. [-2,2]^2 for Mandelbrot) then integer arithmetic actually becomes more accurate than FP for the same number of bits.

Re:Mandelbrot in x86 Assembly by Claude by adrian_b on Wednesday July 02, 2025 @11:29AM (#44444947)
With most available software packages floating point arithmetic remains much faster than integer arithmetic, but because those software packages do not use the more recent CPU features, since thanks to Intel's stupidity there still are many CPUs which do not support those features.

Already in 2018, with Cannon Lake, Intel has introduced instructions (IFMA) which can reuse for integer computations the same vector multipliers and adders that are provided for floating-point computations. However, later Intel has removed AVX-512 from its consumer CPUs. The latest models of Intel CPUs have introduced a slower AVX-encoded variant of IFMA, but this is too little and too late, in comparison with what AMD offers.

Using IFMA and the other AVX-512 integer vector instructions, where supported, it is possible to obtain very similar throughputs, regardless if the computation is done with integer or floating-point numbers.

Hopefully, with the growing installed base of Zen 4 and Zen 5 based computers there will be the incentive to update many software packages to take advantage of their capabilities.

Re:Mandelbrot in x86 Assembly by Claude by dspillett on Wednesday July 02, 2025 @10:45AM (#44444423)
> floating point is definitely much faster than integer arithmetic these days, for most comparable operations.

Is that true of code running on CPUs? My GPU can run rings around the CPU at floating point, and probably itself at integer arithmetic, but I find it hard to believe any CPU is faster at a floating point version of an algorithm than an integer version of the same (assuming both are well, or equally badly, optimised).