I have been having an issue with a certain arithmetic calculation. It is rather complex (a normalised least means squares update):
num = epsilon*nf[k].e[0];
frac = num / nf[k].enorm;
sub = p*frac;
res = nf[k].h - sub;
nf[k].h = res;
As you can see, I have split up the calculation into components num, frac, sub, res.
However, the part that causes the huge cycle count is the final assignment of res to nf[k].h.
Note: 'nf' is an array of structures. Within a loop, I am working on the k'th structure and its elements (.h, .e[0] etc) at a time. All values are float
I have tried storing my structures in memory so they are accessed as close as possible to sequential order.
I have checked the disassembly and when doing the assignment, the program seems to branch off into a '_divf' and '_isnanf' assembly commands for no apparent reason. This should not be due to interrupts, since i think interrupts should be turned off when debugging.
I have tried declaring the value as volatile, but that doesnt help.
I have tried casting to float, but that only helps by a fractional amount, nowhere near for this to be actually used in practice.
Does anyone know what the problem could be?
SMALL UPDATE:
I have tried assigning nf[k].h directly to a small decimal such as 1.1. this works fine.
I tried assigning nf[k].h directly to a large decimal such as 1.9999999999999999999. This failed as before. Even if i cast this decimal to (float).
I tried assigning 'res' to a small decimal such as 1.1. this doesnt work either
UPDATE 2:
I narrowed it down to the line "frac = ..." etc. If i dont divide it by enorm, then the expected cycle count is observed. However, this division is crucial to my program. Is there a way around this? I can successfully assign a local variable to 1/enorm. However, when i try to use this variable to calculate frac, the same problem occurs where it takes an insanely large amount of clock cycles.
If it helps, the value for "num" is usually very small, to the order of ^-10 usually. Could this be the issue? I tried scaling up and down by 10^6 but this doesnt help/makes things worse