1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2013 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci * 23bf215546Sopenharmony_ci * Authors: 24bf215546Sopenharmony_ci * Eric Anholt <eric@anholt.net> 25bf215546Sopenharmony_ci * Matt Turner <mattst88@gmail.com> 26bf215546Sopenharmony_ci * 27bf215546Sopenharmony_ci */ 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include "main/macros.h" 30bf215546Sopenharmony_ci#include "util/streaming-load-memcpy.h" 31bf215546Sopenharmony_ci#include "x86/common_x86_asm.h" 32bf215546Sopenharmony_ci#ifdef USE_SSE41 33bf215546Sopenharmony_ci#include <smmintrin.h> 34bf215546Sopenharmony_ci#endif 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_ci/* Copies memory from src to dst, using SSE 4.1's MOVNTDQA to get streaming 37bf215546Sopenharmony_ci * read performance from uncached memory. 38bf215546Sopenharmony_ci */ 39bf215546Sopenharmony_civoid 40bf215546Sopenharmony_ciutil_streaming_load_memcpy(void *restrict dst, void *restrict src, size_t len) 41bf215546Sopenharmony_ci{ 42bf215546Sopenharmony_ci char *restrict d = dst; 43bf215546Sopenharmony_ci char *restrict s = src; 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci#ifdef USE_SSE41 46bf215546Sopenharmony_ci /* If dst and src are not co-aligned, or if SSE4.1 is not present, fallback to memcpy(). */ 47bf215546Sopenharmony_ci if (((uintptr_t)d & 15) != ((uintptr_t)s & 15) || !cpu_has_sse4_1) { 48bf215546Sopenharmony_ci memcpy(d, s, len); 49bf215546Sopenharmony_ci return; 50bf215546Sopenharmony_ci } 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci /* memcpy() the misaligned header. At the end of this if block, <d> and <s> 53bf215546Sopenharmony_ci * are aligned to a 16-byte boundary or <len> == 0. 54bf215546Sopenharmony_ci */ 55bf215546Sopenharmony_ci if ((uintptr_t)d & 15) { 56bf215546Sopenharmony_ci uintptr_t bytes_before_alignment_boundary = 16 - ((uintptr_t)d & 15); 57bf215546Sopenharmony_ci assert(bytes_before_alignment_boundary < 16); 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci memcpy(d, s, MIN2(bytes_before_alignment_boundary, len)); 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_ci d = (char *)ALIGN((uintptr_t)d, 16); 62bf215546Sopenharmony_ci s = (char *)ALIGN((uintptr_t)s, 16); 63bf215546Sopenharmony_ci len -= MIN2(bytes_before_alignment_boundary, len); 64bf215546Sopenharmony_ci } 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci if (len >= 64) 67bf215546Sopenharmony_ci _mm_mfence(); 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_ci while (len >= 64) { 70bf215546Sopenharmony_ci __m128i *dst_cacheline = (__m128i *)d; 71bf215546Sopenharmony_ci __m128i *src_cacheline = (__m128i *)s; 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_ci __m128i temp1 = _mm_stream_load_si128(src_cacheline + 0); 74bf215546Sopenharmony_ci __m128i temp2 = _mm_stream_load_si128(src_cacheline + 1); 75bf215546Sopenharmony_ci __m128i temp3 = _mm_stream_load_si128(src_cacheline + 2); 76bf215546Sopenharmony_ci __m128i temp4 = _mm_stream_load_si128(src_cacheline + 3); 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ci _mm_store_si128(dst_cacheline + 0, temp1); 79bf215546Sopenharmony_ci _mm_store_si128(dst_cacheline + 1, temp2); 80bf215546Sopenharmony_ci _mm_store_si128(dst_cacheline + 2, temp3); 81bf215546Sopenharmony_ci _mm_store_si128(dst_cacheline + 3, temp4); 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci d += 64; 84bf215546Sopenharmony_ci s += 64; 85bf215546Sopenharmony_ci len -= 64; 86bf215546Sopenharmony_ci } 87bf215546Sopenharmony_ci#endif 88bf215546Sopenharmony_ci /* memcpy() the tail. */ 89bf215546Sopenharmony_ci if (len) { 90bf215546Sopenharmony_ci memcpy(d, s, len); 91bf215546Sopenharmony_ci } 92bf215546Sopenharmony_ci} 93