Author Topic: Extracting individual digits of a multi digit decimal  (Read 10379 times)

0 Members and 1 Guest are viewing this topic.

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: Extracting individual digits of a multi digit decimal
« Reply #25 on: November 09, 2016, 06:01:40 am »
So GK's method honestly is probably the way I'll go since I think I can spare the cycles, but...

How does one go about figuring out how much a particular arithmetic operation costs? I don't believe I have a profiler (running Atmel Studio with an avr Dragon using debug Wire.
four methods:
1) Write the code, compile, take the disassembly and count instructions. keep track of jumps etc. easier if you write the routine directly in ASM
2) simulator with a stopwatch
3) debugger with stopwatch
4) hardware, toggle an output before and after and measure time between the two events

3 and 4 are real world results so external interrupts and other routines which exectution time depends on the input value will affect the result of the measure
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21686
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Extracting individual digits of a multi digit decimal
« Reply #26 on: November 09, 2016, 07:28:07 am »
The nice thing about my AVR example is, AVR is very simple and deterministic, so one can count the cycles very easily, using the datasheet timings.  And it's in ASM to begin with, so you get what you write.  No worries about what the compiler will make of it.

A tree of IF statements is... one way to do it, I suppose.  But if that's all you can think of, you have much to learn about the richness of programming!

Also, if you're absolutely concerned about one concern alone -- like clock cycles, or code and data size, or memory usage, there are many optimizations that can be applied there.  Loop unrolling is one time saver; when you're doing transfer functions (i.e., one parameter comes in, a value goes out), a memory map (lookup table) can save tremendously.  But lookup tables aren't very practical on embedded systems (with limited memory), so you can't go too crazy there.

Taking advantage of your available resources is always the best path.  AVR has hardware multiply, which makes some things fantastically easy, even though the math works out worse.  One 16/16 division is worth about six 16x16 multiplies!

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Extracting individual digits of a multi digit decimal
« Reply #27 on: November 11, 2016, 06:48:17 am »
There was recently a long and detailed discussion along these lines over in avrfreaks:
http://www.avrfreaks.net/forum/optimizing-libc-integer-conversion-routines
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 19506
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Extracting individual digits of a multi digit decimal
« Reply #28 on: November 11, 2016, 10:38:23 am »
So GK's method honestly is probably the way I'll go since I think I can spare the cycles, but...

How does one go about figuring out how much a particular arithmetic operation costs? I don't believe I have a profiler (running Atmel Studio with an avr Dragon using debug Wire.

Normally you should find in the data sheet a table of processor instructions and the number of CPU cycles required for each. You can look at the generated assembly and add them up.

Only for very simple processors, those that don't have caches and don't have overlapped processing. Even on a 486 there could be an 8:1 (IIRC) variation in interrupt routine latency, depending on what was in the cache.

If you want to get accurate timings, use the XMOS processors, where the compiler/IDE will guarantee maximum timings.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf