看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) vc / mingw, windows, 32-bits 問題(Question): 有 library 和 語法/設計 問題,請不吝指點。 Library 我記得早期 vc6.0 時在某份 header 裡面有定義 "類似" __int128 之類的東西 (也可能是我記錯),但一直都不能被使用,鑑於時間有限, 要份效率較佳的 _uint128 library,C/C++ 均可。 自己有花點時間寫份簡易版 class,prototype 大致如下 http://codepad.org/chxsKqql , 內容採用的是 65536 進位, 加上考慮 unsigned 比 unsigned long long 快、處理溢位問題, 所以 data member 弄成 unsigned u[8]. 一般的大數庫 (像 gmp ) 已不考慮了,裡頭沒針對 128-bits 做效能上優化, 另 "我沒記錯的話",gmp 採用的是萬進位,針對 u128 特例應有很大改善空間, 故請教是否有版友使用過效能還不錯的函式庫? 語法/設計 1. 針對 32-bit/64-bit 環境而言,data member 不知會挑下列哪種? unsigned long long u[2]; unsigned long long u[4]; unsigned int u[4]; ---> 若 64-bit 環境之 unsigned int=64bits,我會選這個 unsigned int u[8]; ---> 我於 32-bit環境選擇 2. member function constructor u128::u128(u128 &) {...whatever...} u128::u128 & operator = (const u128& ) { ...whatever...} u128 u128::operator-( const u128& x) const { u128 rst(x); // ----> vc error C2558 unsigned b=0U, s; for(size_t i=0; i<sizeof(u)/sizeof(u[0]); ++i){ s = x[i]+b; rst[i]=u[i]+( (b=(u[i]<s)) <<16)-s; } return rst; } 我以為做了 constructor 出來後,在 member function 裡也可進行調用, 另外在做除法/取模時類似的東西 u128 rst(*this); 也是噴一樣的 error, 請問我觀念哪出錯了? 3. better designed pattern 除了 source 中之 magic number (16 / 65536 這個沒定義) 外, 我想會可能有更好的架構, 特別是最後要 return reference 還是 return object,我寫到後來整個猶豫 Orz. < 除了少數部份是明顯之外,我也只知道 can't return reference of local var.> 另外我沒記錯的話, operator = 只能在類別裡面在複載,但很尬尷的一點是... u128 x; unsinged a; x = a; // 可以做到, u128 overload operator = a = x; // 這個要怎麼做? operator = 不能做成 global ----------- 問題有點多,在此先謝謝各位先進不吝賜教,小弟感激不盡。 -- 就算把新鮮的肝拿回去,還是一樣寫碼到禿頭,加班到天亮, 永遠當老闆的傀儡 你是不是想這麼做? 是的話你就拿回去~ 拿啊!! 九世宅男 : 下輩子不要再讓我幹工程師了 ~ < Kuso 星爺語錄 > -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 180.177.76.161
littleshan:問題2. copy constructor參數沒加const 11/30 10:32
littleshan:3. 用typecast overload 11/30 10:33
EdisonX:謝謝 littleshan,這兩個都解掉了,感謝 :) 11/30 11:08
※ 編輯: EdisonX 來自: 180.177.76.161 (11/30 11:09)
stimim:http://goo.gl/72ia <= c++ 的 operator 還有 prototype 11/30 11:19
loveme00835:gcc/clang on 64bit platform might have types: 11/30 14:53
loveme00835:__(u)int128_t 11/30 14:53
loveme00835:or maybe you can try Boost.Multiprecision 11/30 15:07
EdisonX:@stimim: wiki 那段我有看過,但一直覺得關鍵的 return 11/30 17:23
EdisonX:ref / obj. 它沒做區別,然後也把 const 都拿掉. 11/30 17:23
EdisonX:@loveme00835 : 感謝您提供的東西,謝謝。 11/30 17:24
kevingwn:可以參考_m128i的作法 http://ppt.cc/tm~o 11/30 17:41
EdisonX:k 大您給的 link 太棒了,之前沒找到 M$ 有這個 type, 11/30 17:45
EdisonX:_m128i 的宣告我覺得很漂亮說,感謝您的提供 :) 11/30 17:46
stimim:return type 就看 primitive type 吧,比如說, 11/30 19:39
stimim:(a=b)=c 算是未定義還是等同於a=b? (++a)=3呢? (a++)=3? 11/30 19:43
EdisonX:謝謝 stimim , 我再細思 :) 11/30 22:01
firejox:Q1:用stdint.h 裡的uint32_t,uint64_t去做... 12/01 22:50
EdisonX:@firefox : 謝謝,原本的意思是哪種方式較佳 :) 12/02 12:38