O HAI THIS BLOG PURPZIEZ 2 B UZED AZ MAH PLESIOUS MEM. :)

2006/05/25

Endian variant issue

今日は在るVHDLモジュールのテストベンチの為に,
C言語でコード生成アプリを作ってた.
で,エンディアン処理みたいなモノにハマる.
[Today, I made tiny app for code generater for a VHDL module's
test bench by C lang.
When I poke that my stupid code, I had endian variant issue.]

どう言うモノかと言うと,
例えば,十六進数の0xABCDを"ABCD"と言う文字列で表したとする.
これを,人間が読みやすい様に,
char hex_str[] = "ABCD";
として,内部で最上位ニブルのAを取り出そうとすると,
hex_str[0];
と書かなければならないし,最下位ニブルを取り出そうとすると,
hex_str[3];
としなければならない.
逆に,hex_str[0]を最下位ニブルにして,
hex_str[3]を最上位ニブルにすると,
char hex_str[] = "DCBA";
として処理しなければならない.
[For instance, to represented 0xABCD as hexadecimal,
in the code has "ABCD" as string.
When it put into C lang world as keep human readability,
char hex_str = "ABCD";
It means that if you want to get upper nibble, it's required to code that
hex_str[0];
And then, if you want to get bottom nibble, it's required to code that
hex_str[3];
OTOH, if you want to get hex_str[0] as bottom nibble
and hex_str[3] as upper nibble,
you should code it like that
char hex_str[] = "DCBA";
So, it seems endian variant issue.]

結局,コード生成の為のアプリなので,内部で置換処理を行う事にした.
デバドラとか固定長の入出力なら#defineマクロで一発なんだろうけど,
可変長入力で,内部で文字列としてのビット列への変換ルーチンも必要.
ま,一度作って仕舞えば,共通部分は流用出来るしねー.
でも,{m,c,re}allocとかで動的確保したメモリ上でそんな事したもんだから,
いっぱいSIGSEGVを喰らいましましたとさ. :P
[Finally, due to it's for code generater app,
I've implement the code like dynamic byte swapping fucs.
The most known way for work-around endian issue in device driver world,
only put it into #define macros.
But, I need its funs as dynamic range and trans to binary strings.
So, I've these funcs are splited by common uses.
But, I poked the heaps which are allocated by {m,c,re}alloc fucs,
so I got ton of SIGSEGV, though. :P]

0 件のコメント: