Implementation of CUDA extended precision floating point operations

I have implemented a CUDA library to perform extended precision floating point add, subtract, multiply, divide and compare with number of bits precision between 128 and 4096 bits.

I did this by using the Qwen 3.8 27B model running locally on my system to see what that model was capable of as well as experimenting with a program for animation of zooming into fractals like what a bunch of youtube videos like the Maths Town channel did, trying to approximate real time generation. I wasn’t successful, but results weren’t horrible, maybe 10x slower than real time.

I built this library by prompting the model to build a CUDA library implementing these operations and to match the results generated by GMP on CPU, exact to the last bit.

Over a couple weeks, I got the model to build the library, to build testcases, run those tests and debug the library until it got correct, equivalent results, and some benchmark programs.

I got a working library with numerically equivalent results. I also separately tested this by having the model build tests to compute pi, e, and some irrational square roots to 1000 decimal digits precision and comparing to results from the internet, which compared equivalent.

I had the model generate an equivalent benchmark using GMP and got this comparison where the GMP/CPU program ran 1 thread and 8 threads on an I9-10900X. The GPU is an RTX 4070 TI Super.

4.3 CPU (GMP) vs GPU (CUDA device)

Throughput in ops/s; speedup columns are GPU divided by the GMP column.

Op width GMP (1 thr) GMP (8 thr) GPU GPU/GMP-1t GPU/GMP-8t
Add 256-bit 1,340,027 3,589,792 2,485,645,358 1855x 692x
Sub 256-bit 1,445,074 5,800,536 1,947,230,053 1348x 336x
Mul 256-bit 1,393,644 6,158,445 1,820,266,830 1306x 296x
Div 256-bit 467,192 2,931,001 540,426,613 1157x 184x
Add 512-bit 883,994 5,064,897 1,247,891,064 1411x 246x
Sub 512-bit 1,029,271 4,710,394 1,148,414,155 1116x 244x
Mul 512-bit 910,510 4,753,952 582,832,777 640x 123x
Div 512-bit 245,220 1,813,422 147,948,648 603x 82x
Add 4096-bit 109,452 813,875 50,963,849 466x 63x
Sub 4096-bit 103,742 753,854 45,517,430 438x 60x
Mul 4096-bit 74,988 604,934 2,285,142 30.5x 3.8x
Div 4096-bit 24,444 196,485 506,380 20.7x 2.6x

At this point I have what I consider a working library. I’m willing to create a github repository and upload the library after I clean it up and maybe fine tune it is there is interest, since I haven’t had much luck finding a similar library in the past.

Most importantly, I am a retired software engineer working in HPC that has spent part of my time learning about software including AI. I have a limited math background and understand the basic ideas behind the library, and that’s it. So I’d be uploading it for interested people to use as starting point, but my ability to fix things would be limited.

So if there’s interest in this, I can take it further. If not, I’ll drop it.

Definitely upload it, thanks!

Could the tests (or theorerical reasoning) proof that the library is accurate even for higher bits? Then an exhaustive comparison is no longer possible. But one could reason, which cases belong to the same program flow, etc.

Based on the above description of the creation process, I assume that neither a copyright notice nor one of the common open-source licenses attaches to this code? Am I further correct to assume that the code base that was created has not been audited using plagiarism tools?

At least under current US copyright rules, copyright can only extend to material with “[..]sufficient human controls over the expressive elements”, while “prompts do not alone provide sufficient control”. Trickily, “[w]hether human contributions to AI-generated outputs are sufficient to constitute authorship must be analyzed on a case-by-case basis” ( Copyright and Artificial Intelligence | U.S. Copyright Office ).

As I learned myself over the years, aside from hobby uses by individuals, publicly posted code cannot gain traction in the world if it does not come with a copyright notice and an OSI approved open-source license attached. In many cases, that may be further restricted to particular permissive licenses like BSD and MIT.

Furthermore, companies may (1) audit their code base using plagiarism tools to ferret out identifiable pieces of code to which rights of a third party may attach but that have not been explicitly licensed (2) meticulously document all uses, no matter how small, of third-party open-source code they have licensed to comply with the license terms. There is usually a formal process also involving the legal department to approve and track such uses. In as much as AI-generated code may contain identifiable snippets of other people’s code, there is risk there. The more niche the field of application the more likely it becomes that such snippets are present.

In purely practical terms, I observe that open-source projects, e.g. on GitHub, can only thrive if there is at least one active maintainer. Over the years I have come across several multi-precision library projects for GPUs that are basically dead and unused for that reason. For all I know they might not even build with current CUDA tool chains.

How did you (or the LLM) map the operations to CUDA thread blocks and threads?

For Mandelbrot generation I would think that each CUDA thread can initially work on a pixel, therefore being in charge of processing the entire (e.g. 256 bit) number.

But maybe there is a crossover point where using the entire thread block for one number is beneficial – allowing to hold most of that number either in registers or shared memory instead of spilling to global or local memory.

nVidia XMP has seen its last commits 10 years ago ;) Bit rot can also hit corporate sponsored open source projects pretty hard.

