hrefspace

 找回密码
 立即注册
搜索
热搜: PHP PS 程序设计
查看: 461|回复: 7

求助 C++ 的 mode(TI)

[复制链接]

566

主题

578

帖子

1837

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1837
发表于 2024-3-25 15:50:11 | 显示全部楼层 |阅读模式
看到某人的C++ 代码,里面有一行看不懂, 求解释~~

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

使用道具 举报

0

主题

172

帖子

2

积分

新手上路

Rank: 1

积分
2
发表于 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:
  1. typedef unsigned int dword_t __attribute__((mode(DI)));
复制代码
On 32 bit systems and
  1. typedef 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:
  1. 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).
回复

使用道具 举报

0

主题

166

帖子

2

积分

新手上路

Rank: 1

积分
2
发表于 2024-3-25 15:51:20 | 显示全部楼层
2# gxqcn

我看过了,不知道老大平时用过这个语句没

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

0

主题

183

帖子

159

积分

关内侯

Rank: 2

积分
159
发表于 2024-3-25 15:52:09 | 显示全部楼层
感觉把C++用到这种程度的一定是火星人
回复

使用道具 举报

0

主题

192

帖子

2

积分

新手上路

Rank: 1

积分
2
发表于 2024-3-25 15:52:27 | 显示全部楼层
3# wayne


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

使用道具 举报

0

主题

153

帖子

125

积分

关内侯

Rank: 2

积分
125
发表于 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++火星人提供的代码如下:
  1. #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[len];int primes[len/10];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[v];    int64_t p = v;    for(int index = 0; primes[index] < q && index < numPrimes; ++index)        if(v%primes[index] == 0)    {        p = primes[index];        break;    }    int count = 0;    //std::printf("* %ld %ld  %ld\n", v, p, q);    //assert(v < len);    //while(p == primeFac[v])    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[s+c*siz]*p);    //std::printf("/ %ld %ld  %ld\n", v, p, q);    return ret;} int main(){    for(int i = 0; i < len; ++i)        primeFac[i] = i;    for(int i = 2; i < len; ++i)        if(primeFac[i] == i)            for(int j = i; j < len; j += i)                primeFac[j] = i;     numPrimes = 0;    for(int i = 2; i < len; ++i)        if(primeFac[i] == i)            primes[numPrimes++] = 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());}*/
复制代码
回复

使用道具 举报

0

主题

188

帖子

2

积分

新手上路

Rank: 1

积分
2
发表于 2024-3-25 15:54:00 | 显示全部楼层
这个限定是不是通知编译器可以向量优化?比如用MMX、SIMD、3DNow! 等汇编指令。

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

使用道具 举报

0

主题

182

帖子

2

积分

新手上路

Rank: 1

积分
2
发表于 2024-3-25 15:54:30 | 显示全部楼层
7# gxqcn
stackoverflow.com上的回答好像是在说,直接显式的指定一个类型的大小,而与编译器和机器类型无关。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|hrefspace

GMT+8, 2024-4-27 19:39 , Processed in 0.073592 second(s), 22 queries .

Powered by hrefspace X3.4 Licensed

Copyright © 2022, hrefspace.

快速回复 返回顶部 返回列表