久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <tfoot id='Mmvti'></tfoot>
  • <small id='Mmvti'></small><noframes id='Mmvti'>

        <legend id='Mmvti'><style id='Mmvti'><dir id='Mmvti'><q id='Mmvti'></q></dir></style></legend>
      1. <i id='Mmvti'><tr id='Mmvti'><dt id='Mmvti'><q id='Mmvti'><span id='Mmvti'><b id='Mmvti'><form id='Mmvti'><ins id='Mmvti'></ins><ul id='Mmvti'></ul><sub id='Mmvti'></sub></form><legend id='Mmvti'></legend><bdo id='Mmvti'><pre id='Mmvti'><center id='Mmvti'></center></pre></bdo></b><th id='Mmvti'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Mmvti'><tfoot id='Mmvti'></tfoot><dl id='Mmvti'><fieldset id='Mmvti'></fieldset></dl></div>
          <bdo id='Mmvti'></bdo><ul id='Mmvti'></ul>

        不能讓價值通過carry傳播

        Cant make value propagate through carry(不能讓價值通過carry傳播)
        <i id='aPMw9'><tr id='aPMw9'><dt id='aPMw9'><q id='aPMw9'><span id='aPMw9'><b id='aPMw9'><form id='aPMw9'><ins id='aPMw9'></ins><ul id='aPMw9'></ul><sub id='aPMw9'></sub></form><legend id='aPMw9'></legend><bdo id='aPMw9'><pre id='aPMw9'><center id='aPMw9'></center></pre></bdo></b><th id='aPMw9'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='aPMw9'><tfoot id='aPMw9'></tfoot><dl id='aPMw9'><fieldset id='aPMw9'></fieldset></dl></div>

            • <tfoot id='aPMw9'></tfoot>
              <legend id='aPMw9'><style id='aPMw9'><dir id='aPMw9'><q id='aPMw9'></q></dir></style></legend>
              • <bdo id='aPMw9'></bdo><ul id='aPMw9'></ul>

                    <tbody id='aPMw9'></tbody>

                  <small id='aPMw9'></small><noframes id='aPMw9'>

                  本文介紹了不能讓價值通過carry傳播的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  制作一個小的 C++ 大精度類,并且一切似乎都運行良好,但是添加,如果我將 0xffffffff 和 0x04 加在一起,當我應該得到 0x0100000003 時,我得到 0xffff0003.這是有問題的函數:

                  Making a little C++ large precision class, and everything seems to work decent, but the add, if I add 0xffffffff and 0x04 together I get 0xffff0003 when I should get 0x0100000003. Here is the function with the issue:

                  mpfl operator+(const mpfl &lhs, const mpfl &rhs)
                  {
                      unsigned long i;
                      mpfl ret(0);
                      mpfl trhs(rhs);
                      for (i = lhs.nbytes; i >= 0; i--)
                      {
                          if (
                              (unsigned short)lhs.data[i].data + (unsigned short)trhs.data[i].data
                              > (unsigned short)255
                          ) {
                              if (i > 0)
                              {
                                  ret.data[i].carry = 1;
                                  ret.data[0].carry = 0;
                              }
                              else
                              {
                                  ret.data[0].carry = 1;
                              }
                          }
                          else
                              ret.data[i].carry = 0;
                          ret.data[i].data = lhs.data[i].data + trhs.data[i].data;
                          if (i < lhs.nbytes)
                          {
                              if (ret.data[i].data == 255 && ret.data[i + 1].carry == 1)
                                  increment(&trhs, i + 1);
                              ret.data[i].data += ret.data[i + 1].carry;
                          }
                          if (i == 0) break;
                      }
                      return ret;
                  }
                  

                  這里是完整源代碼的鏈接(github 使這更容易,因為有很多)

                  Here are links to the full source (github made this easier since there is a lot of it)

                  • 標題:https://github.com/phyrrus9/mpfl/blob/主/mpfl.h
                  • 類:https://github.com/phyrrus9/mpfl/blob/master/mpfl.cpp
                  • 驅動程序:https://github.com/phyrrus9/mpfl/blob/master/main.cpp

                  推薦答案

                  你的代碼對我來說很亂.我之前做過很多次(長)num 類(浮動,固定,uint,模板化,...)所以這里有一些提示:

                  Your code is very messy to me. I did (long)num classes many times before (floating,fixed,uint,templated,...) so here are some hints:

                  1. 嘗試設置類似于實際硬件實現的 ALU 架構.

                  大多數算法都是為這種環境編寫的.它將清理并加速您的代碼.在某些情況下,我為此使用 asm,但如果您不想 CPU 依賴,您可以使用我的此類

                  Most algorithms are written for such environment. It will clean and speed up your code. In some cases I use asm for this but if you want to be not CPU dependent you can use this class of mine

                  C++ 中的 ALU 源代碼:

                  //---------------------------------------------------------------------------
                  //--- ALU32 class 2.01 ------------------------------------------------------
                  //---------------------------------------------------------------------------
                  #ifndef _ALU32_h
                  #define _ALU32_h
                  //---------------------------------------------------------------------------
                  //#define _ALU32_no_asm
                  //---------------------------------------------------------------------------
                  class ALU32
                      {
                  public:
                      BYTE cy;
                      ALU32() { cy=0; }
                      void sar(DWORD &c); // msb -> [msb...lsb] -> cy     shift arithmetic right
                      void shl(DWORD &c); // cy  <- [msb...lsb] <- 0      shift left
                      void shr(DWORD &c); // 0   -> [msb...lsb] -> cy     shift right
                      void rcl(DWORD &c); // cy  <- [msb...lsb] <- cy     shift through carry left
                      void rcr(DWORD &c); // cy  -> [msb...lsb] -> cy     shift through carry lright
                      void inc(DWORD &c);                                     
                      void dec(DWORD &c);                                     
                      void add(DWORD &c,DWORD a,DWORD b);                     
                      void sub(DWORD &c,DWORD a,DWORD b);                     
                      void adc(DWORD &c,DWORD a,DWORD b);                     
                      void sbc(DWORD &c,DWORD a,DWORD b);                     
                      void mul(DWORD &ch,DWORD &cl,DWORD a,DWORD b);          // (ch,cl) = a*b
                      void div(DWORD &c,DWORD &d,DWORD ah,DWORD al,DWORD b);  // c = a/b d =a%b
                      };
                  //---------------------------------------------------------------------------
                  void ALU32::inc(DWORD &c) { if (c==0xFFFFFFFF) cy=1; else cy=0; c++; }
                  void ALU32::dec(DWORD &c) { if (c==0x00000000) cy=1; else cy=0; c--; }
                  //---------------------------------------------------------------------------
                  void ALU32::sar(DWORD &c)
                      {
                      cy=c&1;
                      c=((c>>1)&0x7FFFFFFF)|(c&0x80000000);
                      }
                  //---------------------------------------------------------------------------
                  void ALU32::shl(DWORD &c)
                      {
                      cy=c>>31;
                      c=(c<<1)&0xFFFFFFFE;
                      }
                  //---------------------------------------------------------------------------
                  void ALU32::shr(DWORD &c)
                      {
                      cy=c&1;
                      c=(c>>1)&0x7FFFFFFF;
                      }
                  //---------------------------------------------------------------------------
                  void ALU32::rcl(DWORD &c)
                      {
                      DWORD cy0=cy;
                      cy=c>>31;
                      c=((c<<1)&0xFFFFFFFE)|cy0;
                      }
                  //---------------------------------------------------------------------------
                  void ALU32::rcr(DWORD &c)
                      {
                      DWORD cy0=cy;
                      cy=c&1;
                      c=((c>>1)&0x7FFFFFFF)|(cy0<<31);
                      }
                  //---------------------------------------------------------------------------
                  void ALU32::add(DWORD &c,DWORD a,DWORD b)
                      {
                      c=a+b;
                      cy=DWORD(((a &1)+(b &1)   )>> 1);
                      cy=DWORD(((a>>1)+(b>>1)+cy)>>31);
                      }
                  //---------------------------------------------------------------------------
                  void ALU32::sub(DWORD &c,DWORD a,DWORD b)
                      {
                      c=a-b;
                      if (a<b) cy=1; else cy=0;
                      }
                  //---------------------------------------------------------------------------
                  void ALU32::adc(DWORD &c,DWORD a,DWORD b)
                      {
                      c=a+b+cy;
                      cy=DWORD(((a &1)+(b &1)+cy)>> 1);
                      cy=DWORD(((a>>1)+(b>>1)+cy)>>31);
                      }
                  //---------------------------------------------------------------------------
                  void ALU32::sbc(DWORD &c,DWORD a,DWORD b)
                      {
                      c=a-b-cy;
                      if (cy) { if (a<=b) cy=1; else cy=0; }
                      else    { if (a< b) cy=1; else cy=0; }
                      }
                  //---------------------------------------------------------------------------
                  void ALU32::mul(DWORD &ch,DWORD &cl,DWORD a,DWORD b)
                      {
                      #ifdef _ALU32_no_asm
                      const int _h=1; // this is MSW,LSW order platform dependent So swap 0,1 if your platform is different
                      const int _l=0;
                      union _u
                          {
                          DWORD u32;
                          WORD u16[2];
                          } u;
                      DWORD al,ah,bl,bh;
                      DWORD c0,c1,c2;
                      // separate 2^16 base digits
                      u.u32=a; al=u.u16[_l]; ah=u.u16[_h];
                      u.u32=b; bl=u.u16[_l]; bh=u.u16[_h];
                      // multiplication (al+ah<<16)*(bl+bh<<16) = al*bl + al*bh<<16 + ah*bl<<16 + ah*bh<<32
                      c0=(al*bl);
                      add(c1,al*bh,ah*bl);
                      c2=(ah*bh)+(cy<<16);
                      // add subresults
                      add(c0,c0,(c1<<16)&0xFFFF0000); c1=((c1>>16)&0x0000FFFF)+cy;
                      add(c1,c1,c2);
                      // construct result from (c3,c2,c1,c0)
                      ch=c1;
                      cl=c0;
                      #else
                      DWORD _a,_b,_cl,_ch;
                      _a=a;
                      _b=b;
                      asm {
                          mov eax,_a
                          mov ebx,_b
                          mul ebx     // H(edx),L(eax) = eax * ebx
                          mov _cl,eax
                          mov _ch,edx
                          }
                      cl=_cl;
                      ch=_ch;
                      #endif
                      }
                  //---------------------------------------------------------------------------
                  void ALU32::div(DWORD &c,DWORD &d,DWORD ah,DWORD al,DWORD b)
                      {
                      #ifdef _ALU32_no_asm
                      DWORD ch,cl,bh,bl,h,l,mh,ml;
                      int e;
                      // edge cases
                      if (!b ){ c=0xFFFFFFFF; d=0xFFFFFFFF; cy=1; return; }
                      if (!ah){ c=al/b;       d=al%b;       cy=0; return; }
                      // align a,b for binary long division m is the shifted mask of b lsb
                      for (bl=b,bh=0,mh=0,ml=1;bh<0x80000000;)
                          {
                          e=0; if (ah>bh) e=+1;   // e = cmp a,b {-1,0,+1}
                          else if (ah<bh) e=-1;
                          else if (al>bl) e=+1;
                          else if (al<bl) e=-1;
                          if (e<=0) break;        // a<=b ?
                          shl(bl); rcl(bh);       // b<<=1
                          shl(ml); rcl(mh);       // m<<=1
                          }
                      // binary long division
                      for (ch=0,cl=0;;)
                          {
                          sub(l,al,bl);           // a-b
                          sbc(h,ah,bh);
                          if (cy)                 // a<b ?
                              {
                              if (ml==1) break;
                              shr(mh); rcr(ml);   // m>>=1
                              shr(bh); rcr(bl);   // b>>=1
                              continue;
                              }
                          al=l; ah=h;             // a>=b ?
                          add(cl,cl,ml);          // c+=m
                          adc(ch,ch,mh);
                          }
                      cy=0; c=cl; d=al;
                      if ((ch)||(ah)) cy=1;       // overflow
                      #else
                      DWORD _al,_ah,_b,_c,_d;
                      _al=al;
                      _ah=ah;
                      _b=b;
                      asm {
                          mov eax,_al
                          mov edx,_ah
                          mov ebx,_b
                          div ebx
                          mov _c,eax  // eax = H(edx),L(eax) / ebx
                          mov _d,edx  // edx = H(edx),L(eax) % ebx
                          }
                      c=_c;
                      d=_d;
                      #endif
                      }
                  //---------------------------------------------------------------------------
                  #endif
                  //---------------------------------------------------------------------------
                  

                  • muldiv 可通過 # 在快速 CPU 匯編和較慢的 C++ 實現之間切換定義_ALU32_no_asm
                  • DWORD 是 32 位 unsigned int 并且可以像 typedef unsigned __int32 DWORD;
                  • 一樣定義

                    • mul and div are switchable between fast CPU assembly and slower C++ implementation with the #define _ALU32_no_asm
                    • DWORD is 32 bit unsigned int and can be defined like typedef unsigned __int32 DWORD;
                    • 那么現在如果你想添加兩個數組(固定大小 N)

                      可以這樣做:

                      ALU32 alu;
                      DWORD a[N],b[N],c[N]; // a[0] is LSB and a[N-1] is MSB
                      
                      alu.add(c[0],a[0],b[0]);
                      for (int i=1;i<N;i++) alu.adc(c[i],a[i],b[i]);
                      // here c[] = a[] + b[]
                      

                      最好使用最大的基數來提高速度.如果您仍然需要 8 位 ALU,由于直接訪問進位,這也可以很容易地重寫甚至簡化.您可以使用 16 位或 32 位變量并直接從子結果中提取 9th 位作為進位(看起來您正在這樣做).

                      it is a good idea to use the biggest base you can to improve speed. If you still need 8 bit ALU this can be also easily rewritten and even simplified due to direct access to carry. You can use 16 or 32 bit variables and extract 9th bit as carry directly from sub-results (looks like you are doing it).

                      您的問題(從評論中復制)

                      我敢打賭,您的問題就在這里:

                      My bet is that your problem is here:

                      if (i<lhs.nbytes)
                              {
                              if (ret.data[i].data == 255 && ret.data[i + 1].carry == 1) increment(&trhs, i + 1);
                              ret.data[i].data += ret.data[i + 1].carry;
                              }
                      

                      carry 應該總是在第一次使用(你總是在最后一次使用).這也揭示了另一種可能性,您的號碼是如何存儲的?

                      carry should be applied always but the first time (you do it always but the last time). This also reveals other possibility how is your number stored?

                      • data[0]LSB 還是 MSB(低/最高位/字節...)?
                      • data[0] is the LSB or MSB (low/most significant bit/byte...)?

                      您必須從最低位開始添加

                      You have to start adding from lowest digits

                      • 所以要么你只是申請隨身攜帶
                      • 或者您正在從高到低添加

                      但展位不正確.

                      PS. 以防您在純 C 中需要 32ALU 樣式的乘法而無需 asm/C++ 看到這個鏈接(但上次更新后這里的代碼已經包含這樣的mul,div):

                      PS. in case you need 32 bit ALU style multiplication without asm in pure C/C++ see this link (but after last update the code here already contains such mul,div):

                      • 在不使用浮點類型的情況下用 C 構建對數函數

                      這篇關于不能讓價值通過carry傳播的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                      【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  In what ways do C++ exceptions slow down code when there are no exceptions thown?(當沒有異常時,C++ 異常會以何種方式減慢代碼速度?)
                  Why catch an exception as reference-to-const?(為什么要捕獲異常作為對 const 的引用?)
                  When and how should I use exception handling?(我應該何時以及如何使用異常處理?)
                  Scope of exception object in C++(C++中異常對象的范圍)
                  Catching exceptions from a constructor#39;s initializer list(從構造函數的初始化列表中捕獲異常)
                  Difference between C++03 throw() specifier C++11 noexcept(C++03 throw() 說明符 C++11 noexcept 之間的區別)

                          <tbody id='Kt6Gq'></tbody>

                        <small id='Kt6Gq'></small><noframes id='Kt6Gq'>

                          <i id='Kt6Gq'><tr id='Kt6Gq'><dt id='Kt6Gq'><q id='Kt6Gq'><span id='Kt6Gq'><b id='Kt6Gq'><form id='Kt6Gq'><ins id='Kt6Gq'></ins><ul id='Kt6Gq'></ul><sub id='Kt6Gq'></sub></form><legend id='Kt6Gq'></legend><bdo id='Kt6Gq'><pre id='Kt6Gq'><center id='Kt6Gq'></center></pre></bdo></b><th id='Kt6Gq'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Kt6Gq'><tfoot id='Kt6Gq'></tfoot><dl id='Kt6Gq'><fieldset id='Kt6Gq'></fieldset></dl></div>
                          <legend id='Kt6Gq'><style id='Kt6Gq'><dir id='Kt6Gq'><q id='Kt6Gq'></q></dir></style></legend>
                          • <bdo id='Kt6Gq'></bdo><ul id='Kt6Gq'></ul>
                          • <tfoot id='Kt6Gq'></tfoot>
                            主站蜘蛛池模板: 天天综合日日夜夜 | 欧美精品乱码99久久影院 | 欧美v在线| 国产免费自拍 | 亚洲精品一区二区在线观看 | 在线亚洲一区 | 91国内在线观看 | 在线观看电影av | 在线免费观看黄色网址 | 久久久影院 | 国产精品爱久久久久久久 | 国产中的精品av涩差av | 日韩精品在线看 | 成人超碰| 九九亚洲 | 一级二级三级黄色 | av在线视 | av免费网站在线观看 | 在线一级片 | 欧美片网站免费 | 亚洲欧洲精品在线 | 日本久久综合 | 91xxx在线观看 | 亚洲精品久久久久国产 | 五十女人一级毛片 | 成人久久18免费 | 亚洲精品一区二区二区 | jizz中国日本 | 国产98色在线 | 日韩 | 少妇一级淫片aaaaaaaaa | 午夜精品久久久久99蜜 | 成人福利网站 | 免费观看成人性生生活片 | 久久99精品久久久久久噜噜 | 亚洲高清在线观看 | 久久久久精 | 欧美二区三区 | 中文字幕在线观看www | 久久综合香蕉 | 天堂av影院 | 521av网站|