I read sections 2 and 5 of that copyright office document. In my case, where I had the model generate the code, tests and benchmarks, following my prompts, and where I have not significantly changed the generated code, I can’t copyright it, and therefore any of the open source licenses can’t be used. Maybe I could copyright and license it if I used the model as a replacement for Google search, had it explain papers on the topic, and wrote the code myself based on model generated text I could copyright and license it. But that’s not what I did.

As for me, this is a hobby/self education project. So I would not be trying to get any kind of ruling this is copyrightable, nor would I be running it thru plagiarism tools.

Based on my employment experience, that probably rules out anybody just using this in any marketed software product. However, my employer was more lax regarding using software we downloaded as a tool we used in developing a product. We were expected to read licensing terms to make sure we didn’t violate the terms, and that we could not copy anything from that software into our product.

My intent was along the lines of the second, where this could be useful to someone as a tool to help them do something, I’m happy to just let them use it. Back in the 1970’s and 1980’s there was the concept of public domain code that people just put out there and anyone could do what they wanted. Again, that was my intent.

My ‘just putting the code out there’ is more of a statement that I don’t have the math background to fix problems such as inaccurate or imprecise results. What my support would amount to is fixing what I could, asking the AI model to fix other things in the where I still have resources to do so. So I’m being up front that me fully supporting this code is unlikely.

Could the tests (or theorerical reasoning) proof that the library is accurate even for higher bits? Then an exhaustive comparison is no longer possible. But one could reason, which cases belong to the same program flow, etc.

I’m not sure I understand the question.

The number of bits precision is easily increased, just more loop iterations in the calculations. I arbitrarily limited the exponent to the decimal range +/-10000 so the exponent would fit in a signed short.

The real way to verify correctness is reviewing the code looking for errors. Verifying by comparison with known results or by comparison with a reliable alternate software provides some level of confidence the library is correct.

I’ve looked at the code and it looks reasonable. It’s not using sophisticated algorithms. Multiplication for instance uses the grammar school method of multiplication. I also tested by generating 500000 random pairs of numbers, calculating and comparing with gmp. But there’s probably edge cases to be tested, which without math background I have a hard time determining.

The code is not perfect, but is fast enough for what I wanted. It definitely needs cleanup and restructuring if I do any more with it.

How did you (or the LLM) map the operations to CUDA thread blocks and threads?

The operands are 1 dimension arrays of numbers. The model chose a hard coded block size of 256 elements. A single thread performs the entire calculation for a pair of operands. I don’t know much about using multiple threads to subdivide an operation into smaller pieces across some number of threads,

I suppose addition for instance at 4K bits could be divided into 4 1K chunks across 4 threads where each thread iterates up to compute a partial sum. But then there’s a barrier and the problem of rippling intermediate carries upward. Besides which, the time spent dealing with partial adds might just as well be spent dealing with operating on another pair of numbers.

Back when I did a 1024 bit integer arithmetic library I solved the carry propagation using warp shuffle and warp vote instructions. It helped that 1024 bit numbers mapped exactly to 32 threads (one warp), assuming 32 bit integers per thread The worst case performance of course was that 31 iterations were needed where each carry propagation overflowed the next higher limb, but that case would be exceedingly rare. My solution did not really consider arbitrary bit lengths.

I think the maximum iterations can be solved by collecting two warp votes:

  • do you send a carry, if you don’t receive one (G)
  • do you send a carry, if you receive a carry (H)

Then each lane can use logical operations to calculate the resulting carry by themselves without further shuffle or iteration.

As an iterative formula, each bit (= for each lane) determines from which of the two 32-bit values to read the next bit.

(I asked a LLM for the closed formula from those assumptions (and only working for addition with G=>H), it said, it is (G + H) ^ G ^ H, but I have not checked it. With ^ being XOR. The result gives which lane receives a carry.)

[An alternative way is to directly send G and G ^ H and calculate ((G ^ H) + 2 * G) ^ (G ^ H). Could potentially save one operation.]

Back when I did a 1024 bit integer arithmetic library I solved the carry propagation using warp shuffle and warp vote instructions. It helped that 1024 bit numbers mapped exactly to 32 threads (one warp), assuming 32 bit integers per thread

For now, I am getting something that works, then maybe go back and optimize. I understand trying to run work for one operation such as addition across a number of threads for speedup, but I’m not sure what that buys me if I’m doing something like adding pairs of numbers in 2 million element arrays. If I’m keeping all threads close to 100% busy each computing a single sum then isn’t that peak performance? Are there other factors like stalls waiting for memory, or a slow thread in a block stalling the others in the block when they could be doing something productive if the addition was divided and run on more threads?

Addition and Subtraction: If your computation cores are ~100% busy and you are happy with the minimum number of needed pairs and the actual computation algorithm is efficient and the latency is sufficient, then it is good enough.

Memory bandwidth could be a limiting factor. Try to optimize that the reads in global memory can use 32 bytes. E.g. avoid each thread of a warp (lane) reading from a different array.


Mul and Div are much more interesting than addition and subtraction, as the operands fold over each other. Suddenly you can store many more parts of the numbers in different threads or parts of the memory hierarchy (depending on length). The Tensor Cores start to be useful (in 8-bit INT mode).