2014年8月14日 星期四

even-odd register pair




在32bits 的機器中, 要如何表示64 bit 長度的資料?
ex: long long, unsigned long long, double
有一種方式是用一對32bit register pair 來表示, 稱為even-odd register pair

例如:
unsigned long long ull_input = 0x1122334455667788ULL;

如果有某個指令可以做上下半部交換 稱為swap
則指令可能長成這樣 swap r0
(並不用再指定r1)


但在inline assembly 中使用到register pair
可能會讓你有點混淆

unsigned long long
swap (unsigned long long i1, unsigned i2)
{
  unsigned long long i1_return_value;

  __asm__ ("swap\t%0, %2"
          : "=r" (i1_return_value)
          : "0" (i1), "r" (i2)
          );

  return i1_return_value;
}


首先是i1 是unsigned long long 但是卻只指定給一個register
我一開始很想問 那另外一半是怎麼傳的,
看來應該是compiler 會幫你處理 因為你的i1 明白告知型別是unsigned long long
而回傳 看起來則是把一個register 傳給unsigned long long (只傳了一半)
但其實是整個even-odd 都傳回了。

再考慮endian 
(把even-odd 看成是memory, 前者代表address 小)

big endian ->  unsigned long long 是 even-odd
(把MSB 放到位址小的地方)
little endian ->  unsigned long long 是 odd-even
(把LSB 放到位址小的地方)

所以排起來都是EVEN-ODD   (前者代表address 小)

2014年8月10日 星期日

pointer vs reference



const int * p;  // p is a pointer to const int
int * const p; // p is a const pointer to int
const int * const p; // p is a const pointer to const int

int &r; // r is a const reference to int
(特別注意: 如果你重新assign r, compiler 並不會complaint)
const int &r; // r is a const reference to const int

2014年8月9日 星期六

你不用知道多型中實際的類別是什麼 讓多型的物件自己告訴你 他是誰


前提:
有一個Base class Vehicle
其Subclass Car, Trunk, Helicopter
有一個Container 內型別是Vehicle (但實體是subclass)
今天要對container 內的元素做排序, 兩兩比較
我要如何知道該元素究竟是哪個subclass ?

作法一:
3種類別 兩兩比較, 共有9種可能性
用dynamic_cast 做9個if else.

這個方法是OK的, 但code 看起來會比較髒亂
因為如果是4種類別 會達到16個if else,
更別說如果有10種類別了.

作法二:
讓他自己告訴你他是誰,
例如你想在知道他實際的類別之後 給予priority
則可以在base class 宣告pure virtual function : getPriority
讓子類別分別實作 回傳其priority
這樣一來你只要呼叫getPriority
原本要呼叫9次的if else
只要別成  呼叫兩個getPriority + 一次比較.
瞬間就讓程式碼優美許多

2014年7月18日 星期五

vim 顯示行尾空白, 與tab 字元


良好的 coding style 是不允許行尾空白的儘管事後有很多方式可以自動刪除掉但如果可以在編輯的時候就看到行尾空白 也是很不錯的在.vimrc 貼上下列語法即可" High light unwanted spaces in end of linehighlight ExtraWhitespace ctermbg=darkred guibg=darkcyanautocmd BufEnter * if &ft != 'help' | match ExtraWhitespace /\s\+$/ | endifautocmd BufEnter * if &ft == 'help' | match none /\s\+$/ | endif


另一條良好的coding stype
則是用space 取代tab (因為tab 是不固定的字元數目, 容易造成混亂)
但Makefile 又必須使用tab 而不是space
所以還是有顯示tab 的必要

set listchars=tab:>>
set list



2014年6月22日 星期日

LLVM tablegen multiclass definitions and instances


tablegen 的目的是為了提出records 中,所共有的部分。
所以許多語言特色都是用來簡化,

例如: class template arguments

以下面這個例子來說
def bork {      // Value
  bit isMod = 1;
  bit isRef = 0;
}
def hork {      // Value
  bit isMod = 1;
  bit isRef = 1;
}
def zork {      // Value
  bit isMod = 0;
  bit isRef = 1;
}



可以改寫成


class ModRefValue< bit<2>  value >{
bit<2> Value = value
}

class Value<ModRefValue MR> {
bit isMod = MR.Value{0}
bit isRef = MR.Value{0}
}

def bork : Value<Mod>
def hork : Value<ModRef>
def zork : Value<Ref>


看起來簡化多了。
class template arguments 是用來簡化class 內部的value
接下來介紹multiclass definition and instances
可以用來簡化class instance


def ADD_rr : rrinst<0b111, "add">;
def ADD_ri : riinst<0b111, "add">;
def SUB_rr : rrinst<0b101, "sub">;
def SUB_ri : riinst<0b101, "sub">;
def MUL_rr : rrinst<0b100, "mul">;
def MUL_ri : riinst<0b100, "mul">;


很多指令都有_rr, _ri 這兩種版本,
能不能再class instantiation 的時候只宣告一次add, sub, mul 就好
能可以自動定義出add_rr, add_ri 這兩種版本,
這時候就要用multiclass 跟defm

defm ADD : ri_insn<0b111, "add">
defm SUB : ri_insn<0b101, "sub">
defm MUL : ri_insn<0b100, "mul">

multiclass ri_insn < > {

def _rr
def _ri

}
(產生出來的def 會做string concatenation)

而且defm 不只可以放在外面,也可以放在multiclass裡面
參考 http://llvm.org/docs/TableGen/LangIntro.html


2014年6月17日 星期二

用python 來取代shell script


最近工作上,原本有個程式是用shell script 寫的,目的也就是一些簡單的整合應用
但後來越寫越大, 覺得程式碼好醜( shell language 真是我看過最醜的語言)
索性把整個shell script 改寫成python

原本就預期改寫之後的程式碼會比較清晰 而且更短
結果果然是這樣, 驗證了python 真的能夠快速開發的設計理念

但人還是不滿足
python 最強大的library 也成了最大的問題
這麼多module, function name, 怎麼記得住阿?

救星很可能就是ipython (http://ipython.org/index.html)

2014年6月12日 星期四

Preprocessor # and ##

 

介紹兩個用在macro 的運算子

## : 把macro 裡的參數,跟其他string 串接起來

# : 把macro 裡的參數,當作string

#define w(n) "imm"#n
#define w2(n) imm##n

int main()
{
  int imm5 = 10;
  printf("%s\n", w(10));
  printf("%d\n", w2(5));
  return 0;
}

第一個printf 我想要一個string 當參數 所以用#

第二個printf 我想要一個變數 所以用##

 

Reference

https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html