DSPRelated.com
Forums

convert hex to dec four digits on 320F24x DSP

Started by Lotfi May 24, 2004
>>>>> "Lotfi" == nospam <Lotfi> writes:
Lotfi> the best routine :-)) to extract the numbers Lotfi> int m,c,d,u; Lotfi> int Magic, s; Lotfi> double Val; Lotfi> // floor 16 bits Lotfi> unsigned short ns=Val; // here the entry point Lotfi> int W=16; Lotfi> m=0; c=0; d=0; u=0; Lotfi> if (ns>32767) {printf("Attention n&#4294967295;gatif "); Lotfi> ns=-ns; } Lotfi> Magic=8389; s=7; // d=1000 Lotfi> m=floor((Magic*ns>>W)>>s); Lotfi> // if (m>10) alors d&#4294967295;passement affichage Lotfi> ns-=1000*m; Lotfi> Magic=5243; s=3; // d=100 Lotfi> c=floor((Magic*ns>>W)>>s); Lotfi> ns-=100*c; Lotfi> Magic=52429; s=3; // d=10 Lotfi> d=floor((Magic*ns>>W)>>s); Lotfi> u=ns-10*d; I believe these magic numbers might be wrong, since the assumption was that all the numbers would fit in a signed 16-bit integer. 52429 clearly doesn't fit. I think they should be d m s 1000 -31981 9 100 5243 3 10 26215 2 A negative m means that instead of computing m*n/2^W, we compute m*n/2^W + n. Ray
>>>>> "Jerry" == Jerry Avins <jya@ieee.org> writes:
Jerry> Lotfi wrote: Jerry> ... >> f(M,s)= M/2^s/2^16 - 1/d Jerry> M/2^s/2^16 = (M/2^s)/2^16 = M/2^(s+16). Is that what you mean? I think so. But in the book, the computation is done like m*n/2^W/2^s because on many processors the product is returned in 2 registers, so m*n/2^W is just the register containing the most significant W bits of the result. For a DSP that has a single register containing the product, your result would work very nicely. Ray
"Raymond Toy" <toy@rtp.ericsson.se> a &#4294967295;crit dans le message news:
sxdpt8qc6e2.fsf@edgedsp4.rtp.ericsson.se...
> >>>>> "Jerry" == Jerry Avins <jya@ieee.org> writes: > > Jerry> Lotfi wrote: > > Jerry> ... > > >> f(M,s)= M/2^s/2^16 - 1/d > > Jerry> M/2^s/2^16 = (M/2^s)/2^16 = M/2^(s+16). Is that what you mean? > > I think so. But in the book, the computation is done like m*n/2^W/2^s > because on many processors the product is returned in 2 registers, so > m*n/2^W is just the register containing the most significant W bits of > the result. > > For a DSP that has a single register containing the product, your > result would work very nicely. > > Ray
of course, I have 2 words for product results my routine is : but there is a mistake somewhere, coz I don't see the correct result :-( I am in the debugging process :-) ; Convertit un "Word" hexa en "4 chars D&#4294967295;cimaux + signe" ConvDec .macro Var LACC Var BCND positif?, GEQ ZAC SUB Var SACL Var LACC #02Bh B evitneg? positif? LACC #02Dh evitneg? SACL *+ ; SPM 0 ;PM=00, no shift after multiplication ; Magic=8389; s=7; // d=1000 ; m=floor((Magic*ns>>W)>>s); LT M8389 MPY Var SPH tmp LACC tmp,9 ;16-7=9 SACH tmp ;ie 7 LSR ; ici on aurait du checker le d&#4294967295;passement de m>9! ; ns-=1000*m; LACC Var LT tmp MPY #1000 SPAC ;Subtract PREG from ACC SACL Var LACC tmp ADD #030h ; table de charact&#4294967295;res : chiffres SACL *+ ; ecrit char : millier ; Magic=5243; s=3; // d=100 ; c=floor((Magic*ns>>W)>>s); LT M5243 MPY Var SPH tmp LACC tmp,13 ;16-3=13 SACH tmp ;ie 3 LSR ; ns-=100*c; LACC Var LT tmp MPY #100 SPAC ;Subtract PREG from ACC SACL Var LACC tmp ADD #030h ; table de charact&#4294967295;res : chiffres SACL *+ ; ecrit char : centaine ; Magic=52429; s=3; // d=10 ; d=floor((Magic*ns>>W)>>s); LT M52429 MPY Var SPH tmp LACC tmp,13 ;16-3=13 SACH tmp ;ie 3 LSR ; ns-=10*m = u LACC Var LT tmp MPY #10 SPAC ;Subtract PREG from ACC SACL Var LACC tmp ADD #030h ; table de charact&#4294967295;res : chiffres SACL *+ ; ecrit char : dizaine LACC Var ADD #030h ; table de charact&#4294967295;res : chiffres SACL *+ ; ecrit char : unit&#4294967295; .endm
solved the errors, the right code is :
and it works fine :-)

