I have some rather complex and highly templated code (C++, but this may not be very relevant) of which I'd like to know the number of adds, subs, muls, divs, and sqrts at execution. Is there an automatic way to get this information (the compiler could work it out easily)? I tried to count it myself in the assembler code generated, but got confused with jp
, jmp
, and call
s.
Asked
Active
Viewed 890 times
1

Walter
- 44,150
- 20
- 113
- 196
-
1The compiler won't be able to do it because of the loops and calls. I think you need to do it at runtime somehow. Not sure how. – Charlie Burns Sep 10 '13 at 17:30
-
5I would suggest to override `+`, `-`, `*`, `/` operators and `sqrt` function for some float-like type, in which you can count their use. – Maciek D. Sep 10 '13 at 17:31
-
1Do you want the number of opcodes in the translated executable, or the number of times they get used when you run the code? Those are very different things. – aschepler Sep 10 '13 at 17:34
-
Do you want to know the number of float operations, or specifically the number of float add, float subtract, etc? – Mats Petersson Sep 10 '13 at 17:34
-
@MaciekD, I hate operator overriding but in this case your suggestion make sense. Good answer. – Charlie Burns Sep 10 '13 at 17:35
-
1It seems something in the realm of profiling tools. – LorenzoDonati4Ukraine-OnStrike Sep 10 '13 at 17:44
-
1) Write a simulator for the target architecture and have it count. 2) Put a breakpoint on every floating point operation in the code and count how many breakpoints are hit. – Hot Licks Sep 10 '13 at 17:50
-
Possibly useful here: http://stackoverflow.com/questions/7854890/profiling-floating-point-usage-in-c – Jeremy Friesner Sep 10 '13 at 18:49
-
@MaciekD. if you convert that comment into an answer, I'll accept it. – Walter Sep 10 '13 at 22:40
2 Answers
1
I would suggest to override +
, -
, *
, /
operators and sqrt
function for some float-like type, in which you can count their use.
Something like this:
struct Double {
double val;
Double(double v): val(v) {}
static unsigned add_count = 0;
Double operator+(Double other) {
++add_count;
return Double(val + other.val);
}
};
do_your_stuff<Double>();

Maciek D.
- 2,754
- 1
- 20
- 17
-
If do_your_stuff calls any external function, say `sqrt`, this won't work since `sqrt` can't handle this class. – fheshwfq Nov 28 '13 at 22:04
-
You can overload `sqrt` or any other function as well in this manner. But of course this approach only makes sense if you know what `do_your_stuff` calls. – Maciek D. Nov 29 '13 at 18:48
0
Yes you can, but the way is a bit complex:
Try to change your "add", "sub", "mul", "div", "sqrt" in the binary to some invalid opcode. Dont forget to define an invalid opcode error handler to restore the opcode. When you program runs, the cpu will trigger the invalid opcode error at those changed "add", "sub", "mul", "div", "sqrt". By counting the times invalid opcode error being triggered, you can get the exactly what you want.

user2760751
- 343
- 2
- 4
-
This would work if the state of the system is recoverable from an opcode error. Also, your method changes the functionality; so that without one of those opcodes, the program may run more iterations or less. – Thomas Matthews Sep 10 '13 at 19:48
-
huuh. This is serious hacking ... I rather have a less interfering approach. Also, how would I do this in practice *Try to change your "add", "sub", "mul", "div", "sqrt" in the binary to some invalid opcode.* ?? – Walter Sep 10 '13 at 20:55