GCC-2——杂项汇总

1. __builtin_constant_p

它是GCC的内建函数,用于判断一个值是否为编译时常数,如果参数是常数,函数返回 1,否则返回 0。

#include <stdio.h>

#define MAX 10 //built in constant

int global = 10; //non built in constant

int * const p = NULL; //non built in constant

int test_func() //non built in constant
{
    return 0;
}

void main()
{
    if (__builtin_constant_p(p)) {
        printf("built in constant\n");
    } else {
        printf("non built in constant\n");
    }
}

 

2. 编译器会丢弃黑盒代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    long i, j, k, loop_cnt = 100, get_result = 0;
    unsigned long x = 0;

    if (argc == 2)
        loop_cnt = atol(argv[1]);
    if (argc == 3)
        get_result = atol(argv[2]);

    printf("HAM_T: loop_cnt=%ld\n", loop_cnt);

    while(true) {
        for (i = 1; i < loop_cnt; i++) {
            for (j = 1; j < loop_cnt; j++) {
                for (k = 1; k < loop_cnt; k++) {
                    x = i * j % k;
                }
            }
        }
        usleep(4 * 1000); //4ms
        /* if no this load never up */
        if (get_result)
            printf("x=%ld\n", x);
    }

    return 0;
}

 

posted on 2022-11-26 23:20  Hello-World3  阅读(98)  评论(0)    收藏  举报

导航