; Convertit un "Word" hexa en "4 chars D&#4294967295;cimaux + signe"
ConvDec .macro Var
   LACC Var
   BCND positif?, GEQ
   ZAC
   SUB  Var
   SACL Var
   LACC #02Dh
   B  evitneg?
positif?
   LACC #02Bh
evitneg?
   SACL  signe
    CLRC    SXM             ; pas de sign extension
;   SPM   0       ;PM=00, no shift after multiplication
;   Magic=8389; s=7;  // d=1000
;   m=floor((Magic*ns>>W)>>s);
   LT  M8389
   MPY   Var
   SPH  tmp
   LACC tmp,9  ;16-7=9
   SACH  tmp   ;ie 7 LSR
;  ici on aurait du checker le d&#4294967295;passement de m>9!
;   ns-=1000*m;
   LACC Var
   LT  tmp
   MPY   #1000
   SPAC     ;Subtract PREG from ACC
   SACL Var
   LACC tmp
   ADD  #030h  ; table de charact&#4294967295;res : chiffres
   SACL  millier   ; ecrit char : millier
;    Magic=5243; s=3;            // d=100
;    c=floor((Magic*ns>>W)>>s);
   LT  M5243
   MPY   Var
   SPH  tmp
   LACC tmp,13  ;16-3=13
   SACH  tmp   ;ie 3 LSR
;   ns-=100*c;
   LACC Var
   LT  tmp
   MPY   #100
   SPAC     ;Subtract PREG from ACC
   SACL Var
   LACC tmp
   ADD  #030h  ; table de charact&#4294967295;res : chiffres
   SACL  centaine   ; ecrit char : centaine
;   Magic=52429; s=3;           // d=10
;    d=floor((Magic*ns>>W)>>s);
   LT  M52429
   MPYU   Var  ;MPY unsigned
   SPH  tmp
   LACC tmp,13  ;16-3=13
   SACH  tmp   ;ie 3 LSR
;   ns-=10*m = u
   LACC Var
   LT  tmp
   MPY   #10
   SPAC     ;Subtract PREG from ACC
   SACL Var
   LACC tmp
   ADD  #030h  ; table de charact&#4294967295;res : chiffres
   SACL  dizaine   ; ecrit char : dizaine
   LACC Var
   ADD  #030h  ; table de charact&#4294967295;res : chiffres
   SACL  unite   ; ecrit char : unit&#4294967295;
    SETC    SXM             ; remet le sign extension
   .endm


with

   .bss Var,1
   .bss signe,1
   .bss millier,1
   .bss centaine,1
   .bss dizaine,1
   .bss unite,1
   .bss M8389,1
   .bss M5243,1
   .bss M52429,1
   .bss M5000,1

and initialisation

   LDP #M8389
   SPLK #8389, M8389
   SPLK #5243, M5243
   SPLK #52429, M52429
   SPLK #5000, M5000