【问题标题】:Is there any way to do 128-bit shifts on gcc <4.4?有没有办法在 gcc <4.4 上进行 128 位移位?
【发布时间】:2011-04-07 05:02:00
【问题描述】:

gcc 4.4 似乎是他们添加int128_t 时的第一个版本。我需要使用位移,我的一些位域的空间已经用完了。

编辑:可能是因为我在 32 位计算机上,没有办法在 32 位计算机(Intel Atom)上拥有它,是吗?如果我能按预期进行位移,我不会在意它是否会产生棘手的慢机器代码。

【问题讨论】:

    标签: c gcc bit-shift 128-bit


    【解决方案1】:

    我很确定 __int128_t 在早期版本的 gcc 中可用。刚刚检查了 4.2.1 和 FreeBSD,sizeof(__int128_t) 给出了 16。

    【讨论】:

    • 这似乎不适用于早期的 gcc 4.1.2(例如在 RHEL 5 中)
    • @JosephQuinsey 它does work with gcc 4.1.2
    • 不幸的是,这不适用于 OP,因为他使用的是 32 位操作系统。 GCC 仅支持两倍于寄存器大小的类型,因此如果使用 -m32 编译,__int128_t 将不可用
    【解决方案2】:

    您也可以使用库。这将具有可移植性(关于平台和编译器)的优势,您可以轻松切换到更大的数据类型。我可以推荐的一个是 gmp(即使它的目的不是处理位宽 x,而是随心所欲地变大)。

    【讨论】:

      【解决方案3】:

      任意位数的位移都非常容易。只要记住将溢出的位移到下一个肢体。就是这样

      typedef struct {
         int64_t high;
         uint64_t low;
      } int128_t;
      
      
      int128_t shift_left(int128_t v, unsigned shiftcount)
      {
         int128_t result;
         result.high = (v.high << shiftcount) | (v.low >> (64 - shiftcount));
         result.low  =  v.low  << shiftcount;
         return result;
      }
      

      右移类似

      int128_t shift_right(int128_t v, unsigned shiftcount)
      {
         int128_t result;
         result.low  = (v.low  >> shiftcount) | (v.high << (64 - shiftcount));
         result.high =  v.high >> shiftcount;
         return result;
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用两个 64 位整数,但您需要跟踪位之间的移动。

        【讨论】:

          猜你喜欢
          • 2013-04-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-20
          • 2014-08-25
          • 2010-09-22
          相关资源
          最近更新 更多