memset

Trying to understand clang/gcc __builtin_memset on constant size / aligned pointers

喜你入骨 提交于 2021-01-28 22:06:33
问题 Basically I am trying to understand why both gcc/clang use xmm register for their __builtin_memset even when the memory destination and size are both divisible by sizeof ymm (or zmm for that matter) and the CPU supports AVX2 / AVX512 . and why GCC implements __builtin_memset on medium sized values without any SIMD (again assuming CPU supports SIMD). For example: __builtin_memset(__builtin_assume_aligned(ptr, 64), -1, 64)); Will compile to: vpcmpeqd %xmm0, %xmm0, %xmm0 vmovdqa %xmm0, (%rdi)

memset和memcpy使用教训

限于喜欢 提交于 2020-11-14 11:59:00
前两天在调试代码时,发现了一个比较低级的错误:对于memset和memcpy接口的参数错误使用。在这里总结一下,避免以后再犯。 先说一下我的错误教训。 memset的使用,代码如下 ? 1 2 3 4 5 INT16 i16ADBuf[4096]; ... memset (i16ADBuf, 6, sizeof (i16ADBuf)); ... 原意是想把数组的每个元素初始化成6,结果调试给出的元素值为1542,让我莫名奇妙,怎么都想不通,后来才发现自己的错误,你发现了吗? memcpy的使用,代码如下 ? 1 2 3 4 5 6 7 INT16 i16ADBuf[4096]; INT16 g_i16ADBuf[4096]; int pointNum = 205; ... memcpy (g_i16ADBuf,i16ADBuf, pointNum); ... 目的是想拷贝已有的pointNum个元素到全局数组中去,结果却只拷贝了一半的元素到目的数组中,在整个应用中我有多处都犯了这个错误。 教训总结: C标准库中头文件中memset和memcpy的接口为: ? 1 2 3 4 5 6 7 #include <string.h> //把buf中的前count个字符替换为ch,并返回buf。 void * memset ( void *buf, int ch, size_t count)

How to initialize a 2D array with all values same?

自古美人都是妖i 提交于 2020-07-04 04:24:30
问题 I want to initialize a 2D array with -1 as all the value. I used memset() for this. #include <bits/stdc++.h> using namespace std; int dp[100][100]; memset(dp, -1, sizeof(dp)); int dynamicProgramming(some parameters) { //I want to use dp[][] array here } int main() { cout<<dp[99][99]; return 0; } But I am getting an error as prog.cpp:5:7: error: expected constructor, destructor, or type conversion before ‘(’ token memset(dp, -1, sizeof(dp)); Can you tell me the correct way to do it? 回答1: As

NOIp 2018 前的图论板子总结

∥☆過路亽.° 提交于 2020-04-07 07:25:05
存图 存边 直接开一个结构体数组存边 struct Edge { int begin, end, weight; } edge[10010]; int edge_count; inline void AddEdge(const int &u, const int &v, const int &w) { edge[edge_count++] = Edge {u, v, w}; } 应用: Kruskal's algorithm Adjacency matrix 用二维数组 adj[i][j] 表示 \(i\) 与 \(j\) 的关系 int adj[1010][1010]; #define ADD_EDGE(u, v, w) adj[u][v] = w 应用: Floyd-Warshall algorithm Hangarian algorithm Kuhn-Munkres algorithm Adjacency list 有几种形式, 以 adj[i] 表示以 \(i\) 为开头的边 应用: 各种图论算法 vector 优点: 访问方便, 存图方便 缺点: 消耗空间, 容易 \(MLE\) ; 删边速度慢 struct Edge { int destination, weight; }; std::vector<Edge> adj[1010]; 加边 #define ADD

拓展欧几里得 专题

放肆的年华 提交于 2020-04-06 11:32:23
大神orz (具体参考请点这) 我根据个人感觉弄了一下自己的思路 ZOJ 3609 : http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 求最小逆元,坑点就是对m=1的特判 /************************************************************** Problem:zoj 3609 User: youmi Language: C++ Result: Accepted Time:0s Memory:272kb ****************************************************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") //#include<bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <stack> #include <set> #include <sstream> #include <cmath> #include <queue>

专题训练之拓步排序

↘锁芯ラ 提交于 2020-04-04 04:26:05
推荐几个博客:https://blog.csdn.net/dm_vincent/article/details/7714519 拓扑排序的原理及其实现 https://blog.csdn.net/u012860063/article/details/38017661 拓扑排序的几种写法 https://blog.csdn.net/shahdza/article/details/7779389 拓扑排序题集 1.基于DFS的拓扑排序:一般适用于数据较小并且需要判断有无环的情况 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxn=150; 6 int n,vis[maxn],topo[maxn],cnt; 7 bool g[maxn][maxn],flag; 8 9 void dfs(int u) 10 { 11 if ( vis[u]<0 ) { 12 flag=false; 13 return; 14 } 15 if ( vis[u]>0 ) return; 16 else vis[u]=-1; //表示当前还在访问中 17 for ( int v=1;flag&&v<=n;v++ ) { 18 if ( g[u][v] ) dfs

强连通专题

删除回忆录丶 提交于 2020-04-04 04:23:57
求割点(无向边): 所谓的割点,就是删除某个点,图便不连通了。 POJ 1523 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MN=1111; struct Edge { int to,next; }edge[MN<<2]; int dfn[MN]; int low[MN]; int head[MN<<2]; int subnet[MN]; int E,tp,root; int vis[MN]; void Init() { memset(dfn,-1,sizeof(dfn)); memset(low,0,sizeof(low)); memset(head,-1,sizeof(head)); memset(subnet,0,sizeof(subnet)); memset(vis,0,sizeof(vis)); E=tp=0; } void Add(int a,int b) { edge[E].to=b; edge[E].next=head[a]; head[a]=E++; } void tarjan(int u,int fa) { int son=0; vis[u]=1; dfn[u]=low[u]=++tp; for(int i=head[u];i!=

专题训练之强连通分量

北城余情 提交于 2020-04-04 04:23:18
tarjan模板 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxn=20010; 6 const int maxm=50010; 7 struct edge{ 8 int to,nxt; 9 }edge[maxm]; 10 int head[maxn],tot; 11 int low[maxn],dfn[maxn],stack[maxn],belong[maxn]; 12 int index,top; 13 int scc; 14 bool vis[maxn]; 15 int num[maxn]; 16 17 void addedge(int u,int v) 18 { 19 edge[tot].to=v; 20 edge[tot].nxt=head[u]; 21 head[u]=tot++; 22 } 23 24 void tarjan(int u) 25 { 26 int v; 27 low[u]=dfn[u]=++index; 28 stack[top++]=u; 29 vis[u]=true; 30 for ( int i=head[u];i!=-1;i=edge[i].nxt ) { 31 v=edge[i].to; 32