DSP Programming Skills-Do you know all the data types in DSP?

Publisher:幸福梦想Latest update time:2014-08-10 Source: 互联网Keywords:DSP Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  How many data types are there in DSP C/C++ programming? What are the differences between float, double and long double, long and long long? What are the consequences of using data types incorrectly? If you feel it is not clear, let's take a look at what these are:

Table 1  Data types supported by C28x C/C++

      DSP Programming Skills 16-Data Types in DSP, Do You Know Them?
       Ø 64-bit integer processing

  From the table above, we can see that the C28x compiler supports 64-bit integer types, which makes it particularly convenient to process the feedback data of some high-precision smart encoders. Because when programming on older devices that do not support 64-bit integer types, we need to define the 64-bit type ourselves and define the operation rules ourselves when performing operations. A long long type integer needs to use the ll or LL prefix to be correctly processed by I/O. For example, we can use the following code to correctly display them on the screen:

  printf("%lld", 0x0011223344556677);

  printf("%llx", 0x0011223344556677);

  It should be noted that although the compiler supports 64-bit integers, the actual CPU accumulator and related CPU registers are still 32 bits. When the program is running, the 64-bit integer type is "softly supported" by the CPU. We can add related real-time runtime libraries to improve efficiency, including llabs(), strtoll() and strtoull() functions.

  Ø Floating point processing

  From Table 1, we can see that the C28x compiler supports 32-bit single-precision floating point, 64-bit single-precision and double-precision floating point operations. When defining a double-precision 64-bit variable, remember to use the l or L prefix, otherwise it will be regarded as a double-precision 32-bit variable, resulting in a loss of precision. For example:

  long double a = 12.34L; /* Initialize to double-precision 64-bit floating point */

  long double b = 56.78; /* Convert single-precision floating point to double-precision floating point*/

  When processing I/O, it should also be marked with relevant prefixes, for example:

  printf("%Lg", 1.23L);

  printf("%Le", 3.45L);

  It should be noted that although the compiler supports double-precision floating point, the FPU only supports hardware 32-bit single-precision floating point. When the program is running, the double-precision floating point type is "softly supported" by the CPU. In particular, the operation of long double requires the cooperation of multiple CPU registers to complete (the code size and execution time will increase); in the case of multiple long double operands, the addresses of the first two operands will be passed to the CPU auxiliary registers XAR4 and XAR5, and the other addresses will be placed in the stack. For example, in the following code:

  long double foo(long double a, long double b, long double c)

  {

  long double d = a + b + c;

  return d;

  }

  long double a = 1.2L;

  long double b = 2.2L;

  long double c = 3.2L;

  long double d;

  void bar()

  {

  d = foo(a, b, c);

  }

  When foo is called in function bar(), the values ​​of the CPU registers are:

  DSP Programming Skills 16-Data Types in DSP, Do You Know Them?
       The value of the CPU register register:

  In the floating-point operation of C28x, taking addition as an example, the assembly code is different:

  LCR FS$$ADD ; single-precision addition

  LCR FD$$ADD ; double precision addition

  Generally speaking, double-precision floating-point can be completely unnecessary without special needs. For example, in motor control systems, the accuracy of the entire system cannot be achieved with such high precision due to the accuracy limitations of A/D sampling.

  Ø There are many data types, so be careful when using them

  Single precision and double precision, signed and unsigned, a number greater than 65535 assigned to a 16-bit wide type... These conversions are full of hidden dangers, so be careful when using them! For example:

   If you use Excel to analyze and compare data

  Remember that the floating point type in Excel can only use double-precision floating point numbers. So if you take the single-precision floating point data from DSP and put it into Excel, you will not be surprised if you find that the data has changed. For example, if you put the single-precision floating point 0.2 into Excel, it will become 0.200000002980232.

Keywords:DSP Reference address:DSP Programming Skills-Do you know all the data types in DSP?

Previous article:Design of Suspension Motion Control System Based on DSP and L298N
Next article:DSP Programming Tips - Five Issues You Must Consider When Using Code Optimization

Latest Embedded Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号