From 13a329156f5ae51a0ffffcb0083d807da9e5b19f Mon Sep 17 00:00:00 2001 From: Jerry Ge Date: Mon, 3 Jul 2023 16:36:41 +0000 Subject: Support reading anydtype into a 32-bit buffer Signed-off-by: Jerry Ge Change-Id: Ic6b43539fcb2d75c5614d3addccd24a06e9f2a31 --- src/numpy_utils.cpp | 180 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 155 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/numpy_utils.cpp b/src/numpy_utils.cpp index 65d76e3..d31ec1c 100644 --- a/src/numpy_utils.cpp +++ b/src/numpy_utils.cpp @@ -1,5 +1,5 @@ -// Copyright (c) 2020-2021, ARM Limited. +// Copyright (c) 2020-2023, ARM Limited. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,12 +15,14 @@ #include "numpy_utils.h" #include "half.hpp" - +#include // Magic NUMPY header static const char NUMPY_HEADER_STR[] = "\x93NUMPY\x1\x0\x76\x0{"; static const int NUMPY_HEADER_SZ = 128; // Maximum shape dimensions supported static const int NUMPY_MAX_DIMS_SUPPORTED = 10; +// Offset for NUMPY header desc dictionary string +static const int NUMPY_HEADER_DESC_OFFSET = 8; NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, const uint32_t elems, bool* databuf) { @@ -28,34 +30,118 @@ NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, co return readFromNpyFileCommon(filename, dtype_str, 1, elems, databuf, true); } -NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, const uint32_t elems, uint8_t* databuf) +NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, const uint32_t elems, int32_t* databuf) { - const char dtype_str[] = "'|u1'"; - return readFromNpyFileCommon(filename, dtype_str, sizeof(uint8_t), elems, databuf, false); -} + const char dtype_str_uint8[] = "'|u1'"; + const char dtype_str_int8[] = "'|i1'"; + const char dtype_str_uint16[] = "'