2011年5月26日 星期四

Linux中的likely,unlikely

likely跟unlikely其實是兩個define,其內容如下:
#define likely(x) __builtin_expect(!!(x), 1)  
#define unlikely(x) __builtin_expect(!!(x), 0)  


它的功用比較像是提醒compiler這個判斷式比較容易為真或比較容易為假
進而調整pipeline...
比較可能執行的放到較近的地方,比較不可能執行的放到較遠的地方
以下是 __builtin_expect的說明


__builtin_expect(),查閲GCC手册,發現其定義如下:
long __builtin_expect (long exp, long c) [Built-in Function]
You may use __builtin_expect to provide the compiler with branch prediction
information. In general, you should prefer to use actual profile feedback for this
(‘-fprofile-arcs’), as programmers are notoriously bad at predicting how their
programs actually perform. However, there are applications in which this data is
hard to collect.
The return value is the value of exp, which should be an integral expression. The
value of c must be a compile-time constant. The semantics of the built-in are that it
is expected that exp == c. For example:
if (__builtin_expect (x, 0))
foo ();
would indicate that we do not expect to call foo, since we expect x to be zero. Since
you are limited to integral expressions for exp, you should use constructions such as
if (__builtin_expect (ptr != NULL, 1))
error ();
when testing pointer or floating-point values.

大概是說我們可以用__builtin_expect()來提供compiler分支預測
下面懶的翻了…反正就我們預測它比較可能為真就用likely
比較不會成立就用unlikely...

2011年5月25日 星期三

隱藏Blogger上面的導航列(Navbar)

如果有人覺得上方的那列很礙眼可以這樣做

在"設計"的"修改HTML"中
找到
/* Primary layout */
然後在它下面加上

#navbar-iframe {
display: none !important;
}


就OK了

於是我就不知道怎麼登入了…

2011年5月16日 星期一

sscanf心得

%*s => 加上星號表視忽略該字串,簡單來說,就是無法在後面的不定參數中接收該值

For example:
假設source為
<changecount attr="500">100</changecount>

sscanf(text,"%*[^>]>%[^<]",tmp); %*[^>] 就是取到第一次的>前,加上星號代表這是用不到的部份
也就是<changecount attr="500" 為止,
此時的目標變成>100</changecount>
接著%[^<] 代表</changecount>的<前 於是就剩下>100啦
再加上上面的>符號取出來的東西就是100了。


int main()
{
char text[]="1005";
char text2[]= "attr = \"1000\" str = \"50000\"";
double number=0;
char tmp[100];

sscanf(text,"%*[^>]>%d",&number);
printf("Himiro Test:number is %d\n",number);

//sscanf(text,"%*[^>]>%[^<]<%*[^\n]",tmp);
sscanf(text,"%*[^>]>%[^<]",tmp);
printf("Himiro Test:number is %s\n",tmp);

sscanf(text2,"%*[^\"]\"%d",&number);
printf("Himiro Test:value is %d\n",number);

//sscanf(text2,"%*[^\"]\"%[^\"][^\"]",tmp);
sscanf(text2,"%*[^\"]\"%[^\"]",tmp);   //%*[^"]"%[^"]
printf("Himiro Test:value is %s\n",tmp);
}

Result:
Himiro Test:number is 1005
Himiro Test:number is 1005
Himiro Test:value is 1000
Himiro Test:value is 1000

Print colored line in C


#include <stdio.h>
#include <stdarg.h>

#define DEBUG_GREEN "\033[35;32m"
#define DEBUG_LIGHT_BLUE "\033[01;34m"
#define DEBUG_RESTORE "\033[0m"

#define DBG(x,fmt, arg...) debug(x,fmt, ## arg)
extern void debug(const char *color,const char *format, ...)__attribute__((forma t(printf,2,3)));

void debug(const char *color,const char *format, ...)
{
char text[1024];
va_list ap;
//if (!debug_enabled)
// //return;

va_start(ap, format);
vsnprintf((char *)text,1024,format, ap);
va_end(ap);
if (text[strlen(text) - 1] == '\n')
{
text[strlen(text) - 1] = 0;
printf("%s%s\033[0m\n",color,text);
}
else
printf("%s%s\033[0m",color,text);
}

int main()
{
DBG(DEBUG_LIGHT_BLUE,"Himiro Test: %s\n",Hello World);
return 0;
}

Patterns of POSIX.2 REGEX


^ 定位規則,文字開頭
$ 定位規則,文字尾端
. 單一規則,代表任意字元
[chars] 單一規則,有 chars 裡其中一個字元
[^chars] 單一規則,沒有 chars 裡其中一個字元
? 倍數規則, 0 或 1 個的前導符號
* 倍數規則, 0 或多個的前導符號
+ 倍數規則, 1 或多個的前導符號
{n,m} 表示前一符號在字串中的重覆次數。
例如 A{2} 表示 'A' 重覆兩次 (即 'AA') ;
A{2,} 表示字串含有 2 到無數多個 'A' ;
A{2,5} 表示含有 2 到 5 個 'A' 。
\char 轉義,將 char 視為一般字元,而非樣式規則字元
(string) 子樣式規則,將 string 記憶起來,歸於一組。
稍後可利用 \n 的方式,將第 n 組 string 提出。

2011年5月12日 星期四

Typeless swap

Today, one of my colleagues ask me how to create a typeless swap marco,
This is the solution.

#define MySWAP(a, b) \
do { \
typeof (a) _temp = (a);\
(a) = (b); \
(b) = _temp; \
} while(false)

int main()
{
float a=1.33;
float b=2.75;
MySWAP(a,b);
}