¿ÇÜĹÀ°¿ô¤ÇStein¤ÎGCD¤ò»È¤¦
¥×¥í¥°¥é¥à¥á¥â | 2012/04/22 Sun 22:43
| ¼¡¤Î¤è¤¦¤Ê¡¢32bit·Ï¤Î¿ÇÜĹÀ°¿ô¥¯¥é¥¹¤¬¤¢¤Ã¤¿¤È¤·¤Æ¡¢
16¿Ê¤Ç¼¡¤Î¤è¤¦¤Ëɽ¤»¤ëÀ°¿ô¤¬¤¢¤ì¤Ð
0x111100002222000033330000
¤³¤Î¥¯¥é¥¹¤Ç¤Ï¼¡¤Î¤è¤¦¤Ë³ÊǼ¤µ¤ì¤ë¤â¤Î¤È¤¹¤ë¡£(Ãí1)
¤³¤ì¤òÍѤ¤¤Æ¡¢Stein's algorithm¤Ë¤è¤ëGCD·×»»¤ò¹Í¤¨¤ë¡£
Stein¤ÎGCD¤Ë¤Ä¤¤¤Æ¤ÏWikiPedia¤¬¾Ü¤·¤¤¡£
http://en.wikipedia.org/wiki/Binary_GCD_algorithm
°ìÈÌ¤Ë ¥æ¡¼¥¯¥ê¥Ã¥É¤Î¸ß½üË¡(³ÈÄ¥) ¤ÎÊý¤¬¥ë¡¼¥×²ó¿ô¤Ï¾¯¤Ê¤¤¤¬¡¢
¾ê;·×»»¤¬¥Í¥Ã¥¯¤Ë¤Ê¤ê¡¢±þÅúÀ¤¬°¤¯¤Ê¤ë¾ì¹ç¤¬Â¿¤¤¡£
⤷¡¢¼ÂÁõ¤¹¤ë¾è»»¡¦¾ê;»»¤Î¥¢¥ë¥´¥ê¥º¥à¤ä¿ôÃͤηå¿ô¤Ë¤è¤Ã¤Æ¤Ï¡¢
¥ë¡¼¥×²ó¿ô¤Î¾¯¤Ê¤¤¥æ¡¼¥¯¥ê¥Ã¥É¤Î¸ß½üË¡(³ÈÄ¥)¤ÎÊý¤¬Áᤤ¾ì¹ç¤â¤¢¤ë¡£
Stein¤Î¥¢¥ë¥´¥ê¥º¥à¤ÇÃÙ¤¤¤È»×¤ï¤ì¤ë¤Î¤Ï¡¢Shift±é»»¤Î²ó¿ô¤Ë¿Ô¤¤ë¡£
¥µ¥ó¥×¥ë¥³¡¼¥É¤ò¤½¤Î¤Þ¤Þ»È¤¦¤È¡¢¤¤¤¯¤éÉé²Ù¤Î¾¯¤Ê¤¤Shift±é»»¤È¸À¤¨¤É¤â¡¢
¿·å±é»»¤Ç½èÍý²ó¿ô¤¬Â¿¤¯¤Ê¤ë¤ÈÂ礤ÊÃÙ±ä¤ò¾·¤¤Þ¤¹¡£
WikiPedia¤ÎStein¤Î¥µ¥ó¥×¥ë¥³¡¼¥É¤ÇÌäÂê¤È¤Ê¤ë¤Î¤Ï¡¢
¤ÎÉôʬ¡£Íפϱ¦Ã¼¤Î£°¤ò½üµî¤·¤Æ¤¤¤ë¤À¤±¤Ç¤¢¤ë¡£
±¦Ã¼¤Ëʤó¤Ç¤¤¤ë£°¤Î¿ô¤ò¿ô¤¨¤ë´Ø¿ô¤òÍÑ°Õ¤·¤Æ¡¢Shift²ó¿ô¤ò¸º¤é¤³¤È¤òÌܻؤ¹¡£
http://en.wikipedia.org/wiki/Count_trailing_zeros ¤Ë¤â¤¢¤ë¤¬¡¢
intel·Ï¤Ç¤¢¤ì¤Ð¡¢¥¢¥»¥ó¥Ö¥é(bsf)¤¬»È¤¨¤ë¡£
BITPERBLOCK¤Ï¡¢ÇÛÎ󣱤Ĥ˴ޤޤì¤ë¿ÇÜĹÀ°¿ô¤ÎBit¿ô¤Ç¤¢¤ë¡£
32bit¤Ç¤¢¤ì¤Ð¡¢32¤¬»È¤ï¤ì¤ë¾ì¹ç¤¬Â¿¤¤¡£(Ãí2)
(64bit¤Î¾ì¹ç¡¢bsfq¤ò»È¤¦¡£)
(m_sz=2, BITPERBLOCK=32)¤Î¼¡¤Î¤è¤¦¤Ê¿ÇÜĹÀ°¿ô¤¬¤¢¤ë¤È¤
¥¢¥»¥ó¥Ö¥é¤ò»È¤ï¤Ê¤¤¼¡¤ÎÊýË¡¤â¤¢¤ë¡£
http://en.wikipedia.org/wiki/Count_trailing_zeros ¤Ë¤â¤¢¤ê¤Þ¤¹¤¬¡¢
£Í·ÏÎó¤ò»È¤Ã¤¿¥Þ¥¸¥Ã¥¯¤Ç¤¹¡£
¤³¤Á¤é¤ÎÊý¤¬¾Ü¤·¤¤¤Ç¤¹¡£ http://d.hatena.ne.jp/siokoshou/20090704
¤¹¤ë¤È¡¢Stein¤Î¥³¡¼¥É¤Ï¼¡¤Î¤è¤¦¤Ë½ñ¤±¤ë¡£
¤³¤ì¤Ç30¡Á40%¤Ï¡¢½èÍý»þ´Ö¤òû½Ì¤Ç¤¤Þ¤·¤¿¡£
¤³¤ì¤³¤½¤¬Steins;Gate¤ÎÁªÂò¢ö
¤³¤Î¥³¡¼¥É¤ò¼ÂÁõ¤·¤¿¥µ¥ó¥×¥ë¤Î16¿Ê¡¦£²¿Ê¡¦10¿ÊÅÅÂî¤Ç¤¹¡£
À°¿ô¤À¤±¤Ç¤¹¤¬¤ª¤è¤½100Ëü·å¤Þ¤Ç·×»»¤Ç¤¤Þ¤¹¡£
(Ãí1) ¸ÇÄêĹ¤Ç¤Ï¤Ê¤¤Bigint¥¯¥é¥¹¤Ç¤Ï¡¢¿Ä¹¡¦½ÌÂàÅù¤Î¥á¥â¥ê¤Î´ÉÍý¤âɬÍפˤʤë¤Î¤Ç¡¢¤³¤ì¤Û¤Éñ½ã¤Ê¤â¤Î¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£
(Ãí2) 32bit(Block)Á´¤Æ¤ò»È¤¦¤³¤È¤¬É¬¤º¤·¤âÍÍø¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£SSE¤ò͸ú¤Ë»È¤¦¤Ê¤é·å¤¢¤Õ¤ì¤¬È¯À¸¤·¤Ê¤¤¤è¤¦ 31bit °Ê²¼¤Ë¤·¤Þ¤¹¡£
Tags: ¥×¥í¥°¥é¥à¥á¥â
class Bigint { public: Bigint(); ... ... ÍÍ¡¹¤ÊÄêµÁ(»Í§±é»»¡¢operator=,operator<<=Åù) ... private: int m_sz; // ÇÛÎó¿ô int* m_num; // ÇÛÎó(¥ê¥È¥ë¥¨¥ó¥Ç¥£¥¢¥ó) };
16¿Ê¤Ç¼¡¤Î¤è¤¦¤Ëɽ¤»¤ëÀ°¿ô¤¬¤¢¤ì¤Ð
0x111100002222000033330000
¤³¤Î¥¯¥é¥¹¤Ç¤Ï¼¡¤Î¤è¤¦¤Ë³ÊǼ¤µ¤ì¤ë¤â¤Î¤È¤¹¤ë¡£(Ãí1)
(m_sz=3) |- m_num[0]-|- m_num[1]-|- m_num[2]-| | 3333 0000 | 2222 0000 | 1111 0000 |
¤³¤ì¤òÍѤ¤¤Æ¡¢Stein's algorithm¤Ë¤è¤ëGCD·×»»¤ò¹Í¤¨¤ë¡£
Stein¤ÎGCD¤Ë¤Ä¤¤¤Æ¤ÏWikiPedia¤¬¾Ü¤·¤¤¡£
http://en.wikipedia.org/wiki/Binary_GCD_algorithm
°ìÈÌ¤Ë ¥æ¡¼¥¯¥ê¥Ã¥É¤Î¸ß½üË¡(³ÈÄ¥) ¤ÎÊý¤¬¥ë¡¼¥×²ó¿ô¤Ï¾¯¤Ê¤¤¤¬¡¢
¾ê;·×»»¤¬¥Í¥Ã¥¯¤Ë¤Ê¤ê¡¢±þÅúÀ¤¬°¤¯¤Ê¤ë¾ì¹ç¤¬Â¿¤¤¡£
⤷¡¢¼ÂÁõ¤¹¤ë¾è»»¡¦¾ê;»»¤Î¥¢¥ë¥´¥ê¥º¥à¤ä¿ôÃͤηå¿ô¤Ë¤è¤Ã¤Æ¤Ï¡¢
¥ë¡¼¥×²ó¿ô¤Î¾¯¤Ê¤¤¥æ¡¼¥¯¥ê¥Ã¥É¤Î¸ß½üË¡(³ÈÄ¥)¤ÎÊý¤¬Áᤤ¾ì¹ç¤â¤¢¤ë¡£
Stein¤Î¥¢¥ë¥´¥ê¥º¥à¤ÇÃÙ¤¤¤È»×¤ï¤ì¤ë¤Î¤Ï¡¢Shift±é»»¤Î²ó¿ô¤Ë¿Ô¤¤ë¡£
¥µ¥ó¥×¥ë¥³¡¼¥É¤ò¤½¤Î¤Þ¤Þ»È¤¦¤È¡¢¤¤¤¯¤éÉé²Ù¤Î¾¯¤Ê¤¤Shift±é»»¤È¸À¤¨¤É¤â¡¢
¿·å±é»»¤Ç½èÍý²ó¿ô¤¬Â¿¤¯¤Ê¤ë¤ÈÂ礤ÊÃÙ±ä¤ò¾·¤¤Þ¤¹¡£
WikiPedia¤ÎStein¤Î¥µ¥ó¥×¥ë¥³¡¼¥É¤ÇÌäÂê¤È¤Ê¤ë¤Î¤Ï¡¢
/* From here on, u is always odd. */ do { while ((v & 1) == 0) /* Loop X */ v >>= 1;
¤ÎÉôʬ¡£Íפϱ¦Ã¼¤Î£°¤ò½üµî¤·¤Æ¤¤¤ë¤À¤±¤Ç¤¢¤ë¡£
±¦Ã¼¤Ëʤó¤Ç¤¤¤ë£°¤Î¿ô¤ò¿ô¤¨¤ë´Ø¿ô¤òÍÑ°Õ¤·¤Æ¡¢Shift²ó¿ô¤ò¸º¤é¤³¤È¤òÌܻؤ¹¡£
http://en.wikipedia.org/wiki/Count_trailing_zeros ¤Ë¤â¤¢¤ë¤¬¡¢
intel·Ï¤Ç¤¢¤ì¤Ð¡¢¥¢¥»¥ó¥Ö¥é(bsf)¤¬»È¤¨¤ë¡£
// // ¹â®²½¤Î¤¿¤á¥¼¥í¥Á¥§¥Ã¥¯¤ò¹Ô¤Ã¤Æ¤¤¤Ê¤¤ // ¥¼¥í¤À¤È¥¢¥¯¥»¥¹°ãÈ¿¤ÎÎã³°¡£ // int Bigint::bsf() const{ int* p = m_num; __asm{ mov edx, [p] mov eax, edx bsf_l2: bsf ecx, [eax] jnz bsf_l1 add eax, 4 jmp bsf_l2 bsf_l1: sub eax, edx shr eax, 2 mov edx, BITPERBLOCK mul edx add eax, ecx } }
BITPERBLOCK¤Ï¡¢ÇÛÎ󣱤Ĥ˴ޤޤì¤ë¿ÇÜĹÀ°¿ô¤ÎBit¿ô¤Ç¤¢¤ë¡£
32bit¤Ç¤¢¤ì¤Ð¡¢32¤¬»È¤ï¤ì¤ë¾ì¹ç¤¬Â¿¤¤¡£(Ãí2)
(64bit¤Î¾ì¹ç¡¢bsfq¤ò»È¤¦¡£)
(m_sz=2, BITPERBLOCK=32)¤Î¼¡¤Î¤è¤¦¤Ê¿ÇÜĹÀ°¿ô¤¬¤¢¤ë¤È¤
bsf()¤Ï¡¢32+16=48 ¤òÊÖ¤¹¡£
|- m_num[0]-|- m_num[1]-| | 0000 0000 | ABCD 0000 |
¥¢¥»¥ó¥Ö¥é¤ò»È¤ï¤Ê¤¤¼¡¤ÎÊýË¡¤â¤¢¤ë¡£
int Bigint::bsf() const{ static int table[32] = {0,1,10,2,11,14,22,3,30,12,15,17,19,23,26,4,31,9,13,21,29,16,18,25,8,20,28,24,7,27,6,5}; int count = 0; for (int i=0; i > 27]; break; } } return count; } // 64bit/Block¤Î¾ì¹ç int Bigint::bsf64() const{ static int table[64] = { 0,1,59,2,60,40,54,3,61,32,49,41,55,19,35,4,62,52,30,33,50,12,14,42,56,16,27,20,36,23,44,5, 63,58,39,53,31,48,18,34,51,29,11,13,15,26,22,43,57,38,47,17,28,10,25,21,37,46,9,24,45,8,7,6}; int count = 0; for (int i=0; i > 58]; break; } } return count; }
http://en.wikipedia.org/wiki/Count_trailing_zeros ¤Ë¤â¤¢¤ê¤Þ¤¹¤¬¡¢
£Í·ÏÎó¤ò»È¤Ã¤¿¥Þ¥¸¥Ã¥¯¤Ç¤¹¡£
¤³¤Á¤é¤ÎÊý¤¬¾Ü¤·¤¤¤Ç¤¹¡£ http://d.hatena.ne.jp/siokoshou/20090704
¤¹¤ë¤È¡¢Stein¤Î¥³¡¼¥É¤Ï¼¡¤Î¤è¤¦¤Ë½ñ¤±¤ë¡£
// // *this = GCD(*this, v) // void Bigint::gcd(const Bigint& v) { if (v == 0){ return; } if (operator==(0)){ operator= (v); return; } Bigint x[2] = {*this, v}; if (x[0] < 0) x[0].operator-(); if (x[1] < 0) x[1].operator-(); int i0 = x[0].bsf(); int i1 = x[1].bsf(); int shift = (i0 < i1) ? i0 : i1; x[0] >>= i0; x[1] >>= shift; int id = 0; do { x[id^1] >>= x[id^1].bsf(); if (x[id] > x[id^1]){ id ^= 1; } x[id^1] -= x[id]; } while (x[id^1] != 0); operator= (x[id] << shift); }
¤³¤ì¤Ç30¡Á40%¤Ï¡¢½èÍý»þ´Ö¤òû½Ì¤Ç¤¤Þ¤·¤¿¡£
¤³¤ì¤³¤½¤¬Steins;Gate¤ÎÁªÂò¢ö
¤³¤Î¥³¡¼¥É¤ò¼ÂÁõ¤·¤¿¥µ¥ó¥×¥ë¤Î16¿Ê¡¦£²¿Ê¡¦10¿ÊÅÅÂî¤Ç¤¹¡£
À°¿ô¤À¤±¤Ç¤¹¤¬¤ª¤è¤½100Ëü·å¤Þ¤Ç·×»»¤Ç¤¤Þ¤¹¡£
(Ãí1) ¸ÇÄêĹ¤Ç¤Ï¤Ê¤¤Bigint¥¯¥é¥¹¤Ç¤Ï¡¢¿Ä¹¡¦½ÌÂàÅù¤Î¥á¥â¥ê¤Î´ÉÍý¤âɬÍפˤʤë¤Î¤Ç¡¢¤³¤ì¤Û¤Éñ½ã¤Ê¤â¤Î¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£
(Ãí2) 32bit(Block)Á´¤Æ¤ò»È¤¦¤³¤È¤¬É¬¤º¤·¤âÍÍø¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£SSE¤ò͸ú¤Ë»È¤¦¤Ê¤é·å¤¢¤Õ¤ì¤¬È¯À¸¤·¤Ê¤¤¤è¤¦ 31bit °Ê²¼¤Ë¤·¤Þ¤¹¡£
Tags: ¥×¥í¥°¥é¥à¥á¥â
author : HUNDREDSOFT | - | -