CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Help with floating point math

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
bwake



Joined: 22 Jul 2008
Posts: 4

View user's profile Send private message

Help with floating point math
PostPosted: Sat Jul 26, 2008 6:28 am     Reply with quote

I cannot understand what's wrong with the compiler I use (4.068)... In another topic I pointed out that it's not properly handling interrupts for a dsPIC 30F6014, now I discovered another odd behavior in floating point math. Look at the example code:

Code:

#include <30F6014.h>
#include <math.h>


#device ANSI
#device ICD=TRUE
#device ADC=12

#FUSES NOWDT
#FUSES XT_PLL16
#FUSES PR     
#FUSES NOCKSFSM
#FUSES WPSB16 
#FUSES WPSA512
#FUSES PUT64   
#FUSES NOBROWNOUT
#FUSES BORV47   
#FUSES LPOL_HIGH
                 
#FUSES HPOL_HIGH
                 
#FUSES NOPWMPIN 
#FUSES MCLR     
#FUSES NOPROTECT
#FUSES NOWRT     
#FUSES DEBUG     
#FUSES NOCOE     
#FUSES ICS0     
#FUSES RESERVED 

#use delay(clock=120M)


void rfft(float X[],int N)
{
int I,I0,I1,I2,I3,I4,I5,I6,I7,I8, IS,ID;
int J,K,M,N2,N4,N8;
float A,A3,CC1,SS1,CC3,SS3,E,R1,XT;
float T1,T2,T3,T4,T5,T6;

[...]  //other FFT code here

        do
                {
                for(I=IS;I<N;I+=ID)
                        {
                        I1 = I + 1;
                        I2 = I1 + N4;
                        I3 = I2 + N4;
                        I4 = I3 + N4;
                        T1 = X[I4] +X[I3];
                        X[I4] = X[I4] - X[I3];
                        X[I3] = X[I1] - T1;
                        X[I1] = X[I1] + T1;
                        if(N4!=1)
                                {
                                I1 = I1 + N8;
                                I2 = I2 + N8;
                                I3 = I3 + N8;
                                I4 = I4 + N8;
                                T1 = (X[I3] + X[I4])*0.7071067811865475244;
                                T2 = (X[I3] - X[I4])*0.7071067811865475244;
                                X[I4] = X[I2] - T1;         //ERROR HERE: If X[I2] is zero and T1
                                                            //is nonzero positive, the result is positive!
                                X[I3] = (-1.0)*X[I2] - T1;  //ERROR HERE: If I simply
                                                            //write "-X[I2]", the result has wrong sign
                                X[I2] = X[I1] - T2;
                                X[I1] = X[I1] + T2;
                                }
                        }
                        IS = 2 * ID - N2;
                        ID = 4 * ID;
                }while(IS<N);


Maybe I should simple switch to MPLAB C30. I remember no time in the past I had similar problems with CCS using other PIC devices...
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: Help with floating point math
PostPosted: Sat Jul 26, 2008 12:52 pm     Reply with quote

Can you demonstrate this problem using any simpler code? It seems to me that there is too much going on here to be able to say with any certainty that things are happening the way you say.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
bwake



Joined: 22 Jul 2008
Posts: 4

View user's profile Send private message

PostPosted: Sun Jul 27, 2008 3:36 am     Reply with quote

Point is: I can't. I tried to replicate the error inside main(), declaring an array of the same size and using the same variable assignment but I obtained only correct results. This is weird, however, because there's really no reason for the code to behave differently depending on its location...
I should mention, anyway, that at least the syntax is correct: compiling the same function under a PC based compilator I can see the same FFT output MATLAB generates from the same input.

I think there must be something very wrong with the entire system I use: using ICD2 inside MPLAB to debug this code I don't get what I would expect. I cannot understand, for example, why the "step over" function jumps over entire blocks of code when floating point math is involved or why some breakpoints are resolved on the following instruction... maybe the assembly generated is not fully compatible with the debugger?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Jul 27, 2008 3:41 pm     Reply with quote

If you are not able to reproduce the error in a smaller program there is no way for us to verify your results and look for a possible bug.

A few general remarks:
- In the past it has taken CCS at least a year to create a stable compiler version for a new processor from the first compiler release.
- Only few people are discussing the dsPIC processors on this forum. This might indicate a small user base and little feedback to CCS.
- Your v4.068 is old. Current release is v4.076 and it looks like some serious bugs have been fixed:
Quote:
4.076 The COFF file format has been modified to allow MPLAB to read complex structures
4.076 A number of PCD issues fixed including access to high memory locations
4.076 Type casting of unsigned constants to signed constants modified for ANSI
4.075 A debugger problem with 88x parts is fixed
4.075 The debugger now offers an option to freeze the PIC peripherals
4.075 A PCD bug setting and clearing bits in high memory locations is fixed
4.074 A bug in structure bit to byte transfers is fixed
4.074 A PCD problem with &=0xff on an int16 is fixed
4.074 A problem in setup_counters() for some chips is fixed
4.073 UART definitions for some chips were wrong in 4.072
4.072 A number of bug fixes made for the PCD (24 bit) compiler
4.072 Setup_WDT() and Setup_Counters() have been updated for the newest chips
4.072 Using just UNSIGNED for variable types in PCD now yields a int16, not an int8
4.072 I2C slave now defaults to the clock streatch mode for all chips that support it
4.071 A debugger problem is resolved
4.070 Some IDE issues including hidden debugger screens have been fixed
4.070 I2C slave algorithms have been updated for some chips
4.069 Compiler R/W files are now kept in the windows APPDATA directory
4.069 An interrupt bug on many PCD devices is fixed
4.069 A PCD optimization error is fixed


I can't recommend on switching to the Microchip C30 compiler as I don't know that compiler but it won't be trivial.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group