I did some tests and now I'm completely confused. I removed almost all
code from my usart ISR, ISR code just retrive only a byte from RCREG
register and return. So it doesn't call any other routines.

My main() routine looks like this:

volatile long d1,d2;
float tmp,dist;
unsigned int dst;


int main(){
// some intilaization code of important regs
[...]
stdout = STREAM_USART;

while (1){
        d1=d2=100;
        d1=d1*d1;
        d2=d2*d2;
        if(d1 !=10000 || d2!=10000) printf("d1=%lu d2=%lu\r\n",d1,d2);
        tmp=(float) (d1+d2);
        dist=sqrtf(tmp);
        dst=(unsigned  int) dist;
        dst=dst*2;
                if( dst != 282){
                                printf("dst=%u  dist=%.4f \r\n",dst,dist);
                               }
}

when isart int is active (usart receive data), main loops return
randomly wrong values:
dst=259  dist=141.4214
d1=10000 d2=9984
dst=0  dist=0.0000
dst=2  dist=1.0000
dst=257  dist=141.4214
dst=2  dist=1.0000
d1=9984 d2=10000
dst=257  dist=141.4214
dst=2  dist=1.0000
dst=2  dist=1.0000
dst=0  dist=141.4214
dst=0  dist=0.0000
d1=9984 d2=10000
d1=10000 d2=9984


So, when interrupt is enabled all int, long or float calculations are
randomly wrong.
I know, these calcuations are not atomic, but why they are wrong even when all
 int/long sdcclib functions (mulint etc) are compiled as reentrant
(and even not in use in ISR)?
When I disable interrupt calculations problem just disappear.

regards,
ml


2012/6/11, M L <maricol...@gmail.com>:
> Hello,
> I discover some strange behaviour of sqrtf() function when usart
> interrupts are enabled.
> My  sdcclibs are compiled with --stack-auto option, libm too.
> My program is simple and just do a sqrt calculations in endless loop
> (my code compiled is with --stack-auto):
>
> float val;
> unsigned int x;
> while(1){
>   val=sqrtf(20000);
>   x=(unsigned int) val;
>  if(x!=141) printf("x=%u val=%.4f",x,val);
> }
>
> When usart receive some data (usart interrupt routine does some
> integer calculations, only integer, not float) sometimes I get
> randomly wrong values from sqrtf():
> x=0 val=0.0000
> x=19 val=19.0000
> x=1 val=1.0000
>
> I suspect some reentrancy problems, becouse when usart doesn't receive
> any chars, sqrtf always return 141.4214
> As I metioned above libsdcc and lib are compiled with --stack-auto, so
> all helper (int/float) functions should be reentrant...
>
> regards,
> ml
>

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to