FLOPs (the lowercase s indicates the plural of FLOP, per Martinho Fernandes comment) are referring to machine language floating point instructions, so it depends how many instructions your code compiles down to.
First off, if all of these variables are integers, then there are no FLOPs in this code. Let's assume, however, that your language recognizes all of these constants and variables as single-precision floating point variables (using single-precision makes loading the constants easier).
This code could compile to (on MIPS):
Assignment of variables: x is in $f1, a is in $f2, i is in $f3.
All other floating point registers are compiler-generated temporaries.
$f4 stores the loop exit condition of 10.0
$f5 stores the floating point constant 1.0
$f6 stores the floating point constant 2.0
$t1 is an integer register used for loading constants
into the floating point coprocessor.
lui $t1, *upper half of 0.0*
ori $t1, $t1, *lower half of 0.0*
lwc1 $f3, $t1
lui $t1, *upper half of 10.0*
ori $t1, $t1, *lower half of 10.0*
lwc1 $f4, $t1
lui $t1, *upper half of 1.0*
ori $t1, $t1, *lower half of 1.0*
lwc1 $f5, $t1
lui $t1, *upper half of 2.0*
ori $t1, $t1, *lower half of 2.0*
lwc1 $f6, $t1
st: c.gt.s $f3, $f4
bc1t end
add.s $f1, $f1, $f5
lui $t1, *upper half of 5.0*
ori $t1, $t1, *lower half of 5.0*
lwc1 $f2, $t1
mul.s $f2, $f2, $f1
add.s $f2, $f2, $f6
add.s $f3, $f3, $f5
j st
end: # first statement after the loop
So according to Gabe's definition, there are 4 FLOPs inside the loop (3x add.s
and 1x mul.s
). There are 5 FLOPs if you also count the loop comparision c.gt.s
. Multiply this by 10 for a total of 40 (or 50) FLOPs used by the program.
A better optimizing compiler might recognize that the value of a
isn't used inside the loop, so it only needs to compute the final value of a
. It could generate code that looks like
lui $t1, *upper half of 0.0*
ori $t1, $t1, *lower half of 0.0*
lwc1 $f3, $t1
lui $t1, *upper half of 10.0*
ori $t1, $t1, *lower half of 10.0*
lwc1 $f4, $t1
lui $t1, *upper half of 1.0*
ori $t1, $t1, *lower half of 1.0*
lwc1 $f5, $t1
lui $t1, *upper half of 2.0*
ori $t1, $t1, *lower half of 2.0*
lwc1 $f6, $t1
st: c.gt.s $f3, $f4
bc1t end
add.s $f1, $f1, $f5
add.s $f3, $f3, $f5
j st
end: lui $t1, *upper half of 5.0*
ori $t1, $t1, *lower half of 5.0*
lwc1 $f2, $t1
mul.s $f2, $f2, $f1
add.s $f2, $f2, $f6
In this case, you have 2 adds and 1 comparision inside the loop (mutiplied by 10 gives you 20 or 30 FLOPs), plus 1 multiplication and 1 addition outside the loop. Thus, your program now takes 22 or 32 FLOPs depending whether we count comparisions.