avr-chat
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-chat] Floating Point Math Time


From: Thomas D. Dean
Subject: [avr-chat] Floating Point Math Time
Date: Sun, 22 Apr 2012 14:13:17 -0700
User-agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:10.0.2) Gecko/20120310 Thunderbird/10.0.2

I have been using some floating point and became curious of the time.

Looks like floats are promoted to doubles?

Tom Dean

Code:

// math.c - time math functions

// 20120422 tomdean - initial version

// Test run on a ET-Base ATmega128 with 16MHz clock.

// times using xxxf() for floating point and xxx() for doubles
//
// Operation  Float   Double
// Add        7.2us    6.9us
// Subtract  10.2us    6.3us
// Multuiply  9.5us    9.5us
// Divide    31.2us   29.2us
// Sin      140.0us  140.0us
// Asin     174.0us  176.0us
// Cos      122.0us  120.0us
// Acos     172.0us  170.0us
// Tan      174.0us  164.0us
// Atan     192.0us  180.0us
// Ceil       4.08us   4.04us
// Floor      5.9us    5.9us
// Round      9.9us   10.0us
// log      180.0us  174.0us
// fmod       5.2us    5.2us
// modf      31.6us   29.2us

#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include <math.h>

volatile float a,b,x;
volatile double c,d,y;
volatile uint8_t adc_chan;
volatile uint16_t adc[8];
volatile int z;

// use adc to get semi-random values
ISR(ADC_vect) {
  // save current conversion value
  adc[adc_chan++] = ADC;
  if (adc_chan > 7) adc_chan = 0;
  // set the channel
  ADMUX = (ADMUX & 0xe0) | adc_chan;
  // start another conversion
  ADCSRA |= _BV(ADSC);
}

int main() {
  ADMUX  = _BV(REFS0) | _BV(ADLAR);
  // enable ADC, clock/64 - a little high, but, we use the hi 8 bits
  ADCSRA = _BV(ADEN) | _BV(ADIE) | _BV(ADPS2)  | _BV(ADPS1);
  // start with channnel 0
  adc_chan = 0;
   // start the first conversion - the ISR will control after this
  ADCSRA |= _BV(ADSC);

  sei();

  DDRD = 0xff; // all out
  PORTD = 0;

  while (1) {

        a = (float)adc[0];
        b = (float)adc[1];
        c = (double)adc[2];
        d = (double)adc[2];
        ////////////////////////
        // float
        PORTD |= _BV(0);
        x = roundf(a);
        PORTD &= ~_BV(0);
        a = x;
        
        PORTD |= _BV(1);
        x = logf(x);
        PORTD &= ~_BV(1);
        a = x;
        
        PORTD |= _BV(2);
        x = fmodf(a,b);
        PORTD &= ~_BV(2);
        a = x;

        PORTD |= _BV(3);
        x = modff(a,(float *)&b);
        PORTD &= ~_BV(3);
        a = x;

        /////////////////////////
        // double
        PORTD |= _BV(4);
        y = round(c);
        PORTD &= ~_BV(4);
        c = y;
        
        PORTD |= _BV(5);
        y = log(y);
        PORTD &= ~_BV(5);
        c = y;
        
        PORTD |= _BV(6);
        y = fmod(c,d);
        PORTD &= ~_BV(6);
        c = y;
        
        PORTD |= _BV(7);
        y = modf(c,(double *)&d);
        PORTD &= ~_BV(7);
        c = y;
 }

  return 0;
}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]