From 8647201ac739506cdc692439ed8eb73bc259daea Mon Sep 17 00:00:00 2001 From: Edward Chen <18449977+edgchen1@users.noreply.github.com> Date: Thu, 14 Nov 2019 14:21:05 -0800 Subject: [PATCH] Improved documentation for onnxruntime::utils::SwapByteOrderCopy(), added precondition check. --- onnxruntime/core/framework/endian_utils.cc | 4 ++++ onnxruntime/core/framework/endian_utils.h | 13 ++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/onnxruntime/core/framework/endian_utils.cc b/onnxruntime/core/framework/endian_utils.cc index 9fee46b22c..640ba8df29 100644 --- a/onnxruntime/core/framework/endian_utils.cc +++ b/onnxruntime/core/framework/endian_utils.cc @@ -32,6 +32,10 @@ void SwapByteOrderCopy( assert(element_size_in_bytes > 0); assert(source_bytes.size_bytes() % element_size_in_bytes == 0); assert(source_bytes.size_bytes() == destination_bytes.size_bytes()); + // check non-overlapping + // given begin <= end, end0 <= begin1 || end1 <= begin0 + assert(source_bytes.data() + source_bytes.size() <= destination_bytes.data() || + destination_bytes.data() + destination_bytes.size() <= source_bytes.data()); for (size_t element_offset = 0, element_offset_end = source_bytes.size_bytes(); element_offset < element_offset_end; diff --git a/onnxruntime/core/framework/endian_utils.h b/onnxruntime/core/framework/endian_utils.h index 25f136705a..7da16e6709 100644 --- a/onnxruntime/core/framework/endian_utils.h +++ b/onnxruntime/core/framework/endian_utils.h @@ -14,11 +14,14 @@ namespace onnxruntime { namespace utils { /** - * Swaps the byte order of elements in a buffer. - * This is a low-level funtion - please be sure to pass in valid arguments. - * In particular, source_bytes and destination_bytes should have the same size, - * which should be a multiple of element_size_in_bytes. element_size_in_bytes - * should also be greater than zero. + * Copies elements and swaps their byte orders. + * + * This is a low-level function - please be sure to pass in valid arguments. + * In particular: + * - source_bytes and destination_bytes should have the same size, which should + * be a multiple of element_size_in_bytes. + * - element_size_in_bytes should be greater than zero. + * - source_bytes and destination_bytes should not overlap. * * @param element_size_in_bytes The size of an individual element, in bytes. * @param source_bytes The source byte span.