Sum batches of values with SIMD from C++
Call the C++ batch API and interpret each output position.
Optional extension. It is not required to complete the core path.
On this page
Goal: Call the C++ batch API and interpret each output position.
Before you start: A C++ project; this extension is independent of the Blueprint interface.
Optional extension. Finish the core path first, then return when you need this capability.
The idea you will use
BatchAdd adds pairs of elements. It does not reduce the whole array to a total, and five values do not establish performance.
Predict before running
If the first pair is 10 and 1, is the first output 11 or the sum of all balances?
Build and check
This exercise is for C++ projects. If you work exclusively in Blueprint, you can stop here: the array nodes in section 11 remain your workflow. This batch API does not appear as an equivalent set of Blueprint nodes.
You will sum five balances with five increments, keeping one result per position. This is not a sum of all elements into one total.
A. Prepare a test Actor
- Use Unreal’s wizard to create a C++ class derived from Actor called MiLoteNumeros. Use your game’s module, not the plugin’s module.
- In YourModuleName.Build.cs, add
"ScientificNumberTools"to the existing private dependencies list. Keep the other project dependencies. - In MiLoteNumeros.cpp, add these includes after the include for its own header:
#include "ScientificNumber.h"
#include "ScientificNumberSIMD.h"
- Find
AMiLoteNumeros::BeginPlay(). KeepSuper::BeginPlay();and add the following block underneath it, inside the function:
TArray<FScientificNumber> Saldos;
TArray<FScientificNumber> Incrementos;
for (int32 Indice = 1; Indice <= 5; ++Indice)
{
Saldos.Add(FScientificNumber(Indice * 10.0, 0));
Incrementos.Add(FScientificNumber(Indice, 0));
}
TArray<FScientificNumber> Resultados;
Resultados.SetNum(Saldos.Num());
FScientificNumberSIMD::BatchAdd(
Saldos.GetData(), Incrementos.GetData(),
Resultados.GetData(), Saldos.Num());
for (int32 Indice = 0; Indice < Resultados.Num(); ++Indice)
{
const FScientificNumber Esperado((Indice + 1) * 11.0, 0);
const bool Correcto = Resultados[Indice].IsSuccess()
&& Resultados[Indice] == Esperado;
UE_LOG(LogTemp, Display, TEXT("Lote[%d]: %s"),
Indice, Correcto ? TEXT("OK") : TEXT("ERROR"));
}
- Compile the project using your usual C++ workflow. Place an instance of MiLoteNumeros in the level and press Play. Open Output Log and search for
Lote[.
What you should see: five OK lines, from Lote[0] to Lote[4]. The corresponding results are 11, 22, 33, 44, and 55. You can stop at a breakpoint after BatchAdd and inspect Resultados to see its mantissa and exponent.
B. Check what each position represents
Change Indice * 10.0 to Indice * 100.0 and change the expected value from 11.0 to 101.0. Recompile and repeat: the results will be 101, 202, 303, 404, and 505. Each position combines the balance and increment at that same position.
Both inputs and the output must have at least Count elements; this example prepares five. BatchAdd does not return one global bool: check the state of each resulting Scientific Number before using it in the game.
This exercise teaches you to call the API and read its result. Five elements do not demonstrate a performance improvement; the benefit depends on batch size, data, and hardware. Use it when your code already processes sets of numbers, without changing your Blueprint wallet logic.
Check your reasoning
See the explanation
It is 11. Each position combines two inputs; check each result state.
Your turn: Change one input position and predict the affected output.
Ready to continue when: you can justify buffer sizes and check each result before use.
Progress is saved only in this browser and carries over when you switch language.