onnxruntime/winml/lib/Api.Image/NominalRangeConverter.cpp
Tiago Koji Castro Shibata 62c0d24340
Fix Windows Store build (#8753)
* Remove APIs unavailable in Store in #8349, #8178, #8065

* Add UWP stubs of C runtime functions

* Remove UWP incompatible tests from UWP build

* Remove incompatible tests from Store

* Use UWP stubs in store only

* Skip partition check outside of Windows

* Remove unused WRL include

* Workaround Windows header not including what it uses

* Fix precompiled header name clash

* Workaround SDK bugs

* DXCore workaround in Win7

* Fix warning

* Fix more warnings

* Bump WinML to target Windows 8

* Fix more warnings

* Remove unnecessary workarounds

* Remove Desktop only APIs from DML adapter
2021-08-23 11:19:03 -07:00

63 lines
2.1 KiB
C++

#include "lib/Api.Image/pch.h"
#include "inc/NominalRangeConverter.h"
namespace _winml {
NominalRangeConverter::NominalRangeConverter(winml::LearningModelPixelRange pixelRange) {
// For Normalization: the formula is input_range[min, max] / scale - shift
// For DeNormalization: the formula is (input_range[min, max] + shift) * scale
if (pixelRange == winml::LearningModelPixelRange::ZeroTo255) {
scale = 1.f;
shift = 0;
}
else if (pixelRange == winml::LearningModelPixelRange::ZeroToOne) {
scale = 255.f;
shift = 0;
}
else if (pixelRange == winml::LearningModelPixelRange::MinusOneToOne) {
scale = (255.f / 2.f);
shift = 1;
}
};
// [0, 255] --> [0, 255]
// [0, 255] / 255 --> [0, 1]
// [0, 255] * 2 / 255 - 1 --> [-1, 1]
float NominalRangeConverter::Normalize(float val) const {
return val / scale - shift;
}
DirectX::PackedVector::HALF NominalRangeConverter::Normalize(DirectX::PackedVector::HALF val) const {
return static_cast<DirectX::PackedVector::HALF>(val / scale - shift);
}
#if defined(_M_AMD64) || defined(_M_IX86)
__m128 NominalRangeConverter::Normalize(__m128 sse_data) const {
__m128 sse_shift = _mm_set1_ps(static_cast<float>(shift));
__m128 sse_scale = _mm_set1_ps(scale);
auto sse_dived = _mm_div_ps(sse_data, sse_scale);
return _mm_sub_ps(sse_dived, sse_shift);
}
#endif
// [0, 255] --> [0, 255]
// ([0, 1] + 0 ) * 255 -> [0, 1]
// ([-1, 1] + 1) * 255 / 2 --> [-1, 1]
float NominalRangeConverter::Denormalize(float val) const {
return scale * (val + shift);
}
DirectX::PackedVector::HALF NominalRangeConverter::Denormalize(DirectX::PackedVector::HALF val) const {
return static_cast<DirectX::PackedVector::HALF>(scale * (val + shift));
}
#if defined(_M_AMD64) || defined(_M_IX86)
__m128 NominalRangeConverter::Denormalize(__m128 sse_data) const {
__m128 sse_shift = _mm_set1_ps(static_cast<float>(shift));
__m128 sse_scale = _mm_set1_ps(scale);
auto sse_added = _mm_add_ps(sse_data, sse_shift);
return _mm_mul_ps(sse_added, sse_scale);
}
#endif
} // namespace _winml