Aussie AI

Source Code Precomputation

  • Book Excerpt from "Generative AI in C++"
  • by David Spuler, Ph.D.

Source Code Precomputation

The examples of the precomputation of square roots in the previous two sections are not particularly efficient because they must still call the sqrtf function a number of times. A far more efficient alternative is to use C++’s compile-time initialization of arrays to set up the precomputed sqrt_table array inside the C++ source code. Hence, the square_root function becomes a simple lookup into an array variable as follows. Note that the array is declared as “static” so that the initialization occurs at compile-time.

    float square_root_precalc(int n)
    {
        const int NUM_PRECALC = 100; // Precalculate to 100
        static float sqrt_table[] = {
          0.000000f, 1.000000f, 1.414214f, 1.732051f,
          2.000000f, 2.236068f, 2.449490f, 2.645751f,
          2.828427f, 3.000000f, 3.162278f, 3.316625f,
          //... etc .....
        };
        if (n >= NUM_PRECALC) return sqrtf((float)n);
        return sqrt_table[n];
    }

The simplest way to produce the values for the precomputed array is to write another program to produce them. Once the values are produced, this program could be discarded, or it could be left in the build process. The following program was used to produce the declaration of sqrt_table used in the square_root function given above. The output from the following program was copy-pasted into the source code for the program above.

    void generate_sqrt_table()
    {
        const int NUM = 100; // Precalculate to 100
        printf("static float sqrt_table[] = {\n");
        for (int i = 0; i < NUM; i++) {
            printf("%ff", sqrtf((float)i));
            if (i + 1 < NUM)
                printf(", "); // comma after all but last
            if (i % 4 == 3 && i + 1 < NUM)
                printf("\n"); // newline every 4 numbers
        }
        printf("\n};\n"); // finish off declaration
    }

Source code precomputation should always be more efficient than lazy evaluation and run-time precomputation. However, source code precomputation is only applicable when the function can be computed at compile-time (e.g., any “constexpr” function). If the computation involves any variables whose values are known only at run-time, either lazy evaluation or run-time precomputation may be needed.

 

Next:

Up: Table of Contents

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++