1. double -> int //directx9c以上失效,因为其会改变单浮点精度
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ftol
1
inline long ftol(double x)
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
#ifdef __WINDOWS__
4
static const double magic = 6755399441055744.0; // 2^51 + 2^52
5![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
double tmp = x;
7
tmp += (x > 0) ? -0.499999999999 : +0.499999999999;
8
tmp += magic;
9
return *(long*)&tmp;
10
#else
11
return (long)x;
12
#endif
13
}
2. c双向链表操作
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
list
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
struct list_head
{
2
struct list_head *next, *prev;
3
};
4
5
6
#define LIST_HEAD_INIT(name) { &(name), &(name) }
7
8
#define LIST_HEAD(name) \
9
struct list_head name = LIST_HEAD_INIT(name)
10
11
#define INIT_LIST_HEAD(ptr) do { \
12
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
13
} while (0)
14
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
16
* Insert a new entry between two known consecutive entries.
17
*
18
* This is only for internal list manipulation where we know
19
* the prev/next entries already!
20
*/
21
static inline void __list_add(struct list_head *new,
22
struct list_head *prev,
23
struct list_head *next)
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
25
next->prev = new;
26
new->next = next;
27
new->prev = prev;
28
prev->next = new;
29
}
30
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**
32
* list_add - add a new entry
33
* @new: new entry to be added
34
* @head: list head to add it after
35
*
36
* Insert a new entry after the specified head.
37
* This is good for implementing stacks.
38
*/
39
static inline void list_add(struct list_head *new, struct list_head *head)
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
41
__list_add(new, head, head->next);
42
}
43
44![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
..
45
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**
47
* list_entry - get the struct for this entry
48
* @ptr: the &struct list_head pointer.
49
* @type: the type of the struct this is embedded in.
50
* @member: the name of the list_struct within the struct.
51
*/
52
#define list_entry(ptr, type, member) \
53
container_of(ptr, type, member)
54
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**
56
* list_for_each - iterate over a list
57
* @pos: the &struct list_head to use as a loop counter.
58
* @head: the head for your list.
59
*/
60
#define list_for_each(pos, head) \
61
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
62
pos = pos->next)
container_of 宏
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* GCC */
2
#define container_of(ptr, type, member) ({ \
3
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
4
(type *)( (char *)__mptr - offsetof(type,member) );})
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
//or
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
#define container_of(ptr, type, member) ( \
9
(type *)( (char *)ptr - offsetof(type,member) ) )
how to use
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
struct my_data
{
2
int x;
3
int y;
4
struct list_head list;
5
}
6
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* 链表头 */
8
LIST_HEAD(my_listhead);
9
10
void my_function()
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 节点对象 */
14
struct my_data *node_1 = (struct my_data *) malloc(sizeof(struct my_data));
15
struct my_data *node_2 = (struct my_data *) malloc(sizeof(struct my_data));
16
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 加入链表 */
18
list_add (node_1->list, &my_listhead);
19
list_add (node_2->list, &my_listhead);
20
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 遍历链表 */
22
struct my_data * node;
23
struct list_head *pos;
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
list_for_each (pos, &my_listhead)
{
25
node = list_entry (pos, struct my_data, list);
26
27
}