Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Génération de code pour calcul
Francesco MondadaLaboratoire de systèmes autonomes
I2S - STI - EPFL
Cours Microinformatique I
Autonomous Systems Labhttp://asl.epfl.ch
IntroductionLes ressources utilisées pour le traitement de données (calcul ou autre) dépendent fortement du type de variables utilisées, desopérations utilisées et du code généré par le compilateur.
Il est important de comprendre:• Comment sont gérées les divers types de variables• Quel code est généré par le compilateur• Quelles autres possibilités offre le processeur utilisé
Microinformatique I
Autonomous Systems Labhttp://asl.epfl.ch
Introduction
Code observé:• Gestion de types (cast)• Gestion des cast implicite• Types de variables• Entier / virgule fixe / virgule flottante
Microinformatique I
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Cast:#include "p30f6014a.h"
int main(){ int a=3; long int b; float c;
b = (long int)a; a = (int)b; c = (float)a;}
.file "C:\\TP\\tp4\\test.c"
.text
.align 2
.global _main ; export_main:
.set ___PA___,1lnk #10
mov #3,w0mov w0,[w14]
mov [w14],w0asr w0,#15,w1mov w0,[w14+2]mov w1,[w14+4]
mov [w14+2],w0mov w0,[w14]
mov [w14],w0asr w0,#15,w1rcall ___floatsisfmov w0,[w14+6]mov w1,[w14+8]
ulnkreturn.set ___PA___,0.end
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Autonomous Systems Labhttp://asl.epfl.ch
Paramètres de floatsisf.s:;-----------------------------------------------------------------------;; floatsisf.s: Convert 32-bit, signed integer to floating-point;; This file is part of the compact math library for the dsPIC30.; (c) Microchip Technology. 2003.;;-----------------------------------------------------------------------;.include "libm.inc".section .libm,"x";-----------------------------------------------------------------------;; __floatsisf: Convert 32-bit, signed integer to floating-point;; Description:;; Convert a 32-bit, signed integer to floating-point.;; Input:;; (w1:w0) Integer to be converted;; Output:;; (w1:w0) Floating-point representation of input value;;-----------------------------------------------------------------------;
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Cast implicite:#include "p30f6014a.h"
int main(){ int a=3; long int b; float c;
b = a; a = b; c = a;}
.file "C:\\TP\\tp4\\test.c"
.text
.align 2
.global _main ; export_main:
.set ___PA___,1lnk #10
mov #3,w0mov w0,[w14]
mov [w14],w0asr w0,#15,w1mov w0,[w14+2]mov w1,[w14+4]
mov [w14+2],w0mov w0,[w14]
mov [w14],w0asr w0,#15,w1rcall ___floatsisfmov w0,[w14+6]mov w1,[w14+8]
ulnkreturn.set ___PA___,0.end
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Cast implicite:#include "p30f6014a.h"
int main(){ int a,b=2;
a = 3.1415; a = 3.1415 * b;}
.file "C:\\TP\\tp4\\test.c"
.text
.align 2
.global _main ; export_main:
.set ___PA___,1lnk #4
mov #2,w0mov w0,[w14+2]
mov #3,w0mov w0,[w14]
mov [w14+2],w0asr w0,#15,w1rcall ___floatsisfmov #3670,w2mov #16457,w3rcall ___mulsf3rcall ___fixsfsimov w0,[w14]
ulnkreturn
.set ___PA___,0
.end
441 cycles
2 cycles
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Représentation des nombres: virgule flottante (représentation IEEE 754)
mov #3670,w2mov #16457,w3
3.1415?
float
1 [31] 8 [30-23] (BIAS = 127) 23 [22-0]
Sign(signe)
Exponent(exposant)
Fraction(mantisse)
double
1 [63] 11 [62-52] (BIAS = 1023) 52 [51-0]
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Représentation des nombres: virgule flottante
567 5.67 102
13 0xD 1101 +1.101 23
Sign(signe)
Exponent(exposant)BIAS = 127
Fraction(mantisse)
normalisation
binaire
décimal
0 1000 0010 1010 0000 0000 0000 0000 0000
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Représentation des nombres: virgule flottante
mov #3670,w2mov #16457,w3
Ox40490E56
mov #0x0E56,w2mov #0x4049,w3
Sign(signe)
Fraction(mantisse)
0 100 0000 0 100 1001 0000 1110 0101 0110
+1.10010010000111001010110 21
1.57074999809 x 2
Exponent(exposant)BIAS = 127
Calcul avec nombres en virgule flottante: multiplication ;-----------------------------------------------------------------------;; mulsf3.s: Single-precision floating-point multiplication operation.;; This file is part of the compact math library for the dsPIC30.; (c) Microchip Technology. 2003.;;-----------------------------------------------------------------------; .include "libm.inc"
.section .libm,code;-----------------------------------------------------------------------;;; mulsf3;; IEEE-754 single-precision multiplication elementary operation.;; Input:;; (w1:w0) Floating-point multiplier (a); (w3:w2) Floating-point multiplicand (b);; Output:;; (w1:w0) Floating-point product: round(a * b);; Description:;; The product (a * b) is calculated and rounded to nearest.;;-----------------------------------------------------------------------;
.global ___mulsf3
___mulsf3: mov.d w8,[w15++] ; Preserve scratch mov.d w10,[w15++] ; * mov.w w12,[w15++] ; *
;------ Unpack the operands
rcall __funpack2 ; Unpack both operands bra n,__fPropagateNaN ; isNaN(a) || isNaN(b) ...
;-----------------------------------------------------------------------;;; (w7:w6) a(Significand); (w9:w8) a; (w10) a(Type); (w11) a(Biased exponent);; (w1:w0) b(Significand); (w3:w2) b; (w4) b(Type); (w5) b(Biased exponent);;-----------------------------------------------------------------------;
xor w9,w3,w12 ; Preserve the result sign
;------ Check a for special operands: Infinity
cp w10,#INFTYPE ; isInf(a) ? bra z,aisinfinite ; Yes ...
;------ Check b for special operands: Infinity
cp w4,#INFTYPE ; isInf(b) ? bra z,bisinfinite ; Yes ...
;------ Both operands are finite: check for zero
cp w10,#ZEROTYPE ; isZero(a) ? bra z,return8 ; Yes ... return(a) cp w4,#ZEROTYPE ; isZero(b) ? bra z,return2 ; Yes ... return(b)
;-----------------------------------------------------------------------;; Both operands are finite and non-zero;-----------------------------------------------------------------------;
; (w7:w6) = a(significand); (w1:w0) = b(significand); (w11) = a(biased exponent); (w5) = b(biased exponent)
add w5,w11,w11 ; (w11)=result biased exponent + FLT_BIAS sub #FLT_BIAS-1,w11 ; (w11)=biased exponent + 1
;------ Form the 48-bit product of the two significands
mul.uu w6,w1,w8 ; Multiply the significands mul.uu w7,w0,w4 ; * add w4,w8,w8 ; * addc w5,w9,w9 ; * mul.uu w7,w1,w4 ; * mul.uu w6,w0,w0 ; * add w1,w8,w1 ; * addc w4,w9,w2 ; (w2:w1:w0)=product(a,b)
; The product will have an integer component of 1, 2 or 3; (i.e., the significand is 01.f, 10.f or 11.f in binary).; If the integer component is 2 or 3, then the exponent is; correct, and the binary point is located between; bits 46 and 47 of the product.; If the integer component is 1, then the exponent is; 1 greater than the correct value, and the binary point; is located between bits 45 and 46. In this case, the; product is shifted left one bit to position the; binary point between bits 46 and 47, and the exponent; is corrected. bra n,formsticky ; Exponent is correct ...
add w0,w0,w0 ; product(a,b) <<= 1 addc w1,w1,w1 ; * addc w2,w2,w2 ; * dec w11,w11 ; Correct the exponent overstep
;------ Form sticky, round, and result significand;; The 48-bit product is organized as follows:;; 1 Round bit; |; <-- 24-bit significand -->|<----- 23-bit sticky ---->; |; v; +----------------+ +----------------+ +----------------+; | w2 | | w1 | | w0 |; +----------------+ +----------------+ +----------------+; 4 3 3 2 1 1 0; 7 2 1 3 6 5 0
formsticky:
sl w1,#9,w3 ; Form sticky (23 lsb of product) ior w0,w3,w3 ; (w3) = sticky bra z,formRandSig ; Sticky is zero ... mov #1,w3 ; Sticky is non-zero
formRandSig:
lsr w2,#8,w9 ; Align significand sl w2,#8,w8 ; * lsr w1,#7,w2 ; | (w2) = round and #1,w2 ; | * lsr w1,#8,w1 ; * Continue aligning ior w1,w8,w8 ; (w9:w8)=result significand
;------ Final rounding
; (w2) = round; (w3) = sticky; (w9:w8)= p(significand); (w11) = p(exponent)
rcall __fpack ; Round and pack
return0: bclr w1,#15 ; Clear result sign btsc w12,#15 ; Result negative ? bset w1,#15 ; Yes
bra __fbopExit ; Common exit
;-----------------------------------------------------------------------;; Special arguments;-----------------------------------------------------------------------;
;------ a is an Infinity
aisinfinite: cp w4,#ZEROTYPE ; isZero(b) ? bra z,__fbopReturnNaN ; Yes ... (Inf * Zero): return(NaN)return8: mov.d w8,w2 ; Result valuereturn2: mov.d w2,w0 ; Result value bra return0 ; Common exit
;------ b is Infinity
bisinfinite: cp w10,#ZEROTYPE ; isZero(a) ? bra nz,return2 ; No ... return(b) bra __fbopReturnNaN ; Yes ... return(NaN)
;-----------------------------------------------------------------------; .end
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Représentation des nombres: changement d’échelle
signe entier fraction
0.01 = 1%1.34m = 1340mm
int i, sinus[360]; …for(i=0;i<360;i++){ sinus[i] = 10*sin(i/180*3.1415); printf("sin %d = %d\n",i,sinus[i]);}
Représentation des nombres: virgule fixe
Calcul avec nombres en virgule fixe: multiplication
signe (1) entier (5) fraction (10)
x
= fractionentiersigne
3.481 = VF 3565 = OxDED = 0 0 0 0 1 1 0111101101
*1024arrondi
-0.234 = VF -240 = OxFF10 = 1 1 1 1 1 1 1100010000
FFF2F1D0 = 1 1 1 1 1 1 1 1 1 1 1 1 1 00101111000111010000 -855600 =
-0.815 = VF -835 = OxFCBC = 1 1 1 1 1 1 0010111100
/1024arrondi
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Gestion de types:
#include "p30f6014a.h"
int main(){ int a,b=345,c=678; float fa,fb=0.345,fc=0.678;
a=b*c; fa=fb*fc;
}
.file "C:\\test-l3\\test.c"
.align 2
.global _main ; export_main:
.set ___PA___,1lnk #18mov #345,w0mov w0,[w14+2]mov #678,w0mov w0,[w14+4]mov #41943,w0mov #16048,w1mov w0,[w14+10]mov w1,[w14+12]mov #37224,w0mov #16173,w1mov w0,[w14+14]mov w1,[w14+16]mov [w14+2],w1mov [w14+4],w0mul.ss w1,w0,w0mov w0,[w14]mov [w14+14],w2mov [w14+16],w3mov [w14+10],w0mov [w14+12],w1rcall ___mulsf3mov w0,[w14+6]mov w1,[w14+8]ulnkreturn.set ___PA___,0
.end
4 cycles
124 cycles
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Gestion de types:
#include "p30f6014a.h"
int main(){ int a,b=345,c=678; float fa,fb=0.345,fc=0.678;
a=b*c; fa=fb*fc;
printf("a=%d\n",a); printf("fa=%f\n",fa);
}
Output:
a=-28234fa=0.233910
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Gestion de types:
#include "p30f6014a.h"
int main(){ long int a,b=345,c=678; float fa,fb=0.345,fc=0.678;
a=b*c; fa=fb*fc;
printf("a=%ld\n",a); printf("fa=%f\n",fa);
}
Output:
a=233910fa=0.233910
Ox391B6
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Ordre / type de calcul:#include "p30f6014a.h"#include "math.h"
int main(){ int i; int sinus[360];
for(i=2;i<360;i++) { sinus[i] = 10*sin(i/180.0*3.1415); printf("sin %d = %d\n",i,sinus[i]); }}
Breakpoint: 4949 cycles
sinus[i] = 10*sin(i/(180.0/3.1415)); 4835 cycles (-2.3%)
sinus[i] = 10*sin(i*(3.1415/180.0)); 4570 cycles (-7.6%)
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Simplifications:
• Utilisation d’une table statique ou crée à l’initialisation
• Exploitation de cas spéciaux:Exemple: division d’un float par 2024:
for(i=0;i<360;i++) { sinus[i] = 10*sin(i/(180.0/3.1415)); }
float in, out; /* out = in divided by 2024 */long tmp, tmp2;
tmp = ( *((long *)(&in)));tmp2 = expo - ((long)1<<23)*11;out = ( *((float *)(&tmp2)));
Autonomous Systems Labhttp://asl.epfl.ch
Microinformatique I
Division d’un nombre flottant par 2024:
float
1 [31] 8 [30-23] (BIAS = 127) 23 [22-0]
Sign(signe)
Exponent(exposant)
Fraction(mantisse)
Soustraction de 11 = log2 (2024)
Soustraction de 11 * 223
Résumé: choix d’un type de variable
1. Evaluer la gamme de variation de la variableMinimum et maximum
2. Evaluer la résolution nécessaire3. Estimer le type de calcul nécessaire sur la variable4. Vérifier le temps à diposition pour faire ces calculs5. Choisir une unité pour la variable6. Choisir le type de la variable
Autonomous Systems Labhttp://asl.epfl.ch
TP de cette semaine
Gestion de types
Vérification du temps d’éxécution de calculs
Microinformatique I