iii 发表于 2024-3-25 15:50:11

求助 C++ 的 mode(TI)

看到某人的C++ 代码,里面有一行看不懂, 求解释~~

int64_t __attribute__((mode(TI))) a = p*q; a *= r;

ouukucakac 发表于 2024-3-25 15:50:36

引用自:http://stackoverflow.com/questio ... -modexx-actually-do

Q:Hi All,

This arose from a question earlier today on the subject of bignum libraries and gcc specific hacks to the c language. Specifically, these two declarations were used:typedef unsigned int dword_t __attribute__((mode(DI)));On 32 bit systems andtypedef unsigned int dword_t __attribute__((mode(TI)));On 64-bit systemms.

I assume given this is an extension to the C language that there exists no way to achieve whatever it achieves in current (C99) standards. So my questions are simple: is that assumption correct? And what do these statements do to the underlying memory? I think the result is I have 2xsizeof(uint32_t) for a dword in 32-bit systems and 2*sizeof(uint64_t) for 64-bit systems, am I correct?

Thanks for all your help,

A:These allow you to explicitly specify a size for a type without depending on compiler or machine semantics, such as the size of 'long' or 'int'.

They are described fairly well on this page.

I quote from that page:QI: An integer that is as wide as the smallest addressable unit, usually 8 bits.HI: An integer, twice as wide as a QI mode integer, usually 16 bits.SI: An integer, four times as wide as a QI mode integer, usually 32 bits.DI: An integer, eight times as wide as a QI mode integer, usually 64 bits.SF: A floating point value, as wide as a SI mode integer, usually 32 bits.DF: A floating point value, as wide as a DI mode integer, usually 64 bits.So DI is essentially sizeof(char) * 8.

Further explanation, including TI mode, can be found here (possibly better than the first link, but both provided for reference).

So TI is essentially sizeof(char) * 16 (128 bits).

ejiwueu 发表于 2024-3-25 15:51:20

2# gxqcn
https://bbs.emath.ac.cn/static/image/smiley/1/victory.gif
我看过了,不知道老大平时用过这个语句没

jnetoji 发表于 2024-3-25 15:52:09

感觉把C++用到这种程度的一定是火星人

edzugxoggixo 发表于 2024-3-25 15:52:27

3# wayne


没用过。我倒是非常需要这样的数据结构。哪里有例程?

ulenkusefihj 发表于 2024-3-25 15:53:03

5# gxqcn
好像是gcc编译器带的,这有文档:
http://gcc.gnu.org/onlinedocs/gcc/X86-Built_002din-Functions.html

问题源自 projctEuler 221题的 讨论专栏里.
http://projecteuler.net/index.php?section=forum&id=221
答案是1884161251122450 ,你输进去即可查看所有人的讨论,
在我的楼下,有一位C++火星人提供的代码如下:#include <cstdio>#include <set>#include <cmath>#include <vector>#include <cassert>//1805315691966540? int const cutoff = 200000;//1638400000;int64_t const len = 1000000/*cutoff+2*/; //(int64_t)cutoff*cutoff+2; int primeFac;int primes;int numPrimes; std::vector<int64_t> factors(int64_t v, int64_t q){    if(v == 1)      return std::vector<int64_t>(1, 1);   //int p = primeFac;    int64_t p = v;    for(int index = 0; primes < q && index < numPrimes; ++index)      if(v%primes == 0)    {      p = primes;      break;    }    int count = 0;    //std::printf("* %ld %ld%ld\n", v, p, q);    //assert(v < len);    //while(p == primeFac)    while(v%p == 0)    {      ++count;      v /= p;      //std::printf("%d %d\n", v, p);    }    std::vector<int64_t> ret = factors(v, std::min(2+(int64_t)sqrt(v), q));    int siz = ret.size();    for(int c = 0; c < count; ++c)      for(int s = 0; s < siz; ++s)            ret.push_back(ret*p);    //std::printf("/ %ld %ld%ld\n", v, p, q);    return ret;} int main(){    for(int i = 0; i < len; ++i)      primeFac = i;    for(int i = 2; i < len; ++i)      if(primeFac == i)            for(int j = i; j < len; j += i)                primeFac = i;   numPrimes = 0;    for(int i = 2; i < len; ++i)      if(primeFac == i)            primes = i;   std::vector<int64_t> test = factors(120, 120);    for(std::vector<int64_t>::iterator i = test.begin(); i != test.end(); ++i)      std::printf("%ld ", *i);    std::printf("\n\n");   std::set<uint64_t> A;   // s = p+q    for(int64_t q = 1; q <= cutoff; ++q)    {      int64_t zeroModS = q*q+1;      std::vector<int64_t> S = factors(zeroModS, q);//std::min(q, 2000000000000000/(q*q)));      if(q == 100000000)      {            std::printf("q = %ld\nS.size() = %zu\n", q, S.size());            for(std::vector<int64_t>::iterator i = S.begin(); i != S.end(); ++i)                std::printf("    %lu\n", *i);      }      //for(int sign = -1; sign <= 1; sign += 2)      int sign = -1;      for(std::vector<int64_t>::iterator i = S.begin(); i != S.end(); ++i)      {            int64_t s = sign * *i;            int64_t p = s-q;            int64_t r = zeroModS/s-q; //-(p*q-1)/(p+q);            int64_t __attribute__((mode(TI))) a = p*q; a *= r;            if(a > 1 && a < 2000000000000000 && p < 2000000000000000/*(1LL<<62)*/)                A.insert(a);      }    }   unsigned n = 0;    for(std::set<uint64_t>::iterator i = A.begin(); i != A.end(); ++i)    {      ++n;      std::printf("%lu\n", *i);      if(n == 150000)      {            std::printf("Done\n");            break;      }    }    std::printf("set size = %zu\n", A.size());} /*int main(){    int cutoff = 1000000000;    std::set<uint64_t> a;    for(int p = 1; p < cutoff; ++p)    {      int range = std::min(cutoff/p, p);      for(int q = -range; q <= range; ++q)            if(q != 0 && p+q != 0)      {            if(((int64_t)p*q-1) % (p+q) == 0)            {                int r = -(p*q-1)/(p+q);                if(r < cutoff)                  a.insert(std::max((int64_t)p*q*r, -(int64_t)p*q*r));            }      }    }    a.erase(0);    unsigned n = 0;    for(std::set<uint64_t>::iterator i = a.begin(); i != a.end(); ++i)    {      ++n;      std::printf("%lu\n", *i);      if(n == 150000)      {            std::printf("Done\n");            break;      }    }    std::printf("set size = %zu\n", a.size());}*/

uucepekoqijew 发表于 2024-3-25 15:54:00

这个限定是不是通知编译器可以向量优化?比如用MMX、SIMD、3DNow! 等汇编指令。

还有,标准C语言原生态整数类型是否最高仅支持到64位?128位的四则运算、位运算等还需要程序员自定义?

urinowiq 发表于 2024-3-25 15:54:30

7# gxqcn
stackoverflow.com上的回答好像是在说,直接显式的指定一个类型的大小,而与编译器和机器类型无关。
页: [1]
查看完整版本: 求助 C++ 的 mode(TI)