))

Source: Internet
Author: User

01: prevent a header file from being repeatedly contained

# Ifndef COMDEF_H
# Define COMDEF_H
// Header file content
# Endif

 

02: redefines some types to prevent the differences in the number of bytes of the types caused by different platforms and compilers, so as to facilitate migration.
Typedef unsigned char boolean;/* Boolean value type .*/
Typedef unsigned long int uint32;/* Unsigned 32 bit value */
Typedef unsigned short uint16;/* Unsigned 16 bit value */
Typedef unsigned char uint8;/* Unsigned 8 bit value */
Typedef signed long int int32;/* Signed 32 bit value */
Typedef signed short int16;/* Signed 16 bit value */
Typedef signed char int8;/* Signed 8 bit value */

// The following is not recommended
Typedef unsigned char byte;/* Unsigned 8 bit value type .*/
Typedef unsigned short word;/* Unsinged 16 bit value type .*/
Typedef unsigned long dword;/* Unsigned 32 bit value type .*/
Typedef unsigned char uint1;/* Unsigned 8 bit value type .*/
Typedef unsigned short uint2;/* Unsigned 16 bit value type .*/
Typedef unsigned long uint4;/* unsigned 32 bit value type .*/
Typedef signed Char int1;/* signed 8 bit value type .*/
Typedef signed short int2;/* signed 16 bit value type .*/
Typedef long int int4;/* signed 32 bit value type .*/
Typedef signed long sint31;/* signed 32 bit value */
Typedef signed short sint15;/* signed 16 bit value */
Typedef signed char sint7;/* Signed 8 bit value */

 

03: Get a byte or word on the specified address
# Define MEM_ B (x) (* (byte *) (x )))
# Define MEM_W (x) (* (word *) (x )))

 

04: calculate the maximum and minimum values
# Define MAX (x, y) (x)> (y ))? (X): (y ))
# Define MIN (x, y) (x) <(y ))? (X): (y ))

 

05: Get the offset of a field in the struct (struct ).
# Define FPOS (type, field) (dword) & (type *) 0)-> field)

 

06: Get the number of bytes occupied by field in a struct.
# Define FSIZ (type, field) sizeof (type *) 0)-> field)

 

07: convert two bytes into a Word in LSB format
# Define FLIPW (ray) (word) (ray) [0]) * 256) + (ray) [1])

 

08: convert a Word into two bytes in LSB format
# Define FLOPW (ray, val) (ray) [0] = (val)/256); (ray) [1] = (val) & 0xFF)

 

09: Get the address of a variable (word width)
# Define B _PTR (var) (byte *) (void *) & (var ))
# Define W_PTR (var) (word *) (void *) & (var ))

 

10: Get the high and low bytes of a word.
# Define WORD_LO (xxx) (byte) (word) (xxx) & 255 ))
# Define WORD_HI (xxx) (byte) (word) (xxx)> 8 ))

 

11: returns a multiple of 8 nearest to X.
# Define RND8 (x) + 7)/8) * 8)

 

12: convert a letter to uppercase
# Define UPCASE (c)> = 'A' & (c) <= 'Z ')? (C)-0x20): (c ))

 

13: determines whether the character is a decimal number.
# Define DECCHK (c)> = '0' & (c) <= '9 ')

 

14: determines whether the character is a hexadecimal number.
# Define HEXCHK (c)> = '0' & (c) <= '9') (c)> = 'A' & (c) <= 'F ')/
(C)> = 'A' & (c) <= 'F '))

 

15: A method to prevent overflow
# Define INC_SAT (val) (val = (val) + 1> (val ))? (Val) + 1: (val ))

 

16: returns the number of array elements.
# Define ARR_SIZE (a) (sizeof (a)/sizeof (a [0])

 

17: return the value of N at the end of an unsigned number.Mod_by_power_of_two (x, n) = x % (2 ^ N)
# Define mod_by_power_of_two (Val, mod_by) (DWORD) (VAL )&
(DWORD) (mod_by)-1 ))

 

18: For Io space ing in the bucket structure, Input/Output Processing
# Define Indium (port) (* (volatile byte *) (port )))
# Define inpw (port) (* (volatile word *) (port )))
# Define inpdw (port) (* (volatile DWORD *) (port )))
# Define outp (port, Val) (* (volatile byte *) (port) = (byte) (VAL )))
# Define outpw (port, Val) (* (volatile word *) (port) = (Word) (VAL )))
# Define outpdw (port, Val) (* (volatile DWORD *) (port) = (DWORD) (VAL )))

 

19: use some macro tracking for debugging
The ANSI standard specifies five predefined macro names. They are:
_ Line __
_ File __
_ Date __
_ Time __
_ Stdc __
C ++ also defines _ cplusplus

If the compiler is not standard, only a few of the above macro names are supported or not supported at all. Remember that the Compilation Program may also provide other predefined macro names.

_ LINE _ and _ FILE _ macro instructions. # The line command can change its value. In short, during compilation, it contains the current number of lines and FILE names of the program.

The _ DATE _ macro command contains a string in the form of month, day, or year, which indicates the DATE when the source file was translated to the code.
The _ TIME _ macro command contains the program Compilation TIME. Time is represented by a string in the form of minute: Second
_ STDC __
Macro commands are defined during compilation. Generally, if _ STDC _ has been defined, the compiler will only accept standard C/C ++ code that does not contain any non-standard extensions. If the implementation is standard, macro _ STDC _ contains the decimal constant 1. If it contains any other number, the implementation is non-standard.
The consistent compiler of _ cplusplus and Standard c ++ defines it as a value containing at least six characters. A compiler that is inconsistent with the standard c ++ will use a value of 5 or less.

You can define macros, for example:
When _ DEBUG is defined, the output data and the row of the file
# Ifdef _ DEBUG
# Define DEBUGMSG (msg, date) printf (msg); printf ("% d", date, _ LINE _, _ FILE _)
# Else
# Define DEBUGMSG (msg, date)
# Endif

 

20: macro definition to prevent incorrect use of braces.
For example:
Definition in question: # define DUMP_WRITE (addr, nr) {memcpy (bufp, addr, nr); bufp + = nr ;}
Definition: # difne DO (a, B) do {a + B; a ++;} while (0)
For example:
If (addr)
DUMP_WRITE (addr, nr );
Else
Do_somethong_else ();
This will happen after the macro scale:
If (addr)
{Memcpy (bufp, addr, nr); bufp + = nr ;};
Else
Do_something_else ();

When gcc encounters the ";" before else, it considers that the if statement has ended, so the else following it is not in the if statement. With the do {} while (0) definition, there is no problem under any circumstances. The definition of # difne DO (a, B) do {a + B; a ++;} while (0) will not go wrong under any circumstances.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.