]> git.sesse.net Git - casparcg/blob - common/memory/memcpy.h
2.0.2: memcpy: Added alignment check for safety.
[casparcg] / common / memory / memcpy.h
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #pragma once\r
21 \r
22 #include <assert.h>\r
23 \r
24 #include <tbb/parallel_for.h>\r
25 \r
26 namespace caspar {\r
27 \r
28 namespace internal {\r
29 \r
30 static void* fast_memcpy(void* dest, const void* source, size_t count)\r
31 {\r
32         assert(dest != nullptr);\r
33         assert(source != nullptr);\r
34 \r
35         __asm   \r
36         {      \r
37                 mov esi, source;          \r
38                 mov edi, dest;    \r
39                 mov ebx, count;     \r
40                 shr ebx, 7;\r
41 \r
42                 cpy:             \r
43                         movdqa xmm0, [esi+00h];       \r
44                         movdqa xmm1, [esi+10h];      \r
45                         movdqa xmm2, [esi+20h];         \r
46                         movdqa xmm3, [esi+30h];   \r
47 \r
48                         movntdq [edi+00h], xmm0;\r
49                         movntdq [edi+10h], xmm1;\r
50                         movntdq [edi+20h], xmm2;    \r
51                         movntdq [edi+30h], xmm3;\r
52 \r
53                         movdqa xmm4, [esi+40h];\r
54                         movdqa xmm5, [esi+50h];\r
55                         movdqa xmm6, [esi+60h];\r
56                         movdqa xmm7, [esi+70h];  \r
57 \r
58                         movntdq [edi+40h], xmm4; \r
59                         movntdq [edi+50h], xmm5;      \r
60                         movntdq [edi+60h], xmm6;    \r
61                         movntdq [edi+70h], xmm7;    \r
62 \r
63                         lea edi, [edi+80h];       \r
64                         lea esi, [esi+80h];      \r
65 \r
66                         dec ebx;      \r
67                 jnz cpy;  \r
68         }   \r
69         return dest;\r
70 }\r
71 \r
72 }\r
73 \r
74 static void* fast_memcpy(void* dest, const void* source, size_t count)\r
75 {   \r
76         if((reinterpret_cast<int>(source) & 15) || (reinterpret_cast<int>(dest) & 15))\r
77                 return memcpy(dest, source, count);\r
78 \r
79         if(count < 2048)\r
80                 return memcpy(dest, source, count);\r
81 \r
82         size_t rest = count % 128;\r
83         count -= rest;\r
84 \r
85         tbb::affinity_partitioner ap;\r
86         tbb::parallel_for(tbb::blocked_range<size_t>(0, count/128), [&](const tbb::blocked_range<size_t>& r)\r
87         {       \r
88                 internal::fast_memcpy(reinterpret_cast<char*>(dest) + r.begin()*128, reinterpret_cast<const char*>(source) + r.begin()*128, r.size()*128);   \r
89         }, ap);\r
90 \r
91         return memcpy(reinterpret_cast<char*>(dest)+count,  reinterpret_cast<const char*>(source)+count, rest);\r
92 }\r
93 \r
94 \r
95 }