Aussie AI
Example: Add-as-int Approximate Multiply
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Example: Add-as-int Approximate Multiply
The add-as-integer method suggested by Mogami (2020)
simply adds the integer bit representation of two floating-point variables,
as if they are integers.
It's quite surprising that this has any useful meaning,
but it's actually a type of approximate multiplication called Mitchell's algorithm.
Here's what the C++ code looks like on 32-bit float
types:
float aussie_add_as_int_mogami(float f1, float f2) { // Add as integer Mogami(2020) int c = *(int*)&(f1)+*(int*)&(f2)-0x3f800000; return *(float*)&c; }
The magic number 0x3f800000
is (obviously) equal to “127<<23
” and its purpose is to fix up the offset of the exponent.
Otherwise, there are two offsets of 127 combined.
(Is there a faster way? It's annoying to waste a whole addition operation on what's just an adjustment.)
Note that this algorithm is one exceptional case where we don't want to use unsigned
integer types
when tweaking bit representations.
This trick needs the temporary variable of type “int
” and the pointers to be “int*
” so that
it can correctly handle the sign bits of the two floating-point numbers.
This add-as-integer algorithm is not restricted to 32-bit float
data.
It should also work for 16-bit floating-point numbers in both float16
and bfloat16
formats,
provided the magic number is changed to a different bitshift count and an offset of 15 (not 127) for 5-bit exponents.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |