Api.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "CommonCorePch.h"
  6. #ifdef _WIN32
  7. #include <wchar.h> // wmemcpy_s
  8. #endif
  9. int AssertCount = 0;
  10. int AssertsToConsole = false;
  11. #if _WIN32
  12. // IsInAssert defined in header
  13. #elif !defined(__IOS__)
  14. __declspec(thread) int IsInAssert = false;
  15. #else
  16. int IsInAssert = false;
  17. #endif
  18. void __stdcall js_memcpy_s(__bcount(sizeInBytes) void *dst, size_t sizeInBytes, __in_bcount(count) const void *src, size_t count)
  19. {
  20. Assert((count) <= (sizeInBytes));
  21. if ((count) <= (sizeInBytes))
  22. memcpy((dst), (src), (count));
  23. else
  24. Js::Throw::FatalInternalError();
  25. }
  26. void __stdcall js_wmemcpy_s(__ecount(sizeInWords) char16 *dst, size_t sizeInWords, __in_ecount(count) const char16 *src, size_t count)
  27. {
  28. //Multiplication Overflow check
  29. Assert(count <= sizeInWords && count <= SIZE_MAX/sizeof(char16));
  30. if(!(count <= sizeInWords && count <= SIZE_MAX/sizeof(char16)))
  31. {
  32. Js::Throw::FatalInternalError();
  33. }
  34. wmemcpy_s(dst, count, src, count);
  35. }
  36. #if defined(_M_IX86) || defined(_M_X64)
  37. void __stdcall js_memset_zero_nontemporal(__bcount(sizeInBytes) void *dst, size_t sizeInBytes)
  38. {
  39. if ((sizeInBytes % sizeof(__m128i)) == 0)
  40. {
  41. size_t writeBytes = 0;
  42. __m128i simdZero = _mm_setzero_si128();
  43. for (__m128i * p = (__m128i *)dst; writeBytes < sizeInBytes; p += 1, writeBytes += sizeof(__m128i))
  44. {
  45. _mm_stream_si128(p, simdZero);
  46. }
  47. _mm_sfence();
  48. }
  49. // cannot do non-temporal store directly if set size is not multiple of sizeof(__m128i)
  50. else
  51. {
  52. memset(dst, 0, sizeInBytes);
  53. }
  54. }
  55. #endif