Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:50

0001 /*
0002 Main program to test function dump LUTs, called IEEE32toDSP
0003 Author: A. Gozzelino
0004     INFN LNL & Padova University
0005 Date: May 11th 2012
0006 */
0007 
0008 //---------------
0009 // C++ Headers --
0010 //---------------
0011 #include <iostream>
0012 #include <iomanip>
0013 #include <fstream>
0014 #include <sstream>
0015 #include <cstring>
0016 #include <math.h>   
0017 #include <stdio.h>
0018 
0019 using namespace std;
0020 
0021 // Function under test
0022 void IEEE32toDSP(float f, short int & DSPmantissa, short int & DSPexp);
0023 
0024 //  Main program
0025 int main()
0026 {
0027 
0028   float ST    = 30.;
0029   float pitch = 4.2;
0030   float h     = 1.3;
0031   float DD    = 18.;
0032 
0033   for(int i=-511;i<512;i++)  {
0034     float fpsi =  atan( ((float)(i) * pitch) /(DD * h * ST ) );
0035     unsigned short int ipsi = int(fpsi*512);
0036     unsigned short int ipsi_9bits = ipsi & 0x1FF;
0037     //cout << dec << setw(3) << i << setw(10) << fpsi << hex <<
0038         //setw(10) << ipsi << setw(10) << ipsi_9bits << endl;
0039   }
0040 
0041   // convert parameters from IEE32 float to DSP float format
0042   short int DSPmantissa = 0;
0043   short int DSPexp = 0;
0044   float d = 34.;
0045   short int btic = 31;
0046   cout << "CHECK BTIC " << btic << endl;
0047   short int Low_byte = (btic & 0x00FF);   // output in hex bytes format with zero padding
0048   short int High_byte =( btic>>8 & 0x00FF);
0049   
0050   cout << setw(2) << setfill('0') << hex << High_byte << setw(2) << setfill('0')
0051     << Low_byte << endl;    
0052 
0053   // d parameter conversion and dump
0054   IEEE32toDSP(d, DSPmantissa, DSPexp);
0055 
0056   Low_byte = (DSPmantissa & 0x00FF);   // output in hex bytes format with zero padding
0057   High_byte =( DSPmantissa>>8 & 0x00FF);
0058   cout << setw(2) << setfill('0') << hex << High_byte << setw(2) << setfill('0') << Low_byte << endl;   
0059   Low_byte = (DSPexp & 0x00FF);
0060   High_byte =( DSPexp>>8 & 0x00FF);
0061   cout << setw(2) << setfill('0') << High_byte << setw(2) << setfill('0') << Low_byte << endl;  
0062 
0063   return 0;
0064 }
0065 
0066 // Function under test: correct definition 
0067 // Function used during test beam phase. Today it is not called anymore. Keep it as reference for test.
0068 
0069 void IEEE32toDSP(float f, short int & DSPmantissa, short int & DSPexp)
0070 {
0071   long int pl = 0;
0072   long int lm;
0073 
0074   bool sign=false;
0075 
0076   DSPmantissa = 0;
0077   DSPexp = 0;
0078 
0079   if( f!=0.0 )
0080   {
0081         memcpy (&pl,&f,sizeof(float)); 
0082         if((pl & 0x80000000)!=0) 
0083         sign=true;    
0084         lm = ( 0x800000 | (pl & 0x7FFFFF)); // [1][23bit mantissa]
0085         lm >>= 9; //reduce to 15bits
0086       lm &= 0x7FFF;
0087         DSPexp = ((pl>>23)&0xFF)-126;
0088       DSPmantissa = (short)lm;
0089       if(sign) 
0090         DSPmantissa = - DSPmantissa;  // convert negative value in 2.s complement   
0091         
0092     /*
0093     //*********************************
0094     // Old and wrong definition 
0095     //*********************************
0096     //long int *pl=0, lm;
0097     bool sign=false;
0098 
0099         memcpy(pl,&f,sizeof(float));
0100         if((*pl & 0x80000000)!=0) 
0101         sign=true;    
0102         lm = ( 0x800000 | (*pl & 0x7FFFFF)); // [1][23bit mantissa]
0103         lm >>= 9; //reduce to 15bits
0104       lm &= 0x7FFF;
0105         DSPexp = ((*pl>>23)&0xFF)-126;
0106       DSPmantissa = (short)lm;
0107       if(sign) 
0108         DSPmantissa = - DSPmantissa;  // convert negative value in 2.s complement
0109     //***********************************************   
0110         */   
0111      
0112   }
0113   return;
0114 }
0115