Storage of deep learning data in memory (C/C++)

Data storage in memory (C/C++)

*Data type classification

1. Plastic surgery

short //------------------------ short integer (2 byte)
unsigned short [int] // unsigned bit
signed short [int] / /Signed bit
int //---------------------------Integer (4 byte)
unsigned int
signed int
long //--- ---------------------Long integer (8 byte)
unsigned long [int]
signed long [int]
char //--------- -------------Character data type (1 byte)
unsigned char
signed char

2. Floating point

float //------(4 byte)
double//------(8 byte)

3. Pointer type

int *pi;
char * pc;
float
* pf;
void
*pv;//----- (cannot dereference and add and subtract operations)

4. Construction type

Array type
structure type struct
enumeration type enum
union type union

5. Empty type

void represents an empty type (no type)

* Plastic storage in memory

Original code, inverse code, complement

There are three ways to represent the signed number in the computer, namely the original code, the inverse code and the complement code.
The three representation methods have two parts: the sign bit and the value bit . The sign bit uses 0 to indicate "positive" and 1 to indicate "negative", and the three representation methods of the value bit are different.
<Original code>
Directly translate binary into binary in the form of positive and negative numbers.
<Inverse code>
Leave the sign bit of the original code unchanged, and the other bits can be obtained by inverting them bit by bit.
<complement code>
Inverse code +1 will get the same original, inverse and complement of the positive number in the complement code
.
Negative numbers are calculated according to the above method, and the negative numbers are stored in the form of complement in the memory.

In computer systems, values ​​are always represented and stored in complements. The reason is that with the complement code, the sign bit and the value field can be processed uniformly; at the same time, addition and subtraction can also be processed uniformly (the CPU only has an adder). In addition, the complement code and the original code are converted mutually, and the operation process is the same. Need extra hardware circuit.

Storage in data sub-memory:

#include<stdio.h>
int main(){
    
    
    int x=10;
    int y=-10;
    printf("%d",x+y);
    return 0;
}

(1) Storage of .x in memory:

Insert picture description here
(2) The storage of .y in the memory:
Insert picture description here
introduction to the big and small end

The computer circuit processes the low-order byte first, and the efficiency is relatively high. Because the calculation starts from the low-order byte, the internal processing of the computer is in little-endian byte order. However, humans are still accustomed to reading and writing big-endian byte order, so in addition to the internal processing of the computer, almost all other occasions are big-endian byte order, such as network transmission and file storage.

Big-endian (storage) mode means that the low-order bits of the data are stored in the high addresses of the memory, and the high-order bits of the data are stored in the low addresses of the memory;

Little-endian (storage) mode means that the low bits of data are stored in the low addresses of the memory, and the high bits of data are stored in the high addresses of the memory.

Why is there a distinction between large and small endian models?

This is because in computer systems, we use bytes as the unit, and each address unit corresponds to a byte, and a byte is 8 bits. But in the C language, in addition to the 8bit char, there are 16bit short type, 32bit long type (depending on the specific compiler), and for processors with more than 8 bits, such as 16-bit or 32-bit Because the register width is greater than one byte, there must be a problem of arranging multiple bytes. This led to the big-endian storage model and the little-endian storage model.
For example, a 16-bit short type x, the address in the memory is 0x0010, and the value of x is 0x1122, then 0x11 is the high byte, and 0x22 is the low byte. For big-endian mode, put 0x11 in the low address, that is, 0x0010, and 0x22 in the high address, that is, 0x0011. Little-endian mode is just the opposite. Our commonly used X86 structure is little-endian mode, while KEIL C51 is big-endian mode. Many ARMs and DSPs are in little-endian mode. Some ARM processors can also choose big-endian mode or little-endian mode by hardware.

*Storage of floating point type in memory

Common floating-point numbers:
3.14159 1E10 The floating-point number family includes: float, double, and long double types. The range of floating-point numbers: defined in float.h

For example: 5.0 in decimal, 101.0 in binary, equivalent to 1.01×2^2.
Then, according to the format of V above, s=0, M=1.01, and E=2 can be obtained. Decimal -5.0, written in binary is -101.0, which is equivalent to -1.01×2^2. Then, s=1, M=1.01, and E=2.
IEEE 754 stipulates:
For 32-bit floating-point numbers, the highest 1 bit is the sign bit s, the next 8 bits are the exponent E, and the remaining 23 bits are the significant digits M.
Insert picture description here

Such as: float x=9.0f;
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_47364122/article/details/109988828